Update语句和ExecuteNonQuery不返回C#,MySQL

时间:2015-06-22 04:17:47

标签: c# mysql

在我的C#,asp.net应用程序中,我将字段值插入到MySQl中,然后在编辑后更新fiels值。我使用下面的代码。它被执行但数据库中没有添加值。我也用过

  

numRowsUpdated = cmd.ExecuteNonQuery();

但是在调试器窗口中,它显示为

  

当前上下文中不存在名称'numRowsUpdated'

我的代码:

 protected void UpdateBtn_Click(object sender, EventArgs e)
        {
            int numRowsUpdated = -1;
            string Query = "UPDATE employee SET  EmployeeName=@a,EmpID =@b,Designation =@c,JoiningDate=@d,BankName =@e,AccountNumber = @f,Pancard =@g,PFNumber=@h WHERE id = @i" ;
            if (GlobalCS.OpenConnection() == true)
            {                    
                MySqlCommand cmd = new MySqlCommand(Query, GlobalCS.objMyCon);
                cmd.Parameters.AddWithValue("i", selectedid);
                cmd.Parameters.AddWithValue("a", NameTxt.Text);
                cmd.Parameters.AddWithValue("b", EmpidTxt.Text);
                cmd.Parameters.AddWithValue("c", DesgTxt.Text);
                cmd.Parameters.AddWithValue("d", JoinTxt.Text);
                cmd.Parameters.AddWithValue("e", BankTxt.Text);
                cmd.Parameters.AddWithValue("f", ACNoTxt.Text);
                cmd.Parameters.AddWithValue("g", PANTxt.Text);
                cmd.Parameters.AddWithValue("h", PFTxt.Text);
                numRowsUpdated = cmd.ExecuteNonQuery(); 
            } 
            GlobalCS.CloseConnection();                
        }

GridView选择:

protected void OnSelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = EmployeeGrid.SelectedRow;
            selectedid = Convert.ToInt32(row.Cells[1].Text);
            NameTxt.Text = row.Cells[2].Text;
            EmpidTxt.Text = row.Cells[3].Text;
          ...

        }

selectedid声明:

namespace SimERP
{
    public partial class Employee : System.Web.UI.Page
    {
        int selectedid = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadEmployeeData();
        }

1 个答案:

答案 0 :(得分:1)

代码似乎很好。您必须将@符号与参数名称放在一起:

示例:

 cmd.Parameters.AddWithValue("@i", selectedid);
 cmd.Parameters.AddWithValue("@a", NameTxt.Text);
 cmd.Parameters.AddWithValue("@b", EmpidTxt.Text);
 cmd.Parameters.AddWithValue("@c", DesgTxt.Text);

<强>更新

在Page_Load事件中也似乎缺少if(!IsPostBack)

namespace SimERP
{
    public partial class Employee : System.Web.UI.Page
    {
        int selectedid = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               LoadEmployeeData();
            }
        }