How to write a query in SQL server CE to check data before updating?

62 views Asked by At

I'm trying to write a query that can compare each row of a DGV with SQL Server CE and if there were any row that its first and third column is equal to one of SQL rows, do nothing and if there were no duplicate value, then add that complete row to the SQL SERVER CE file. I tried the following code but it throws runtime error:

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();
}
0

There are 0 answers