当前位置:编程学习 > C#/ASP.NET >>

update语句无法更新数据库

大家好。

我这有个类。主要是实现添加、更新和删除。

public class Addstaff : Page
{
    // Fields
    protected Button cancelBTN;
    protected Button createBTN;
    protected DropDownList ddl_archives;
    protected TextBox dept;
    protected TextBox image_big;
    protected TextBox image_small;
    protected TextBox intro;
    protected TextBox orderid;
    protected RequiredFieldValidator RequiredCnName;
    protected RequiredFieldValidator RequiredEnName;
    protected RequiredFieldValidator RequiredFieldValidator1;
    protected RequiredFieldValidator RequiredImage;
    protected RequiredFieldValidator RequiredIntro;
    protected TextBox staffcode;
    protected Button updateBTN;
    protected TextBox username;

    // Methods
    protected void cancelBTN_Click(object sender, EventArgs e)
    {
        DALHelper.ExecuteNonQuery("sql", " delete from po_staff where id=" + base.Request.QueryString["id"]);
        Helper.Result(this, "操作成功", "stafflist.aspx");
    }

    protected void createBTN_Click(object sender, EventArgs e)
    {
        string commandText = " select count(*) from po_staff where username='" + this.username.Text.Trim() + "' ";
        if (int.Parse(DALHelper.ExecuteScalar("sql", commandText).ToString()) > 0)
        {
            Helper.Result(this, "用户名已经存在");
        }
        else
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@username", this.username.Text), new SqlParameter("@staffcode", this.staffcode.Text), new SqlParameter("@dept", this.dept.Text), new SqlParameter("@intro", this.intro.Text), new SqlParameter("@image_big", this.image_big.Text), new SqlParameter("@image_small", this.image_small.Text), new SqlParameter("@orderid", this.orderid.Text), new SqlParameter("@poid", this.ddl_archives.SelectedItem.Value) };
            commandText = "\r\n            insert into po_staff(username,staffcode,dept,intro,image_small,image_big, orderid, poid)values\r\n              (@username,@staffcode,@dept,@intro,@image_small,@image_big,@orderid,@poid )\r\n             ";
            DALHelper.ExecuteNonQuery(DALHelper.GetCon(), CommandType.Text, commandText, commandParameters);
            Helper.ResultRefresh(this, "操作成功,您可以继续添加候选人");
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet set;
        if (!this.Page.IsPostBack)
        {
            set = DALHelper.ExecuteDataset("sql", " select id, votename from po_config order by id desc ");
            this.ddl_archives.DataSource = set;
            this.ddl_archives.DataTextField = "votename";
            this.ddl_archives.DataValueField = "id";
            this.ddl_archives.DataBind();
        }
        if (base.Request.QueryString["id"] != null)
        {
            this.createBTN.Visible = false;
            this.updateBTN.Visible = true;
            this.cancelBTN.Visible = true;
            set = DALHelper.ExecuteDataset("sql", " select * from po_staff where id=" + base.Request.QueryString["id"]);
            this.username.Text = set.Tables[0].Rows[0]["username"].ToString();
            this.staffcode.Text = set.Tables[0].Rows[0]["staffcode"].ToString();
            this.intro.Text = set.Tables[0].Rows[0]["intro"].ToString();
            this.image_small.Text = set.Tables[0].Rows[0]["image_small"].ToString();
            this.image_big.Text = set.Tables[0].Rows[0]["image_big"].ToString();
            this.dept.Text = set.Tables[0].Rows[0]["dept"].ToString();
            this.orderid.Text = set.Tables[0].Rows[0]["orderid"].ToString();
            this.username.Enabled = false;
            try
            {
                this.ddl_archives.SelectedValue = set.Tables[0].Rows[0]["poid"].ToString();
            }
            catch
            {
            }
        }
        else
        {
            this.username.Enabled = true;
            this.createBTN.Visible = true;
            this.updateBTN.Visible = false;
            this.cancelBTN.Visible = false;
        }
    }

