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)"

カンマを開き括弧に変更します。

他にもエラーがあるかもしれませんが、これは間違いなく 1 つです。

また、正確な sql エラーが表示されていることを確認するために、catch ステートメントを調整することをお勧めします。通常、SQL 例外 (または内部例外) は、SQL コードの何が問題なのか、または接続文字列に問題があるかどうかについて、かなり良い手がかりを与えてくれます。

于 2013-02-12T00:07:49.403 に答える
0

この部分では

"VALUES, @FirstName

後で閉じる括弧を開くのに失敗しています。これがエラーです。

于 2013-02-12T00:10:35.673 に答える