![](/img/trans.png)
[英]'SqlException was unhandled' error when trying to connect to SQL Server database
[英]Catch SqlException throwing error invalid syntax when trying to add data to sql database through button click event
我正在開發一個學校項目的應用程序,但是在向sql數據庫添加數據時遇到了麻煩。 計划是用戶將單擊一個按鈕以“添加”數據,這將打開一個新表單,其中包含各種文本框以輸入數據。 當用戶滿意時,單擊“添加詳細信息”,然后將這些添加到sql數據庫中。 問題是嘗試捕獲不斷返回錯誤,我不知道為什么,可能是一個容易修復的新眼睛的人。 代碼如下:
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
}
}
我已經環顧四周但似乎無法找到我正在尋找的東西..或者我只是不知道我在尋找什么!
更改
"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代碼錯誤或連接問題是否存在問題的非常好的線索。
在這一部分
"VALUES, @FirstName
您未能打開稍后關閉的括號,這就是錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.