使用文本框在SQL Express中更新查询

时间:2014-01-08 07:10:34

标签: c# asp.net sql-server ado.net

protected void Button1_Click(object sender, EventArgs e)
{
     SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["project"].ConnectionString.ToString());
     conn.Open();
     string arun = "UPDATE [RECEIVE] SET [RDDF2]=@RDFF2 WHERE [OIESN]=@SRT";
     string SR2 = TextBox1.Text;
     int SR = int.Parse(TextBox1.Text);
     string SR1 = TextBox9.Text;

     SqlCommand cmd = new SqlCommand();
     cmd.Connection = conn;
     cmd.CommandText = arun;
     cmd.Parameters.AddWithValue("@SRT", SR);
     cmd.Parameters.AddWithValue("@RDFF2", SR1);
     cmd.ExecuteNonQuery();
     conn.Close();
}

它没有显示错误,但没有在数据库中更新。

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["project"].ConnectionString.ToString());

        string arun = "UPDATE [RECEIVE] SET [RDDF2]=@RDFF2 WHERE [OIESN]=@SRT";
        string SR2 = TextBox1.Text;
        int SR = int.Parse(TextBox1.Text);
        string SR1 = TextBox9.Text;

        using (SqlCommand SqlCmd = new SqlCommand(arun, conn))
        {
            conn.Open();
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@SRT", SqlDbType.Int).Value = SR;
            cmd.Parameters.Add("@RDFF2", SqlDbType.VarChar).Value = SR1;
            cmd.ExecuteNonQuery();
            conn.Close();
        }
    }