ExecuteStoreCommand SqlParameterCollection 中已包含 SqlParameter
今天很奇怪调用EF的ExecuteStoreCommand 出现了个这样的错误,怎么也调试不过,痛定思痛
原来 command被连着调用了而没有销毁掉
public static DataTable GetDataTable(
string connDBStr, string sql, params SqlParameter[] cmdParms)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(connDBStr))
{
PrepareSqlCommand(cmd, conn, null, sql, cmdParms);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable (SetSqlAsDataTableName(sql));
da.Fill(dt);
cmd.Parameters.Clear();//多了这一句,就解决了问题
return dt;
}
string connDBStr, string sql, params SqlParameter[] cmdParms)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(connDBStr))
{
PrepareSqlCommand(cmd, conn, null, sql, cmdParms);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable (SetSqlAsDataTableName(sql));
da.Fill(dt);
cmd.Parameters.Clear();//多了这一句,就解决了问题
return dt;
}
}
还要说说EF调用 ExecuteStoreCommand 的用法
string orderCode =“test1”;
string dishMenuCode=“test2”
var parameter = new SqlParameter[]{
new SqlParameter("@p_order",orderCode),
new SqlParameter("@p_dish",dishMenuCode),
};//多个参数的列子
var paramenter2 = new SqlParameter { ParameterName = "@p_user", Value = "gxw" };//一个参数的例子
ef.ExecuteStoreCommand("select * From UserTable where username=@p_user", paramenter2);