C#怎样调用存储过程
1个回答
展开全部
下面讲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 //执行对表的增删改操作的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 }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询