我在代码后面有这个:当我在数据库中插入一行时,它是正确的,但是当我刷新页面时,最后插入的行被一次又一次地插入。
protected void ButtonExecute_Click(object sender, EventArgs e)
{
string connectionString =cs.getConnection();
string insertSql = "INSERT INTO profitCategories(name, IdUser) VALUES(@name, @UserId)";
using (SqlConnection myConnection = new SqlConnection(connectionString))
{
myConnection.Open();
SqlCommand command = new SqlCommand(insertSql, myConnection);
command.Parameters.AddWithValue("@name", TextBoxCategory.Text);
command.Parameters.AddWithValue("@UserId", cui.getCurrentId());
command.ExecuteNonQuery();
myConnection.Close();
}
TextBoxCategory.Text = string.Empty;
}
这解决了我的问题:为每个页面刷新插入新行