1

我正在为一个学校项目开发一个应用程序,但在将数据添加到 sql 数据库时遇到了麻烦。计划是用户将单击一个按钮来“添加”数据,这将打开一个带有各种文本框的新表单,用于输入数据。满意后,用户单击“添加详细信息”,然后将这些信息添加到 sql 数据库中。问题是 try catch 不断返回错误,我不知道为什么,对于有新眼光的人来说,这可能是一个简单的解决方法。代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace CarsULike
{
public partial class AddCust : Form
{
    public AddCust()
    {
        InitializeComponent();
    }

    private void AddCust_Load(object sender, EventArgs e)
    {

    }

    private void AddCustBtn_Click(object sender, EventArgs e)
    {
        if (FirstNameTxtBox.Text == "")
        {
            MessageBox.Show("Please enter a First Name");
        }
        else if (SurnameTxtBox.Text == "")
        {
            MessageBox.Show("Please enter a Surname");
        }
        else if (TitleTxtBox.Text == "")
        {
            MessageBox.Show("Please enter a Title");
        }
        else if (Address1TxtBox.Text == "")
        {
            MessageBox.Show("Please enter an address");
        }
        else if (TownCityTxtBox.Text == "")
        {
            MessageBox.Show("Please enter a Town/City");
        }
        else if (PostCodeTxtBox.Text == "")
        {
            MessageBox.Show("Please enter a Postcode");
        }
        else
        {
            //stores the values entered as variables
            string firstName = FirstNameTxtBox.Text;
            string surname = SurnameTxtBox.Text;
            string title = TitleTxtBox.Text;
            string address1 = Address1TxtBox.Text;
            string address2 = Address2TxtBox.Text;
            string townCity = TownCityTxtBox.Text;
            string postCode = PostCodeTxtBox.Text;
            string mobPhone = MobPhoneTxtBox.Text;
            string homePhone = HomePhoneTxtBox.Text;
        }

        //establishes a connection with the database
        SqlConnection connection = new SqlConnection(@"Data Source=Lee-PC\SQLEXPRESS; Initial Catalog=CarsULike_P116274;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();
        //SqlDataReader datareader;
        string sql;

        try
        {
            // this query is for insertion into the Customer table
            sql = "INSERT INTO Customer (FirstName, Surname, Title, AddressLine1, AddressLine2, TownCity, PostCode, EmailAddress, MobilePhoneNo, HomePhoneNo)";
            sql += String.Format("VALUES, @FirstName, @Surname, @Title, @AddressLine1, @AddressLine2, @TownCity, @PostCode, @EmailAddress, @MobilePhoneNo, @HomePhoneNo)");

            cmd = new SqlCommand(sql, connection);
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;

            cmd.Parameters.AddWithValue("@FirstName", FirstNameTxtBox.Text);
            cmd.Parameters.AddWithValue("@Surname", SurnameTxtBox.Text);
            cmd.Parameters.AddWithValue("@Title", TitleTxtBox.Text);
            cmd.Parameters.AddWithValue("@AddressLine1", Address1TxtBox.Text);
            cmd.Parameters.AddWithValue("@AddressLine2", Address2TxtBox.Text);
            cmd.Parameters.AddWithValue("@TownCity", TownCityTxtBox.Text);
            cmd.Parameters.AddWithValue("@PostCode", PostCodeTxtBox.Text);
            cmd.Parameters.AddWithValue("@EmailAddress", MobPhoneTxtBox.Text);
            cmd.Parameters.AddWithValue("@MobilePhoneNo", HomePhoneTxtBox.Text);
            cmd.Parameters.AddWithValue("@HomePhoneNo", HomePhoneTxtBox.Text);

            connection.Open(); //opens connection
            cmd.ExecuteNonQuery(); //writes to the database
            MessageBox.Show("Details Added", "Successful");
        }

        catch (SqlException ex)
        {
            throw new Exception("Error adding details", ex);

        }
        finally
        {
            connection.Close(); // Close connection
        }
    }

    private void CancelAddCustBtn_Click(object sender, EventArgs e)
    {
        this.Close();   // Close current form
    }
}

我环顾四周,但似乎找不到我要找的东西..或者我只是不知道我在找什么!

4

2 回答 2

3

改变

"VALUES, @FirstName, @Surname, @Title, @AddressLine1, @AddressLine2, @TownCity, @PostCode, @EmailAddress, @MobilePhoneNo, @HomePhoneNo)"

"VALUES ( @FirstName, @Surname, @Title, @AddressLine1, @AddressLine2, @TownCity, @PostCode, @EmailAddress, @MobilePhoneNo, @HomePhoneNo)"

将逗号更改为 open-paren。

可能还有其他错误,但这绝对是一个。

此外,您可能需要调整您的 catch 语句以确保您看到的是准确的 sql 错误。通常,SQL 异常(或内部异常)会给您一个很好的线索,让您了解 SQL 代码有什么问题,或者连接字符串是否有问题。

于 2013-02-12T00:07:49.403 回答
0

在这部分

"VALUES, @FirstName

您没有打开稍后关闭的括号,这就是错误。

于 2013-02-12T00:10:35.673 回答