我开发了一个简单的Windows应用程序。为了重用代码,我为数据库管理创建了一个单独的类文件,命名为DM。
一切正常,但我收到的错误仍未解决。
该错误类似于链接The SqlParameter is already contained by another SqlParameterCollection - Does using() {} cheat?
但解决方案对我不起作用。
我的代码:
SqlParameter[] SQL_Params =
{
new SqlParameter("@app_srno", textBox1.Text.Trim()),
new SqlParameter("@resetBy", username_Form)
};
queryString = "insert into Record_Reset (app_srno, resetBy) values(@app_srno, @resetBy)";
DM.execute_query(queryString, SQL_Params);
// sqlParams = null;
queryString = "update [KYC_Index] set [transform_int] = 'N',[transform_int_by] = null,[transform_pic] = '',[transform_poi] = '',[transform_poa] = '',[transform_by]=null,[qc_int]='N',[qc_int_by]=null,[qc]='N',[qc_by]=null where [srno] = @app_srno";
DM.execute_query(queryString, SQL_Params); // error happens here
我的班级文件DM
中的代码public void execute_query(string query, SqlParameter[] sqlparams = null)
{
using (SqlConnection connection = new SqlConnection(connectString))
{
using (SqlCommand sqlcmd = new SqlCommand(query))
{
sqlcmd.Connection = connection;
if (sqlparams == null)
{
sqlcmd.CommandType = CommandType.Text;
}
else
{
sqlcmd.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter p in sqlparams)
{
sqlcmd.Parameters.Add(p);
}
}
try
{
connection.Open();
sqlcmd.ExecuteNonQuery();
sqlparams = null;
sqlcmd.Parameters.Clear();
connection.Close();
}
catch (Exception)
{
//MessageBox.Show(ex.Message);
}
}
}
}