C# 调用sql的存储过程并传递参数

 1         //执行插入数据的字符串(含参数)
 2         public void ProcInsert(string connStr,string procedureName)
 3         {
 4             //参数区
 5             String conn = connStr;//--连接字符串
 6             String procName = procedureName;//--调用的插入的存储过程
 7 
 8             SqlConnection sqlcon = new SqlConnection(conn);
 9             SqlCommand comm = new SqlCommand(procName, sqlcon);
10             try
11             {
12                 sqlcon.Open();
13                 comm.CommandType = CommandType.StoredProcedure;
14                 comm.Parameters.Add(new SqlParameter("@name", SqlDbType.VarChar));
15                 comm.Parameters["@name"].Value = 4;
16                 comm.Parameters.Add(new SqlParameter("@age", SqlDbType.VarChar));
17                 comm.Parameters["@age"].Value = 4;
18                 comm.Parameters.Add(new SqlParameter("@sex", SqlDbType.VarChar));
19                 comm.Parameters["@sex"].Value = 4;
20                 comm.Parameters.Add(new SqlParameter("@address", SqlDbType.VarChar));
21                 comm.Parameters["@address"].Value = 4;
22                 int trfa = comm.ExecuteNonQuery();
23                 if (trfa==1)
24                     System.Windows.Forms.MessageBox.Show("添加成功");
25             }
26             catch (Exception ex)
27             {
28                 System.Windows.Forms.MessageBox.Show(ex.ToString());
29             }
30             finally {
31                 comm.Clone();
32                 sqlcon.Close();
33             }
posted @ 2012-04-25 15:40  muddy-bear  阅读(1024)  评论(0编辑  收藏  举报