C# 用于文本框和组合框中数据的C Form OleDb Excel更新命令
我有一个带有文本框和组合框的表单。我使用OleDb insert into命令将这两个框中的数据插入excelC# 用于文本框和组合框中数据的C Form OleDb Excel更新命令,c#,excel,oledbcommand,C#,Excel,Oledbcommand,我有一个带有文本框和组合框的表单。我使用OleDb insert into命令将这两个框中的数据插入excel OleDbConnection connection = new OleDbConnection(); connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Desktop\Excel\Book1.xlsx; Extended Properties=
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中以前的任何数据。您的更新查询如下所示。我为用户提供的值使用参数,您在插入时也应该这样做:
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));
在SQL查询中使用用户输入时,应始终使用参数。您的方法目前对SQL注入非常开放。这可能只是一种优秀的表现,但你应该让正确的方法成为一种习惯