【问题标题】:ExecuteNonQuery() returns -1ExecuteNonQuery() 返回 -1
【发布时间】:2016-02-28 00:05:44
【问题描述】:

由于某种原因,result 始终是-1,并且没有任何内容添加到数据库中。我在 SQL Server 中执行了查询,它运行良好。我没有得到任何异常,也没有使用任何存储过程。

SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=RAINBOW;Integrated Security=True");
SqlCommand cmd;
cmd = new SqlCommand("INSERT INTO ItemDetails.item(description,category_id) VALUES (@item_desc,@cat_id)", con);
cmd.Parameters.AddWithValue("@item_desc", txtitemdesc.Text);
cmd.Parameters.AddWithValue("@cat_id", GetCategoryID());
try
{
     con.Open();
     int result = cmd.ExecuteNonQuery();
     if (result > 0)
     {
         MessageBox.Show("Record Inserted Successfully!");
     }
     else
     {
         MessageBox.Show("Failed to add record");
     }
}
catch (SqlException ex)
{
     MessageBox.Show("An error has occured! " + ex);
}
finally
{
     con.Close();
}

编辑

  int GetCategoryID()
        {
            int cat_id = 0;
            cmd = new SqlCommand("SELECT category_id FROM ItemDetails.category  WHERE category_desc=@cat_desc", con);
            con.Open();
            cmd.Parameters.AddWithValue("@cat_desc", cboCategory.Text);
            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                cat_id = int.Parse(reader["category_id"].ToString());
            }
            reader.Close();
            con.Close();
            return cat_id;
        }

【问题讨论】:

  • 如果它也可以工作(但来自 C#),您是否已经尝试过选择?控制连接字符串总是一件好事,如果它是正确的并且它是否按预期工作(一个没有参数的普通选择是该测试的一个不错的选择)。
  • 您是否还检查了 GetCategoryID() 和 txtitemdesc.Text 是否包含值?
  • 删除AddWithValue()并添加这样的参数 - cmd.Parameters.Add("@item_desc", SqlDbType.VarChar, 1000).Value = txtitemdesc.Text;
  • 这张桌子上有触发器吗?可能会被违反的约束或 FK?也许甚至其他列需要此表中的值,这可能会阻止插入值?
  • 如果您使用select 语句,这是正常行为,因为对于select 语句,ExecuteNonQuery 的返回值是-1,但由于您的问题出在Insert,因此您应该有如果您确定 GetCategoryID() 和 txtitemdesc.Text 包含值并且没有其他任何东西阻止插入这些值,则没问题。看看这个答案:stackoverflow.com/a/32736842/2946329

标签: c# sql-server executenonquery


【解决方案1】:

如果可能,请不要使用AddWithValue()。实际上,当您没有显式提供类型时,它会尝试隐式转换,有时隐式转换可能不是最佳转换。您可以找到更多讨论 in this link。

最重要的是不要忘记在分配之前清除参数,使用这一行。

cmd.Parameters.Clears();

检查下面的代码。

string sqlQuery = "INSERT INTO ItemDetails.item(description,category_id) VALUES (@item_desc,@cat_id)";

using (SqlCommand cmd = new SqlCommand(sqlQuery, con))
{
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.Clears(); // Add this same line in your getcategory function.
    cmd.Parameters.Add("@item_desc", SqlDbType.VarChar, 1000).Value = txtitemdesc.Text;
    cmd.Parameters.Add("@cat_id", SqlDbType.Int).Value = GetCategoryID();

    try
    {
        con.Open();
        int result = cmd.ExecuteNonQuery();
        if (result > 0)
        {
            MessageBox.Show("Record Inserted Successfully!");
        }
        else
        {
            MessageBox.Show("Failed to add record");
        }
    }
    catch (SqlException ex)
    {
        MessageBox.Show("An error has occured! " + ex);
    }
    finally
    {
        con.Close();
    }
}

【讨论】:

  • 我的代码的问题是GetCategoryID()。我将它编码为返回一个整数,但不知何故我无法让它与Add 或AddWithValue 一起使用你的代码和我如果我对 id 进行硬编码,就可以工作
  • @user2650277:您能否在您的问题中向我们展示您的GetCategoryID() 函数代码?
  • @user2650277: cboCategory 是组合框??如果是,那么它应该是cboCategory.SelectedValue
  • 错误发生在以下行reader = cmd.ExecuteReader();
  • @user2650277:将此行从cmd.Parameters.AddWithValue("@cat_desc", cboCategory.Text); 更改为cmd.Parameters.Add("@cat_desc", SqlDbType.varchar, 500).Value = cboCategory.Text; 这一行
猜你喜欢
  • 1970-01-01
  • 2011-12-28
  • 2011-12-09
  • 1970-01-01
  • 2017-01-20
  • 2013-02-07
  • 1970-01-01
相关资源
最近更新 更多