    protected void updateBTN_Click(object sender, EventArgs e)
    {
        SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@id", base.Request.QueryString["id"]), new SqlParameter("@staffcode", this.staffcode.Text), new SqlParameter("@dept", this.dept.Text), new SqlParameter("@intro", this.intro.Text), new SqlParameter("@image_big", this.image_big.Text), new SqlParameter("@image_small", this.image_small.Text), new SqlParameter("@orderid", this.orderid.Text), new SqlParameter("@poid", this.ddl_archives.SelectedItem.Value) };
        string commandText = "\r\n           update po_staff set \r\n          staffcode=@staffcode,\r\n          intro=@intro, \r\n          image_small=@image_small, \r\n          image_big=@image_big,\r\n          dept=@dept,\r\n          orderid=@orderid,\r\n          poid=@poid\r\n          where  id=@id\r\n             ";
        DALHelper.ExecuteNonQuery(DALHelper.GetCon(), CommandType.Text, commandText, commandParameters);
        Helper.Result(this, "操作成功", "stafflist.aspx");
    }
}



添加:从表单里获得数据添加进入数据库

更新:从数据库读取数据更改后修改数据库数据

删除:直接删除数据库数据

添加和删除都正常,但是更新时虽然弹出了“操作成功”,但是数据库的数据根本没有改变,update语句失效了。

这是什么问题?

谢谢大家了。

附:界面截图

添加


更新和删除


--------------------编程问答-------------------- 估计是new SqlParameter("@id", base.Request.QueryString["id"])这个id不有传值过来,没有符合条件的记录,不会报错,也不会更新。你查一下是否影响行数?

--------------------编程问答--------------------
引用 1 楼 hdhai9451 的回复:
估计是new SqlParameter("@id", base.Request.QueryString["id"])这个id不有传值过来,没有符合条件的记录,不会报错,也不会更新。你查一下是否影响行数?


我试了直接修改update语句,把其中某个字段手动赋值而不从表单获取,可以更新。

比如我把“简介"直接赋1111111111111111能更新。说明id值应该传过来了。

string commandText = "\r\n           update po_staff set \r\n          staffcode=@staffcode,\r\n          intro=1111111111111111, \r\n          image_small=@image_small, \r\n          image_big=@image_big,\r\n          dept=@dept,\r\n          orderid=@orderid,\r\n          poid=@poid\r\n          where  id=@id\r\n             ";
--------------------编程问答-------------------- DALHelper.ExecuteNonQuery(DALHelper.GetCon(), CommandType.Text, commandText, commandParameters);

这一行打个断点吧   看看执行的sql语句是什么,拷出来放到查询分析器里去执行估计就清楚了 --------------------编程问答-------------------- sql语句可能有错误,把update的语句打出来,查一查 --------------------编程问答-------------------- 我怀疑是我点‘更新’的时候会不会没把我修改后的数据传过来,而还是读取的之前的数据。虽然更新了但数据没变。

因为我直接赋值还是能更新的。至少执行了update语句。

那代码哪出问题了? --------------------编程问答-------------------- 进DALHelper.ExecuteNonQuery 调试下吧  --------------------编程问答--------------------
引用 5 楼 k1014247445 的回复:
我怀疑是我点‘更新’的时候会不会没把我修改后的数据传过来,而还是读取的之前的数据。虽然更新了但数据没变。

因为我直接赋值还是能更新的。至少执行了update语句。

那代码哪出问题了?
自己打断点调试啊,看一下commandText的值 --------------------编程问答--------------------
引用 7 楼 Chinajiyong 的回复:
Quote: 引用 5 楼 k1014247445 的回复:

我怀疑是我点‘更新’的时候会不会没把我修改后的数据传过来,而还是读取的之前的数据。虽然更新了但数据没变。

因为我直接赋值还是能更新的。至少执行了update语句。

那代码哪出问题了?
自己打断点调试啊,看一下commandText的值


。。。。这些代码是从网上一个分享的项目里反编译出来的,调试不了了。只能找问题改了再编译回去。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,