C# Form OleDb Excel 文本框和组合框中数据的更新命令
C# Form OleDb Excel Update command for data in textbox and combobox
我有一个带有文本框和组合框的表单。我正在使用 OleDb insert into command
将这两个框中的数据插入 excel
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\Desktop\Excel\Book1.xlsx;
Extended Properties='Excel 12.0 Xml; HDR = YES'";
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string error = comboBox1.SelectedItem.ToString();
command.CommandText = "insert into [Sheet1$] (NAME, MARKS) values('" +
textBox1.Text + "' , '" + error + "') ";
command.ExecuteNonQuery();
MessageBox.Show("data saved");
connection.Close();
现在我需要更新命令来更新 excel 中的任何以前的数据。
您的更新查询将如下所示。我正在为用户提供的值使用参数,您也应该在 INSERT:
中这样做
OleDbCommand command = new OleDbCommand();
command.CommandText = "update [Sheet1$] set MARKS = @Marks where NAME = @Name";
command.Paramerers.Add(new OleDbParameter("@Marks", error));
command.Paramerers.Add(new OleDbParameter("@Name", textBox1.Text));
我有一个带有文本框和组合框的表单。我正在使用 OleDb insert into command
将这两个框中的数据插入 excelOleDbConnection connection = new OleDbConnection();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\Desktop\Excel\Book1.xlsx;
Extended Properties='Excel 12.0 Xml; HDR = YES'";
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string error = comboBox1.SelectedItem.ToString();
command.CommandText = "insert into [Sheet1$] (NAME, MARKS) values('" +
textBox1.Text + "' , '" + error + "') ";
command.ExecuteNonQuery();
MessageBox.Show("data saved");
connection.Close();
现在我需要更新命令来更新 excel 中的任何以前的数据。
您的更新查询将如下所示。我正在为用户提供的值使用参数,您也应该在 INSERT:
中这样做OleDbCommand command = new OleDbCommand();
command.CommandText = "update [Sheet1$] set MARKS = @Marks where NAME = @Name";
command.Paramerers.Add(new OleDbParameter("@Marks", error));
command.Paramerers.Add(new OleDbParameter("@Name", textBox1.Text));