0

我正在尝试从 C# 表单更新我的 Access 表。当我运行程序时,它运行时没有任何消息,但是当我单击更新时,它会中断并给出以下错误消息:

输入字符串的格式不正确。

访问表字段:- ID(string)、Name(string)、Score(int)

错误信息 :-

 public Form1()
 {
    InitializeComponent();
 }

 OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Database3.accdb");

 public void UpdateRecord()
 {
        int score = Convert.ToInt32(textBox2.Text);
        **string command = "update score set Name= '"+textBox1.Text+"' , Score= '"+score+"' where ID= '"+textBox3.Text+"'  ";**
    OleDbCommand cmd = new OleDbCommand(command,conn);
    conn.Open();

    cmd.ExecuteNonQuery();

public void showData()
{
   string command = "Select * from score";
   OleDbCommand cmd = new OleDbCommand(command, conn);

   OleDbDataAdapter da = new OleDbDataAdapter(cmd);
   DataTable Table = new DataTable();
   da.Fill(Table);
   dataGridView1.DataSource = Table;
}
4

1 回答 1

0

Score is an integer. You have quotes around it in your string, which is invalid. Try:

string id = textBox3.Text;
string command = "update score set Name= '"+textBox1.Text+"' , Score= "+textBox2.Text+" where ID= '"+id+"'  ";

I'd be concerned that you have no data validation in place. What if they don't enter a number in the text box? And why are you converting 'ID' to a integer when it's an string field?

于 2013-04-18T17:35:58.030 回答