P
P
prog3232014-04-19 03:31:47
C++ / C#
prog323, 2014-04-19 03:31:47

How to do mssql update correctly in c#?

Do update records on msdn.microsoft.com/ru-ru/library/ca56w9se(v=vs.90).aspx
I get
Error (1205): The transaction caused a resource deadlock blocking | buffer communication with another process and became the victim of a deadlock. Restart the transaction.
How to do mssql update correctly in c#?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
morello, 2014-05-28
@morello

I am using code similar to:

public void DBTableUpdate(int id, int col1, int col2, int col3)
{
  string connectionString = GetDBConnectionString();

  using (SqlConnection connection = new SqlConnection(connectionString))
  {
    SqlCommand command = new SqlCommand();
    command.Connection = connection;

    command.Parameters.AddWithValue("@id", id);
    command.Parameters.AddWithValue("@col1", col1);
    command.Parameters.AddWithValue("@col1", col2);
    command.Parameters.AddWithValue("@col1", col3);
    
    command.CommandType = System.Data.CommandType.Text;
    string commandText = "UPDATE dbo.tablename SET [email protected],[email protected],[email protected] WHERE [email protected];";
    
    try
    {
      Console.WriteLine(String.Format("Executing SQL: {0}", command.CommandText));

      connection.Open();
      Int32 rowsAffected = command.ExecuteNonQuery();

      Console.WriteLine(String.Format("RowsAffected: {0}", rowsAffected));
    }
    catch (SqlException ex)
    {
      Console.WriteLine(String.Format("SqlException: [{0}] {1}", ex.ErrorCode, ex.Message));
    }
    catch (InvalidOperationException ex)
    {
      Console.WriteLine(String.Format("InvalidOperationException: {1}", ex.Message));
    }
  }
}

In code, the GetDBConnectionString function looks something like this:
private string GetDBConnectionString(string Server, string Database, string Username, string Password)
{
  return string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}", Server, Database, Username, Password);
}

PS I see you have another similar question

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question