SqlHelper类,写的方法如下- class SqlHelper
- {
- private static string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
- public static DataSet ExecuteDataSet(string sql, SqlParameter[] pars)
- {
- using (SqlConnection conn = new SqlConnection(constr))
- {
- conn.Open();
- using (SqlCommand cmd = conn.CreateCommand())
- {
- cmd.CommandText = sql;
- foreach (SqlParameter par in pars)
- {
- cmd.Parameters.AddRange(pars);
- }
- DataSet dst = new DataSet();
- SqlDataAdapter adapter = new SqlDataAdapter(cmd);
- adapter.Fill(dst);
- return dst;
- }
- }
- }
- }
复制代码 在主程序里边调用的时候传入了两个参数,程序报异常了- DataSet ds= SqlHelper.ExecuteDataSet("select * from T_tavle where d=@DDD or f=@fff",new SqlParameter[]{new SqlParameter("@DDD",231),new SqlParameter("@fff",213)});
- foreach (DataRow row in ds.Tables[0].Rows)
- {
- string name = (string)row["s"];
- MessageBox.Show(name);
- }
复制代码 设置断点,第一次正常,第二次提示sqlparameter重复,求解
|
-
1.jpg
(63.23 KB, 下载次数: 31)
|