在SQL Server中实现保存或更新数据库的按钮
代码示例:
protected void btnSave_Command(object sender, CommandEventArgs e)
{
string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;";
using(SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = CommandType.Text;
command.CommandText = "UPDATE myTable SET Column1=@Column1, Column2=@Column2 WHERE ID=@ID";
command.Parameters.AddWithValue("@Column1", txtColumn1.Text);
command.Parameters.AddWithValue("@Column2", txtColumn2.Text);
command.Parameters.AddWithValue("@ID", txtID.Text);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}