SqlHelper使用方法(转)

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SQLHelper;
using System.Text;
using System.Data.SqlClient;
/// <summary>
/// UserInfo 的摘要说明
/// </summary>
namespace BLL
{
   
public class UserInfo
    {
       
#region
       
///<summary>
       
///创建一个新用户
       
///</summary>
       
///<param name="username">用户名称</param>
       
///<param name="password">用户密码</param>
       
///<param name="email">电子邮件</param>
       
///<param name="question">密码问题</param>
       
///<param name="answer">问题答案</param>
       
///<returns></returns>
       public int createUser(string username, string password, int power, string email, string question, string answer)
        {
            StringBuilder sb
= new StringBuilder();
            sb.Append(
"insert into UserInfo ([UserName],[Password],[Power],[Email],[Question],[Answer]) values");
            sb.Append(
"(@UserName,@Password,@Power,@Email,@Question,@Answer)");
            SqlParameter[] param
=
                                    {
                                        DBHelper.GetParameter(
"@UserName",SqlDbType.NVarChar,50,"[UserName]",username),
                                        DBHelper.GetParameter(
"@Password",SqlDbType.NVarChar,50,"[Password]",password),
                                        DBHelper.GetParameter(
"@Power",SqlDbType.Int,4,"[Power]",power),
                                        DBHelper.GetParameter(
"@Email",SqlDbType.NVarChar,50,"[Email]",email),
                                        DBHelper.GetParameter(
"@Question",SqlDbType.NVarChar,50,"[Question]",question),
                                        DBHelper.GetParameter(
"@Answer",SqlDbType.NVarChar,50,"[Answer]",answer)
                                    };
           
return DBHelper.ExecuteSql(sb.ToString(), param);
        }
       
#endregion
       
#region
       
///<summary>
       
///删除一个用户
       
///</summary>
       
///<param name="userid">用户编号</param>
       
///<returns></returns>
       public int deleteUser(int userid)
        {
            StringBuilder sb
= new StringBuilder();
            sb.Append(
"delete from UserInfo where [UserID]=@UserID");
            SqlParameter[] param
= { DBHelper.GetParameter("@UserID", SqlDbType.Int,4, "[UserID]", userid) };
           
return DBHelper.ExecuteSql(sb.ToString(), param);
        }
       
#endregion
        #region
       
///<summary>
       
///修改密码
       
///</summary>
       
///<param name="userid">用户编号</param>
       
///<param name="newpassword">新密码</param>
       
///<returns></returns>
       public int updateUserPassword(int userid, string newpassword)
        {
            StringBuilder sb
= new StringBuilder();
            sb.Append(
"update UserInfo set [Password]=@newpassword where [UserID]=@UserID");
            SqlParameter[] param
=
                                   {
                                       DBHelper.GetParameter(
"@UserID", SqlDbType.Int, 4,"[UserID]", userid) ,
                                       DBHelper.GetParameter(
"@newpassword",SqlDbType.NVarChar,50,"[Password]",newpassword)
                                   };
           
return DBHelper.ExecuteSql(sb.ToString(), param);
        }
       
///<summary>
       
///修改用户资料
       
///</summary>
       
///<param name="userid">用户编号</param>
       
///<param name="password">用户密码</param>
       
///<param name="email">电子邮件</param>
       
///<param name="question">密码问题</param>
       
///<param name="answer">问题答案</param>
       
///<returns></returns>
       public int updateUser(int userid, string password, string email, string question, string answer)
        {
            StringBuilder sb
= new StringBuilder();
            sb.Append(
"update UserInfo set [Password]=@Password,[Email]=@Email,[Question]=@Question,");
            sb.Append(
"[Answer]=@Answer where [UserID]=@UserID");
            SqlParameter[] param
=
                                    {
                                        DBHelper.GetParameter(
"@UserID",SqlDbType.Int,4,"[UserID]",userid),
                                        DBHelper.GetParameter(
"@Password",SqlDbType.NVarChar,50,"[Password]",password),
                                        DBHelper.GetParameter(
"@Email",SqlDbType.NVarChar,50,"[Email]",email),
                                        DBHelper.GetParameter(
"@Question",SqlDbType.NVarChar,50,"[Question]",question),
                                        DBHelper.GetParameter(
"@Answer",SqlDbType.NVarChar,50,"[Answer]",answer)
                                    };
           
return DBHelper.ExecuteSql(sb.ToString(), param);
        }
       
#endregion
       
#region
       
///<summary>
       
///找回用户密码
       
///</summary>
       
///<param name="username">用户名</param>
       
///<param name="question">密码问题</param>
       
///<param name="answer">问题答案</param>
       
///<returns></returns>
       public stringresearchPassword(stringusername, stringquestion, stringanswer)
        {
            StringBuilder sb
= new StringBuilder();
            sb.Append(
"select [Password] from UserInfo where [UserName]=@UserName");
            sb.Append(
"and [Question]=@Question and [Answer]=@Answer");
            SqlParameter[] param
=
                                    {
                                        DBHelper.GetParameter(
"@UserName",SqlDbType.NVarChar,50,"[UserName]",username),
                                        DBHelper.GetParameter(
"@Question",SqlDbType.NVarChar,50,"[Question]",question),
                                        DBHelper.GetParameter(
"@Answer",SqlDbType.NVarChar,50,"[Answer]",answer)
                                    };
            DataTable table
= DBHelper.ExecuteDt(sb.ToString(), param);
           
if (table.Rows.Count > 0)
            {
               
return table.Rows[0]["Password"].ToString();
            }
           
else
            {
               
return "";
            }
        }
       
///<summary>
       
///获得一个用户资料
       
///</summary>
       
///<param name="userid"></param>
       
///<returns></returns>
       public DataTable getUser(intuserid)
        {
            StringBuilder sb
= new StringBuilder();
            sb.Append(
"select [UserID],[UserName],[Password],[Power],[Email],[Question],[Answer]");
            sb.Append(
"from UserInfo where [UserID]=@UserID");
            SqlParameter[] param
= { DBHelper.GetParameter("@UserID", SqlDbType.Int,4, "[UserID]", userid) };
           
return DBHelper.ExecuteDt(sb.ToString(), param);
        }
       
#endregion
       
publicUserInfo()
        {
           
//
           
// TODO: 在此处添加构造函数逻辑
           
//
        }
    }
}

文章出自:http://blog.sina.com.cn/s/blog_64310abb0100i0lc.html

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值