【问题标题】:'ExecuteNonQuery: CommandText property has not been initialized' beginner here'ExecuteNonQuery:CommandText 属性尚未初始化'初学者在这里
【发布时间】:2019-01-23 02:38:27
【问题描述】:

我是 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();
}

【问题讨论】:

  • 你调试代码了吗?你检查过type_rdonly.Text 的值吗?
  • @ChetanRanpariya 嗨!它的 SL 和 VL。 SL 和 VL 来自 datagridview。我尝试使用 if (!string.IsNullOrEmpty(queryUpdate1)) { cmd1.CommandText = queryUpdate1; 来调试代码cmd1.ExecuteNonQuery(); } con.Close();但它没有更新 sa sql
  • 您是否检查了 ifEmployeeExist 方法返回的值?如果您在type_rdonly.Text 中有SLVL,那么ifEmployeeExist 将返回false,这会导致queryUpdate1 不会被初始化。尝试使用ifEmployeeExist 方法调试您的代码,您应该能够找出问题所在。

标签: c# sql error-handling command-text


【解决方案1】:

我认为 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();
    }

【讨论】:

  • 嗨!我尝试使用 if (!string.IsNullOrEmpty(queryUpdate1)) { cmd1.CommandText = queryUpdate1; 来调试它cmd1.ExecuteNonQuery(); } con.Close();因为你是对的,它可能有太多子句,但我不知道为什么 queryUpdate 1 和 2 是空的
  • 嗨!感谢您调整我的代码,但不幸的是,它没有用。它昨天还在工作,我今天早上检查了一下,然后就发生了这种情况。有没有 c# 突然不工作的事件?
猜你喜欢
  • 2012-02-23
  • 1970-01-01
  • 1970-01-01
  • 2019-12-02
  • 2021-06-21
  • 2017-03-03
  • 2012-05-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多