日期:2014-05-18  浏览次数:20455 次

新人求SQL语句指点
小弟有一个gridview 数据是动态加载的 现在要实现gridview数据的更新 删除 不知道怎么写了求高手指教

------解决方案--------------------
C# code

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {//取消编辑
        GridView1.EditIndex = -1;
        studentdatashow();//数据绑定的方法
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {//点击编辑之后
        GridView1.EditIndex = e.NewEditIndex;
        studentdatashow();//编辑之后的刷新数据绑定
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {//修改
        SqlConnection con = DB.creatcon();//数据库连接
        string uid = GridView1.DataKeys[e.RowIndex].Value.ToString();
        GridViewRow row = GridView1.Rows[e.RowIndex];
        TextBox tb1, tb2, tb3, tb4, tb5, tb6;//修改时的值所在文本框
        tb1 = (TextBox)(row.Cells[2].Controls[0]);
        tb2 = (TextBox)(row.Cells[3].Controls[0]);
        tb3 = (TextBox)(row.Cells[4].Controls[0]);
        tb4 = (TextBox)(row.Cells[5].Controls[0]);
        tb5 = (TextBox)(row.Cells[6].Controls[0]);
        tb6 = (TextBox)(row.Cells[7].Controls[0]);
        string sql = "update t_studentinfo set userpwd='"+tb1.Text.Trim()+"',name='"+tb2.Text.Trim()+"',sex='"+tb3.Text.Trim()+"',birthday='"+tb4.Text.Trim()+"',address='"+tb5.Text.Trim()+"',telphone='"+tb6.Text.Trim()+"' where sid="+uid;
//更新数据,sql写的很烂 见笑了
        SqlCommand cmd = new SqlCommand(sql,con);
        cmd.ExecuteNonQuery();//执行语句
        con.Close();
        GridView1.EditIndex = -1;//确定之后 刷新数据
        studentdatashow();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {//删除数据
        string uid = GridView1.DataKeys[e.RowIndex].Value.ToString();
        string sql = "delete from t_studentinfo where sid="+uid;
        SqlConnection con=DB.creatcon();
        SqlCommand cmd = new SqlCommand(sql,con);
        cmd.ExecuteNonQuery();
        con.Close();
        studentdatashow();
    }