我试图吨的可能办法,但都无济于事,现在我已经“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; } –
如何当我尝试输入整数一切工作正常?我认为它应该是一个字符串,当我包装用户,年龄等与''? –