更新命令无法在asp.net中工作c#

时间:2013-06-03 14:04:08

标签: c# asp.net sqlcommand

 protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection mycon = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=master;Integrated Security=True");
        SqlDataAdapter myadp = new SqlDataAdapter();
        myadp.UpdateCommand = new SqlCommand("Update [orgs] Set [fname]=@fname,[weblnk]=@weblnk,[email]=@email,[cntct]=@cntct,[lctn]=@lctn,[cdscrptn]=@cdscrptn,[bsnstp]=@bsnstp WHERE [cmpny]=" +Label1.Text,mycon);
        myadp.UpdateCommand.Parameters.Add("@fname", SqlDbType.VarChar, 50).Value = TextBox1.Text;
        myadp.UpdateCommand.Parameters.Add("@weblnk", SqlDbType.VarChar,80).Value = TextBox3.Text;
        myadp.UpdateCommand.Parameters.Add("@email", SqlDbType.VarChar,80).Value = TextBox4.Text;
        myadp.UpdateCommand.Parameters.Add("@cntct", SqlDbType.VarChar,20).Value = TextBox5.Text;
        myadp.UpdateCommand.Parameters.Add("@lctn", SqlDbType.VarChar,80).Value = TextBox6.Text;
        myadp.UpdateCommand.Parameters.Add("@cdscrptn", SqlDbType.VarChar,600).Value = TextBox7.Text;
        myadp.UpdateCommand.Parameters.Add("@bsnstp", SqlDbType.VarChar,40).Value = TextBox8.Text;
        myadp.UpdateCommand.Connection = mycon;
        mycon.Open();
        myadp.UpdateCommand.ExecuteNonQuery();
        mycon.Close();
    }

protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection mycon = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=master;Integrated Security=True");
        SqlDataAdapter myadp = new SqlDataAdapter();
        myadp.UpdateCommand = new SqlCommand("Update [orgs] Set [fname]=@fname,[weblnk]=@weblnk,[email]=@email,[cntct]=@cntct,[lctn]=@lctn,[cdscrptn]=@cdscrptn,[bsnstp]=@bsnstp WHERE [cmpny]=@cmpny", mycon);
        myadp.UpdateCommand.Parameters.Add("@fname", SqlDbType.VarChar, 50).Value = TextBox1.Text;
        myadp.UpdateCommand.Parameters.Add("@cmpny", SqlDbType.VarChar, 50).Value = TextBox2.Text;
        myadp.UpdateCommand.Parameters.Add("@weblnk", SqlDbType.VarChar,80).Value = TextBox3.Text;
        myadp.UpdateCommand.Parameters.Add("@email", SqlDbType.VarChar,80).Value = TextBox4.Text;
        myadp.UpdateCommand.Parameters.Add("@cntct", SqlDbType.VarChar,20).Value = TextBox5.Text;
        myadp.UpdateCommand.Parameters.Add("@lctn", SqlDbType.VarChar,80).Value = TextBox6.Text;
        myadp.UpdateCommand.Parameters.Add("@cdscrptn", SqlDbType.VarChar,600).Value = TextBox7.Text;
        myadp.UpdateCommand.Parameters.Add("@bsnstp", SqlDbType.VarChar,40).Value = TextBox8.Text;
        myadp.UpdateCommand.Connection = mycon;
        mycon.Open();
        myadp.UpdateCommand.ExecuteNonQuery();
        mycon.Close();
    }

这里我也有参数化cmpny,但它仍无法正常工作

3 个答案:

答案 0 :(得分:4)

我假设cmpny是一个文本字段,因此您需要将其括在撇号中:

WHERE [cmpny]='" + Label1.Text + "'",mycon);

然而,请立即忘记这一点。您应该使用参数始终

WHERE [cmpny]=@cmpny", mycon);

myadp.UpdateCommand.Parameters.AddWithValue("@cmpny", TextBox1.Text);

答案 1 :(得分:1)

  1. 摆脱不必要的SqlDataAdapter。

  2. 在本地服务器上获取真正的数据库。主数据库不适用于您的数据。

  3. 检查ExecuteNonQuery的返回值。也许你假定的cmpny值不存在于表中?

  4. 将最少的异常处理附加到您的代码中。

  5. using (SqlConnection mycon = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=master;Integrated Security=True"))
    {
        mycon.Open();
        using (SqlCommand cmd = new SqlCommand("Update [orgs] Set [fname]=@fname,[weblnk]=@weblnk,[email]=@email,[cntct]=@cntct,[lctn]=@lctn,[cdscrptn]=@cdscrptn,[bsnstp]=@bsnstp WHERE [cmpny]=@cmpny", mycon))
        {
            cmd.Parameters.Add("@fname", SqlDbType.VarChar, 50).Value = TextBox1.Text;
            // all the other params
            cmd.Parameters.Add("@bsnstp", SqlDbType.VarChar, 40).Value = TextBox8.Text;
            cmd.Parameters.Add("@cmpny", /*correct Datatype here*/).Value = Label1.Text;  // from a Label ?? how does it got there? You should take the value from the actual source
            int affectedRecords = cmd.ExecuteNonQuery();
        }
    }
    

答案 2 :(得分:0)

Label1.Text也应该参数化,因为它可能包含一些引号。 一般来说,您应该在SQL中对每个用户的参数进行参数化,以避免SQL注入。

因此您可能会遇到一些错误,因此您的更新命令无效。