xxxxxxxxxx
string querystr = "UPDATE Users SET User_FirstName=@User_FirstName, User_LastName=@User_LastName WHERE User_ID=@User_ID";
xxxxxxxxxx
using System.Data.SqlClient;
// Define the connection string
string connectionString = "Data Source=MyServer;Initial Catalog=MyDatabase;Integrated Security=True";
// Define the UPDATE statement with parameters
string query = "UPDATE MyTable SET Column1=@Value1, Column2=@Value2 WHERE ID=@ID";
// Create a new SqlConnection using the connection string
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Open the connection
connection.Open();
// Create a new SqlCommand using the query and the SqlConnection
using (SqlCommand command = new SqlCommand(query, connection))
{
// Add parameters to the SqlCommand
command.Parameters.AddWithValue("@Value1", "New Value 1");
command.Parameters.AddWithValue("@Value2", "New Value 2");
command.Parameters.AddWithValue("@ID", 1);
// Execute the UPDATE statement
int rowsAffected = command.ExecuteNonQuery();
// Output the number of rows affected
Console.WriteLine($"{rowsAffected} rows affected.");
}
}
xxxxxxxxxx
command.CommandText = "UPDATE Student
SET Address = @add, City = @cit Where FirstName = @fn and LastName = @add";