Использую код, схожий на:
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 col1=@col1,col2=@col2,col3=@col3 WHERE id=@id;";
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));
}
}
}
По коду функция GetDBConnectionString выглядит примерно так:
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);
}
P.S. Смотрю у Вас есть еще один
схожий вопрос