我試圖噸的可能辦法,但都無濟於事,現在我已經「OledbException是未處理」試圖捕獲和拋出異常。請指教謝謝!C#OledbException被處理錯誤
(假設新的數據輸入都是字符串不是整數。)
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//connect to database
string strOleDbConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Project.mdb";
OleDbConnection objConnection = new OleDbConnection(strOleDbConnectionString);
objConnection.Open();
string newTagID = textBox1.Text;
string newUser = textBox2.Text;
string newAge = textBox3.Text;
string newPhoneNumber = textBox4.Text;
string InsertNewRecord = "INSERT INTO jiahe ([Tag ID], [User], [Age], [Phone Number]) VALUES ('" + newTagID + "', '" + newUser + "', '" + newAge + "', '" + newPhoneNumber + "')";
OleDbCommand InsertCommand = new OleDbCommand(InsertNewRecord, objConnection);
try
{
InsertCommand.Parameters.Add("@[Tag ID]", newTagID);
InsertCommand.Parameters.Add("@[User]", newUser);
InsertCommand.Parameters.Add("@[Age]", newAge);
InsertCommand.Parameters.Add("@[Phone Number]", newPhoneNumber);
InsertCommand.ExecuteNonQuery();
MessageBox.Show("Record Added!");
}
catch (OleDbException ex)
{
throw ex;
}
finally
{
objConnection.Close();
}
}
}
}
什麼行會拋出錯誤? – Mark
catch(OleDbException ex) throw ex; } –
如何當我嘗試輸入整數一切工作正常?我認爲它應該是一個字符串,當我包裝用戶,年齡等與''? –