本文介绍了进入数据库时​​出现空值异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在基于服务的数据库中创建了一个表来存储销售信息.该表包括日期、时间、名称、采购、数量、成本(以这种特定方式)列.插入表格的代码是

i have created a table in a service based database to store the sales information.the table includes the columns date,time,name,purchase,quantity,cost(in this specific manner). the code for inserting into the table is

SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\rnawa_000\Documents\Visual Studio 2013\Projects\Random\Random\sales.mdf;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
cmd.CommandText = "insert into salesTB (Date,Time,Name,Quantity,Cost) values ('"+date.Text+"','"+time.Text+"','"+txtName.Text+"','"+listBox1.Items.Count+"','"+ txtCost.Text+")";

foreach (string item in listBox1.Items)
{
    cmd.CommandText = "insert into salesTB (Purchase) values ('" +item.Substring(0,10)  + "')";
}
cmd.ExecuteNonQuery();
cmd.Clone();
conn.Close();

date.texttime.text 标签分别显示日期和时间.它们在表单加载事件中被赋值.txtName.texttxtCost.text 标签使用构造函数从以前的表单获取它们的值

The date.text and time.text labels displays the date and time respectively.they are assigned their values in the form load event.the txtName.text and txtCost.text labels get their values from previous form using constructor as

public Form2(ListBox.ObjectCollection objectCollection, string name,string total)
{
    InitializeComponent();
    this.listBox1.Items.AddRange(objectCollection);
    txtName.Text = name;
    txtCost.Text = total;
}

当我执行此操作时,它给我一个错误提示无法将值 NULL 插入到列 'Cost' 中".如果我在数据库的成本列中设置允许为空,那么它会在异常错误中显示任何其他列名.

When I am executing this, it's giving me an error saying "Cannot insert the value NULL into column 'Cost'". And if I set allow nulls in the cost column in database then it show any other column name in the exception error.

推荐答案

这里有一种更好、更安全的方式来完成您的任务:

Here is a nicer and safer way to do what your after:

      string SQLQuery = "INSERT INTO salesTB (Date,Time,Name,Quantity,Cost)" +
                            "VALUES (@date, @time, @name, @quantity, @cost)";

        using (SqlConnection DBConn = new SqlConnection(cs.ToString()))
        {
            using (SqlCommand sqlCmd = new SqlCommand(SQLQuery, DBConn))
            {
                sqlCmd.Parameters.Add("@date", SqlDbType.Text);
                sqlCmd.Parameters["@date"].Value = date.Text;

                sqlCmd.Parameters.Add("@time", SqlDbType.Text);
                sqlCmd.Parameters["@time"].Value = time.Text;

                sqlCmd.Parameters.Add("@name", SqlDbType.Text);
                sqlCmd.Parameters["@name"].Value = txtName.Text;

                sqlCmd.Parameters.Add("@quantity", SqlDbType.Int);
                sqlCmd.Parameters["@quantity"].Value = listBox1.Items.Count;

                sqlCmd.Parameters.Add("@cost", SqlDbType.Text);
                sqlCmd.Parameters["@cost"].Value = txtCost.Text

                DBConn.Open();
                sqlCmd.ExecuteNonQuery();
                DBConn.Close();
            }
        }

这篇关于进入数据库时​​出现空值异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 01:27