How to restrict to execute a single sql query using OleDbCommand

148 views Asked by At

I have tried to give multiple insert or update in single query like

OleDbCommand command = new OleDbCommand("INSERT INTO Employee (Id, Name, Address, Salary) VALUES (2001, 'name1', 'address1', 100000);UPDATE Employee SET Salary = 2000000 WHERE Id = 2001;");

This executes successfully.

But I don't want multiple sql queries in single execution. Is there anything like any property or something like that in C# which can be used to restrict to only one query.

I want to restrict it to single sql query in single execution.

Thanks.

1

There are 1 answers

0
Znaneswar On

Try something like this

Create a stringbuilder of values

StringBuilder sb = new StringBuilder();
string sep = "";
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
sb.AppendFormat("value1","value2" etc);
 }  
string insertthis = sb.ToString();

Then insert to sql

using (OleDbCommand cmd = new OleDbCommand("INSERT INTO Employee (ItemName) VALUES (@ins)", con))
  {
  cmd.Parameters.AddWithValue("@ins", insertthis );
  cmd.ExecuteNonQuery();
  }
MessageBox.Show("Data added");

Update also works like same as insert

Ref