【问题标题】:update command not working in asp.net c# [closed]更新命令在asp.net c#中不起作用[关闭]
【发布时间】:2013-06-03 14:04:08
【问题描述】:
 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 进行了参数化,但它仍然无法正常工作

【问题讨论】:

  • 任何错误或异常?
  • 你也应该参数化cmpny
  • 不工作?你有什么异常吗?
  • 描述更多你所面临的错误......我们无法在没有解释的情况下帮助你
  • 您正在连接到主数据库?你确定这就是你想要的吗?

标签: c# asp.net sqlcommand


【解决方案1】:

我假设cmpny 是一个文本字段,所以你需要用撇号把它括起来:

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

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

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

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

【讨论】:

  • 这个也不行
  • @GiteshSawariya:你有更多细节吗?你在mycon.Open(); 上遇到异常吗?使用调试器。
  • 不,我没有遇到任何异常。
【解决方案2】:
  1. 去掉不必要的 SqlDataAdapter。

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

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

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

 

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();
    }
}

【讨论】:

  • 这个也不行..
  • 我都试过了。我没有收到任何异常或错误,但它没有更新
  • 你检查过ExecuteNonQuery的返回吗?
【解决方案3】:

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

因此您可能会遇到一些错误,因此您的更新命令不起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多