C#如何调用SQL存储过程

在工作中,经常遇到需要调用存储过程,如何调用呢?

下面实现的功能是判断某参数值,Fail时调用存储过程发送邮件至相关人员:

 1 using System;  
 2 using System.Collections.Generic;  
 3 using System.Linq;  
 4 using System.Web;  
 5 using System.Web.UI;  
 6 using System.Web.UI.WebControls;  
 7 using System.Net.Mail;  
 8 using System.Data.SqlClient;  
 9   
10 public partial class SendEmail : System.Web.UI.Page  
11 {  
12     public string mgs;  
13     protected void Page_Load(object sender, EventArgs e)  
14     {  
15     }     
16     protected void Button1_Click(object sender, EventArgs e)  
17     {  
18         if (TextBox1.Text=="Fail")  
19         {  
20         try  
21         {  
22   
23             SqlConnection conn = new SqlConnection("Data Source=10.99.106.120;Initial Catalog=MES;User ID=fisya;pwd=gogofis");  
24             conn.Open();  
25             SqlCommand comm = new SqlCommand();  
26             comm.Connection = conn;  
27             comm.CommandText = "dbo.PlateCheck";  
28             comm.CommandType = System.Data.CommandType.StoredProcedure;  
29             //传值以及赋值  
30   
31             string bianhao = TextBox2.Text;  
32             string RC = TextBox3.Text;  
33             string CA = TextBox4.Text;  
34             SqlParameter[] sps = new SqlParameter[] {   
35                     new SqlParameter("@TextValue",bianhao),  
36                     new SqlParameter("@RC",RC),  
37                     new SqlParameter("@CA",CA),  
38                   };  
39             comm.Parameters.AddRange(sps);  
40             object Result1 = comm.ExecuteScalar();  
41             mgs = "<script>alert('" + Result1 + "')</script>";  
42         }  
43         catch (Exception ex)  
44         {  
45             Response.Write(ex.Message);  
46         }  
47         }  
48     }  
49 } 

1,三个String时需要传递至存储过程的参数

2,Result1时存储存储过程返回来的结果(该出可以作为弹框等提示作用)

posted @ 2018-04-11 19:00  毕吧卟  阅读(4009)  评论(0编辑  收藏  举报