为什么我会变得不正确的语法接近“字母”?

发布于 2025-01-30 08:30:40 字数 1318 浏览 2 评论 0原文

我正在尝试将新车详细信息添加到系统中。我想添加到Carid的VARCHAR值中。在Cari​​d的数据库类型中是Varchar。

这是我的代码:

private void btnAddCar_Click(object sender, EventArgs e){
if (txtCarId.Text == "" || txtModel.Text == "" || txtColor.Text == "" || txtFuelType.Text == "" || txtPrice.Text == "")
            {
                MessageBox.Show("Missing Information");
            }
            else
            {
                try
                {
                    Con.Open();
                    string query = "insert into CAR(CarID,Model,Color,FuelType,Available,Price) values(" + txtCarId.Text + ",'" + txtModel.Text + "','" + txtColor.Text + "','"+txtFuelType.Text + "','"+cmbBoxAvailable.SelectedItem.ToString()+"',"+txtPrice.Text+")";
                    SqlCommand cmd = new SqlCommand(query, Con);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Car Successfully Added");
                    Con.Close();
                    populate();
                }
                catch (Exception myEx)
                {
                    MessageBox.Show(myEx.Message);
                }
            }
        }

但是当我将值输入到Carid Textbox时, 发生例外:“无效列名”,不正确的语法靠近“字母” enter image Description

I'm trying to ADD new car details to the system. I want to add to the varchar values for the CarId. In the database type of the carId is varchar.

This is my code:

private void btnAddCar_Click(object sender, EventArgs e){
if (txtCarId.Text == "" || txtModel.Text == "" || txtColor.Text == "" || txtFuelType.Text == "" || txtPrice.Text == "")
            {
                MessageBox.Show("Missing Information");
            }
            else
            {
                try
                {
                    Con.Open();
                    string query = "insert into CAR(CarID,Model,Color,FuelType,Available,Price) values(" + txtCarId.Text + ",'" + txtModel.Text + "','" + txtColor.Text + "','"+txtFuelType.Text + "','"+cmbBoxAvailable.SelectedItem.ToString()+"',"+txtPrice.Text+")";
                    SqlCommand cmd = new SqlCommand(query, Con);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Car Successfully Added");
                    Con.Close();
                    populate();
                }
                catch (Exception myEx)
                {
                    MessageBox.Show(myEx.Message);
                }
            }
        }

But when I input values to the CarId textBox,
an exception occurs: "Invalid Column name" , Incorrect Syntax near"Letter"enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时间海 2025-02-06 08:30:40

我将尝试修复您的代码,并希望它能解决您的问题,或者至少使它更清楚地出了问题。这是新的代码:

private void btnAddCar_Click(object sender, EventArgs e)
{
    if (txtCarId.Text == "" || txtModel.Text == "" || txtColor.Text == "" || txtFuelType.Text == "" || txtPrice.Text == "")
    {
        MessageBox.Show("Missing Information");
    }
    else
    {
        try
        {
            using (var con = new SqlConnection(<your connectionstring goes here>)
            {
                con.Open();
                string query = "INSERT INTO CAR(CarID,Model,Color,FuelType,Available,Price) VALUES(@CarID,@Model,@Color,@FuelType,@Available,@Price)";
                using (var cmd = new SqlCommand(query, con))
                {
                    cmd.Parameters.Add("@CarID").Value = txtModel.Text;
                    cmd.Parameters.Add("@Model").Value = txtModel.Text;
                    cmd.Parameters.Add("@Color").Value = txtColor.Text;
                    cmd.Parameters.Add("@FuelType").Value = txtFuelType.Text;
                    cmd.Parameters.Add("@Available").Value = cmbBoxAvailable.SelectedItem.ToString();
                    cmd.Parameters.Add("@Price").Value = txtPrice.Text;
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Car Successfully Added");
                }
                populate();
            }
        }
        catch (Exception myEx)
        {
            MessageBox.Show(myEx.Message);
        }
    }
}

因此,SQLConnection现在是本地的,并以使用模式包裹,因此它将自动关闭并自动处置。 SQLCommand也是如此。

查询使用参数,因此可以防止SQL注入,并且您不必考虑数据库类型。这不是真的,因为我认为所有数据库字段都是字符串,所以这极不可能,但是您尚未指定。不是字符串的字段,您必须在设置参数值之前转换为正确的类型。

在现实生活中,您会将所有数据库内容放在数据层中,而仅在此处处理用户界面,但是我将其保留给自己。

另外,您不应该捕获异常,但是可能发生的异常子类型。

I'm going to make an attempt at fixing your code and hope it solves your issue or at least makes it clearer what is going wrong. Here is the new code:

private void btnAddCar_Click(object sender, EventArgs e)
{
    if (txtCarId.Text == "" || txtModel.Text == "" || txtColor.Text == "" || txtFuelType.Text == "" || txtPrice.Text == "")
    {
        MessageBox.Show("Missing Information");
    }
    else
    {
        try
        {
            using (var con = new SqlConnection(<your connectionstring goes here>)
            {
                con.Open();
                string query = "INSERT INTO CAR(CarID,Model,Color,FuelType,Available,Price) VALUES(@CarID,@Model,@Color,@FuelType,@Available,@Price)";
                using (var cmd = new SqlCommand(query, con))
                {
                    cmd.Parameters.Add("@CarID").Value = txtModel.Text;
                    cmd.Parameters.Add("@Model").Value = txtModel.Text;
                    cmd.Parameters.Add("@Color").Value = txtColor.Text;
                    cmd.Parameters.Add("@FuelType").Value = txtFuelType.Text;
                    cmd.Parameters.Add("@Available").Value = cmbBoxAvailable.SelectedItem.ToString();
                    cmd.Parameters.Add("@Price").Value = txtPrice.Text;
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Car Successfully Added");
                }
                populate();
            }
        }
        catch (Exception myEx)
        {
            MessageBox.Show(myEx.Message);
        }
    }
}

So, the SqlConnection is now local and wrapped in a using pattern, so it will be closed and disposed automatically. The same goes for the SqlCommand.

The query is using parameters, so that Sql Injection is prevented and you don't have to think about the database types. That's not quite true, since I assume all database fields are strings, that's highly unlikely, but you haven't specified otherwise. The fields that are not strings, you will have to convert to the right type before setting the value of the parameter.

In real life you would put all the database stuff in a data layer and only deal with the user interface here, but I left that for yourself to sort out.

Also you shouldn't catch Exception, but the Exception subtypes that are likely to occur.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文