.NET中Update的应用

1. VIEW层
protected void gv_UserInfoList_RowCommand(object sender, GridViewCommandEventArgs e)
{
    int iRowNO;
    ClearMsg();

    switch (e.CommandName)
    {
        case "Edit":
            {
                try
                {
                    iRowNO = Convert.ToInt32(e.CommandArgument.ToString());
                    Response.Redirect(string.Format("{0}?UserID={1}", "UserInfo_Update.aspx", gv_UserInfoList.DataKeys[iRowNO].Value.ToString()));

                }
                catch (Exception ex)
                {
                    ShowMsg(ex.ToString());
                }
                finally
                {
                    SqlDBHelper.Close(connection);
                }

                break;
            }
    }
}


protected void btn_Update_Click(object sender, EventArgs e)
{
    ClearMsg();

    //判断必填项是否填写
    if (IsControlsValueNotNull() != "")
    {
        ShowMsg(IsControlsValueNotNull());
        return;

    }

    //判断填写项是否超过固定长度
    if (IsControlsValueOutOfSize() != "")
    {
        ShowMsg(IsControlsValueOutOfSize());
        return;

    }

    try
    {
        connection = SqlDBHelper.GetNewSqlConnection(DB.ConnectionString);

        //从界面获取相关控件的值
        t_tw_userinfo = GetControlsValue();

        //更新RoleInfo
        BT_TW_UserInfo.UserInfo_Update(connection, t_tw_userinfo);

        Response.Redirect("UserInfo_List.aspx");

    }
    catch (Exception ex)
    {
        ShowMsg(ex.ToString());
    }
    finally
    {
        SqlDBHelper.Close(connection);
    }
}


2. BUSINESS层
//====================================================================================
/// <summary>
/// 完成UserInfo的更新
/// </summary>
/// <returns></returns>
/// <originated>Shawn Qiu</originated>
/// <mender></mender>
/// <date>2010-04-01</date>
public static void UserInfo_Update(SqlConnection connection, MT_TW_UserInfo userInfo)
{
    ArrayList arrSql = new ArrayList();
    arrSql.Add(SQL_UserInfo_Update(userInfo));
    //事务执行
    SqlDBHelper.ExecuteNonQuery_Trans(connection, CommandType.Text, arrSql);

}//end of Function:UserInfo_Update(SqlConnection connection, MT_TW_UserInfo userInfo)

//====================================================================================
/// <summary>
/// 构造Update的基本数据的SQL语句
/// </summary>
/// <returns>SQL语句</returns>
/// <originated>Shawn Qiu</originated>
/// <mender></mender>
/// <date>2010-03-29</date>
private static string SQL_UserInfo_Update(MT_TW_UserInfo userInfo)
{
    return string.Format(@"update {0} set {1}=N'{4}',{2}=N'{5}',{3}=N'{6}' where {7}='{8}'"

        , MT_TW_UserInfo.TableName//0

        , MT_TW_UserInfo.FldName.UserName
        , MT_TW_UserInfo.FldName.UserRole
        , MT_TW_UserInfo.FldName.UserMemo

        , userInfo.UserName
        , userInfo.UserRole
        , userInfo.UserMemo

        , MT_TW_UserInfo.FldName.UserID
        , userInfo.UserID

        );
}//end of Function:SQL_UserInfo_Update(MT_TW_UserInfo userInfo)



你可能感兴趣的:(sql,C++,c,.net,C#)