none
怎么把SQL的存储过程转成C#代码,举个例子 RRS feed

答案

全部回复

  • 你好,

    请问你是通过c#来执行你的存储过程,还是把存储过程的逻辑用c#。如果是第一种的话,可以使用ado.ne 来做。

    using (SqlConnection conn = new SqlConnection("Data Source=*******;Initial Catalog=ChatApp;User ID=Chatapplication;Password=****"))

    using (SqlCommand cmd = new SqlCommand("Proc_AuditeInstorage", conn)) { string chatroomidno = textBox1.Text; string chatroomname = textBox2.Text; SqlDataAdapter adapt = new SqlDataAdapter(cmd); adapt.SelectCommand.CommandType = CommandType.StoredProcedure;

    //相关参数 adapt.SelectCommand.Parameters.Add(new SqlParameter("@ChatRoomID", SqlDbType.VarChar, 100)); adapt.SelectCommand.Parameters["@ChatRoomID"].Value = chatroomidno; adapt.SelectCommand.Parameters.Add(new SqlParameter("@ChatRoomName", SqlDbType.VarChar, 50)); adapt.SelectCommand.Parameters["@ChatRoomName"].Value = chatroomname; // fill the data table - no need to explicitly call `conn.Open()` - // the SqlDataAdapter automatically does this (and closes the connection, too) DataTable dt = new DataTable(); adapt.Fill(dt); if (dt.Rows.Count > 0) { MessageBox.Show("Connection Succedded"); } else { MessageBox.Show("Connection Fails"); } }

    如果是第二种的话,也是用ado.net 加事务来做。ado.net 来执行每一个SQL 语句,如果存储过程里面有事务的话,转成ado.net 的事务, 关于ado.net 的事务,你可以参考下面的链接.

    https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/local-transactions

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年4月20日 5:13
    版主
  • 就是把存储过程去掉
    2018年4月20日 6:04
  • 你好,

    右边是已经做好的代码吗? 还是你准备用其他的代码做?

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年4月20日 7:35
    版主
  • 类似右边的写法,但是具体的就不懂了
    2018年4月20日 8:07
  • 你好,

    右边的写法有点类似于ado.net entity framework, 你可以参考下面的问题学习一下EF.

    https://msdn.microsoft.com/en-us/library/ee712907(v=vs.113).aspx

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 Lexan-Live 2018年4月21日 1:47
    2018年4月20日 9:38
    版主