Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用于文本框和组合框中数据的C Form OleDb Excel更新命令_C#_Excel_Oledbcommand - Fatal编程技术网

C# 用于文本框和组合框中数据的C Form OleDb Excel更新命令

C# 用于文本框和组合框中数据的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=

我有一个带有文本框和组合框的表单。我使用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='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注入非常开放。这可能只是一种优秀的表现,但你应该让正确的方法成为一种习惯