简体   繁体   English

从C#代码更新Ms-Access表时出错

[英]Error In updating Ms-Access Table from C# Code

I am trying to update my Access table from a C# form. 我正在尝试从C#表单更新Access表。 When I run the program it runs without any message but when I click on Update it breaks and gives the following error message: 当我运行程序时,它运行时没有任何消息,但是当我单击“更新”时,它中断并给出以下错误消息:

Input string was not in a correct format. 输入的字符串格式不正确。

ACCESS TABLE FIELDS:- ID(string), Name(string), Score(int) 访问表字段:-ID(字符串),名称(字符串),Score(整数)

Error Message :- 错误信息 :-

 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;
}

Score is an integer. Score是整数。 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? 为何在字符串字段中将“ ID”转换为整数?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM