我正在尝试编写一个查询,可以将 DGV 的每一行与 SQL Server CE 进行比较,如果有任何行其第一列和第三列等于 SQL 行之一,则什么也不做,如果没有重复值,然后将该完整行添加到 SQL SERVER CE 文件中。我尝试了以下代码,但它引发了运行时错误:
SqlCeConnection connection2 = new SqlCeConnection();
connection2.ConnectionString = @"DataSource = WO_No.sdf; password= rss900";
SqlCeCommand mycommand = new SqlCeCommand();
mycommand.Connection = connection2;
for (int i = 0; i < DataGridView3.Rows.Count; i++)
{
connection2.Open();
mycommand.CommandText = $"IF EXISTS (SELECT * FROM [MyData] WHERE Wo={DataGridView3.Rows[i].Cells[0].Value} AND Code={DataGridView3.Rows[i].Cells[2].Value}" +
$"BEGIN" +
$"" +
$"END" +
$"ELSE" +
$"BEGIN" +
$"INSERT INTO [MyData] Wo={DataGridView3.Rows[i].Cells[0].Value}, EqN={DataGridView3.Rows[i].Cells[1].Value}, Code={DataGridView3.Rows[i].Cells[2].Value}, Work={DataGridView3.Rows[i].Cells[3].Value}, Cost={DataGridView3.Rows[i].Cells[4].Value}" +
$"END";
mycommand.ExecuteNonQuery();
connection2.Close();
}