c# - 'ExecuteNonQuery:CommandText 属性尚未初始化'初学者在这里
问题描述
我是 sql 和 c# 的新手,我在 executenonquery commandtex 中有错误。我不知道错误在哪里。你们能帮我吗?
private void submitBtn_Click(object sender, EventArgs e)
{
con.Open();
string a = "Accept";
string b = "Reject";
string queryUpdate1 = "";
string queryUpdate2 = "";
int row = DGVLeaves.CurrentCell.RowIndex;
if (accptBtn.Checked)
{
if (type_rdonly.Text == "SL")
{
if (ifEmployeeExist(con, emptime_rdonly.Text))
{
queryUpdate1 = @"UPDATE [LEAVE_EMP] SET EMP_STATUS ='" + a + "'WHERE [EMP_TIME] ='" + emptime_rdonly.Text + "'";
}
queryUpdate2 = "UPDATE LEAVE_ADMIN SET L_SPENT_SL = (L_SPENT_SL + 1), L_REM_SL = (L_REM_SL - 1)";
}
if (type_rdonly.Text == "VL")
{
if (ifEmployeeExist(con, emptime_rdonly.Text))
{
queryUpdate1 = @"UPDATE [LEAVE_EMP] SET EMP_STATUS ='" + a + "'WHERE [EMP_TIME] ='" + emptime_rdonly.Text + "'";
}
queryUpdate2 = "UPDATE LEAVE_ADMIN SET L_SPENT_VL = (L_SPENT_VL + 1),L_REM_VL = (L_REM_VL - 1)";
}
SqlCommand cmd1 = new SqlCommand(queryUpdate1, con);
SqlCommand cmd2 = new SqlCommand(queryUpdate2, con);
cmd2.ExecuteNonQuery();
cmd1.ExecuteNonQuery();
}
if (rejBtn.Checked)
{
if (ifEmployeeExist(con, emptime_rdonly.Text))
{
queryUpdate1 = @"UPDATE [LEAVE_EMP] SET EMP_STATUS ='" + b + "'WHERE [EMP_TIME] ='" + emptime_rdonly.Text + "'";
}
SqlCommand cmd1 = new SqlCommand(queryUpdate1, con);
cmd1.ExecuteNonQuery();
}
con.Close();
}
解决方案
我认为 if 子句太多,您可能会在此过程中遗漏一些东西。在执行 ExecuteNonQuery 命令之前尝试调试并查看变量 queryUpdate1 或 queryUpdate2 是否为空。如果它是空的,那应该是原因
我已经为您调整了代码,希望这对您有所帮助
private void submitBtn_Click(object sender, EventArgs e)
{
string a = "Accept";
string b = "Reject";
string queryUpdate1 = "";
string queryUpdate2 = "";
int row = DGVLeaves.CurrentCell.RowIndex;
if (accptBtn.Checked)
{
if (type_rdonly.Text == "SL")
{
if (ifEmployeeExist(con, emptime_rdonly.Text))
{
queryUpdate1 = @"UPDATE [LEAVE_EMP] SET EMP_STATUS ='" + a + "'WHERE [EMP_TIME] ='" + emptime_rdonly.Text + "'";
}
queryUpdate2 = "UPDATE LEAVE_ADMIN SET L_SPENT_SL = (L_SPENT_SL + 1), L_REM_SL = (L_REM_SL - 1)";
}
if (type_rdonly.Text == "VL")
{
if (ifEmployeeExist(con, emptime_rdonly.Text))
{
queryUpdate1 = @"UPDATE [LEAVE_EMP] SET EMP_STATUS ='" + a + "'WHERE [EMP_TIME] ='" + emptime_rdonly.Text + "'";
}
queryUpdate2 = "UPDATE LEAVE_ADMIN SET L_SPENT_VL = (L_SPENT_VL + 1),L_REM_VL = (L_REM_VL - 1)";
}
}
else if (rejBtn.Checked)
{
if (ifEmployeeExist(con, emptime_rdonly.Text))
{
queryUpdate1 = @"UPDATE [LEAVE_EMP] SET EMP_STATUS ='" + b + "'WHERE [EMP_TIME] ='" + emptime_rdonly.Text + "'";
}
}
con.Open();
SqlCommand cmd = new SqlCommand() { Connection = con, CommandType = System.Data.CommandType.Text };
if (!string.IsNullOrEmpty(queryUpdate1)) {
cmd.CommandText = queryUpdate1;
cmd.ExecuteNonQuery();
}
if (!string.IsNullOrEmpty(queryUpdate2))
{
cmd.CommandText = queryUpdate2;
cmd.ExecuteNonQuery();
}
if (string.IsNullOrEmpty(queryUpdate1) && string.IsNullOrEmpty(queryUpdate2))
{
MessageBox.Show("Empty query");
}
con.Close();
}
推荐阅读
- javascript - 在 nextjs 中未正确返回更新的对象
- ip - 具有多个 IP 的模拟虚拟需求环境
- r - 如何生成字符串列表的所有排列?
- database - laravel 中的电子商务过滤器查询/逻辑
- c# - 握住手机时陀螺仪控制的相机抖动
- mongodb - 保存到 mongodb:py4j.Py4JException:Python 代理引发异常
- sql - 每月提供利润的代理的 SQL 计数
- css - 从我使用 create-react-app 的 react 项目中删除 node-sass
- amazon-web-services - 模板的输出块中未解决的资源依赖项 [BasicAWSApiGateway]
- python - Python:如何使用多个参数对我的函数进行多处理?