存儲過程(Stored Procedure)即用來存儲數據表操作的一個過程,是把對數據表操作的方法存儲到一起的一個對象,是存儲在數據庫中。
優點:1.降低網絡傳輸數據量:通過存儲過程的名稱和參數傳遞即可調用執行,不用傳輸sql。
2.執行效率高:sqlserver會事先將存儲過程編譯成可執行的二進制代碼,運行存儲過程時,無需再次編譯。
3.封裝性:也可叫模塊式編程,將實現某種功能的多條sql封裝到一個對象中,可多次重復調用,可移植性強。
4.安全:可針對不同的權限的用戶使用不同的存儲過程。
下面講C#如何調用寫好的存儲過程,下面是對數據操作封裝幾個函數
1 //執行對表的增刪改操作的sql語句
2 public static int ExecuteCommand(string text)
3 {
4 using (SqlConnection conn = new SqlConnection(connectionString))
5 {
6 conn.Open();
7 using (SqlCommand cmd = new SqlCommand(text, conn))
8 {
9 int i = cmd.ExecuteNonQuery();
10 return i;
11 }
12 }
13 }
14
15 public static int ExecStoredProcedure(string procName, params SqlParameter[] parameters)
16 {
17 int rtn = 0;
18 using (SqlConnection conn = new SqlConnection(connectionString))
19 {
20 conn.Open();
21 using (SqlCommand cmd = conn.CreateCommand())
22 {
23 SqlTransaction st = conn.BeginTransaction();
24 cmd.Transaction = st;
25 try
26 {
27 cmd.CommandText = procName;
28 cmd.CommandType = CommandType.StoredProcedure;
29 cmd.Parameters.AddRange(parameters);
30 rtn= cmd.ExecuteNonQuery();
31 st.Commit();
32 return rtn;
33 }
34 catch(SqlException sqlex)
35 {
36 st.Rollback();
37 throw sqlex;
38 }
39 }
40 }
41 }
42 public static int ExecuteStoredProcedure(string procName,
43 params SqlParameter[] parameters)
44 {
45 using (SqlConnection conn = new SqlConnection(connectionString))
46 {
47 conn.Open();
48 using (SqlCommand cmd = conn.CreateCommand())
49 {
50 cmd.CommandText = procName;
51 cmd.CommandType = CommandType.StoredProcedure;
52 cmd.Parameters.AddRange(parameters);
53 return cmd.ExecuteNonQuery();
54 }
55 }
56
57 }
推薦用第三種方式,用到了事務處理,若只要有一條不通過全部回滾,避免臟數據的產生。也可以在存儲過程中使用事務,后續會繼續更新...
1 protected void Button1_Click(object sender, EventArgs e)
2 {
3 string mainName = "圓夢組";
4 string detailName="劉能|趙本山|沈燕|歐陽鋒";
5 string detailAge="23|32|18|19";
6 int rtn=DBHelper.ExecuteCommand(string.Format("exec dbo.Proc_TestBatchMainDetailIns '{0}','{1}','{2}'", mainName, detailName,detailAge));
7 }
8 protected void Button2_Click(object sender, EventArgs e)
9 {
10 string mainName = "起航組2";
11 string detailName="段毅2|喬峰2|楊過2|李莫愁2";
12 string detailAge="18|28|jj|35";
13 SqlParameter[] sps = new SqlParameter[] {
14 new SqlParameter("@mainName",mainName),
15 new SqlParameter("@detailNameStr",detailName),
16 new SqlParameter("@detailAgeStr",detailAge)
17 };
18 try
19 {
20 int rtn = DBHelper.ExecuteStoredProcedure("dbo.Proc_TestBatchMainDetailIns", sps);
21 }
22 catch (Exception ex)
23 {
24 Response.Write(ex.Message);
25 }
26
27 }
28 protected void Button3_Click(object sender, EventArgs e)
29 {
30 string mainName = "起航組2";
31 string detailName = "段毅2|喬峰2|楊過2|李莫愁2";
32 string detailAge = "18|28|jj|35";
33 SqlParameter[] sps = new SqlParameter[] {
34 new SqlParameter("@mainName",mainName),
35 new SqlParameter("@detailNameStr",detailName),
36 new SqlParameter("@detailAgeStr",detailAge)
37 };
38 try
39 {
40 int rtn = DBHelper.ExecStoredProcedure("dbo.Proc_TestBatchMainDetailIns", sps);
41 }
42 catch (Exception ex)
43 {
44 Response.Write(ex.Message);
45 }
46 }
protected void Button1_Click(object sender, EventArgs e)
2 {
3 string mainName = "圓夢組";
4 string detailName="劉能|趙本山|沈燕|歐陽鋒";
5 string detailAge="23|32|18|19";
6 int rtn=DBHelper.ExecuteCommand(string.Format("exec dbo.Proc_TestBatchMainDetailIns '{0}','{1}','{2}'", mainName, detailName,detailAge));
7 }
8 protected void Button2_Click(object sender, EventArgs e)
9 {
10 string mainName = "起航組2";
11 string detailName="段毅2|喬峰2|楊過2|李莫愁2";
12 string detailAge="18|28|jj|35";
13 SqlParameter[] sps = new SqlParameter[] {
14 new SqlParameter("@mainName",mainName),
15 new SqlParameter("@detailNameStr",detailName),
16 new SqlParameter("@detailAgeStr",detailAge)
17 };
18 try
19 {
20 int rtn = DBHelper.ExecuteStoredProcedure("dbo.Proc_TestBatchMainDetailIns", sps);
21 }
22 catch (Exception ex)
23 {
24 Response.Write(ex.Message);
25 }
26
27 }
28 protected void Button3_Click(object sender, EventArgs e)
29 {
30 string mainName = "起航組2";
31 string detailName = "段毅2|喬峰2|楊過2|李莫愁2";
32 string detailAge = "18|28|jj|35";
33 SqlParameter[] sps = new SqlParameter[] {
34 new SqlParameter("@mainName",mainName),
35 new SqlParameter("@detailNameStr",detailName),
36 new SqlParameter("@detailAgeStr",detailAge)
37 };
38 try
39 {
40 int rtn = DBHelper.ExecStoredProcedure("dbo.Proc_TestBatchMainDetailIns", sps);
41 }
42 catch (Exception ex)
43 {
44 Response.Write(ex.Message);
45 }
46 }

