Answers for "update sql database c# example"

SQL
1

sql server update c# example code

string name = "test1";
int id = 1;
using (SqlConnection cnn = new SqlConnection("Server=localhost;Database=DEV;User Id=test;Password=test!@#$;Connect Timeout=5400"))
{
  cnn.Open();
  string querystr = "UPDATE [dbo].[TABLE] SET [name]=@p_name WHERE [id]=@p_id";
  using (var cmd = new SqlCommand(querystr, cnn))
  {
     //set value  
     cmd.Parameters.AddWithValue("@p_name", name);
     //where with id
     cmd.Parameters.AddWithValue("@p_id", id);
     int ret = cmd.ExecuteNonQuery();
  }
}
Posted by: Guest on January-08-2021
0

how to update database using sql in c#

using (var sqlConnection = new SqlConnection(config.ConnectionString))
{
	foreach(var position in positions)
	{
		var sql =
         	$"UPDATE BillOfMaterial SET Position = '{pos}' WHERE Creator = '{position.Creator}' AND Idx = '{position.Idx}' AND ArtNumber = '{position.ArtNumber}'";
          using (var sqlCommand = new SqlCommand(sql,sqlConnection))
          {
               sqlConnection.Open();
               sqlCommand.ExecuteNonQuery();
               sqlConnection.Close();
          }
   	}
}
Posted by: Guest on April-08-2021

Code answers related to "SQL"

Browse Popular Code Answers by Language