在工作中,經常遇到需要調用存儲過程,如何調用呢?
下面實現的功能是判斷某參數值,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時存儲存儲過程返回來的結果(該出可以作為彈框等提示作用)