问个关于数据库操作的问题,请进
[问]请问下面两种方法哪种方法比较好?
第一个方法:
public bool GetLogin(string UserName, string Password)
{
try
{
StringBuilder sbSql = new StringBuilder();
sbSql.Append( " IF (SELECT Count( 'x ') FROM [User] WHERE UserName = @UserName AND Password = @Password) > = 1 ");
sbSql.Append( " BEGIN ");
sbSql.Append( " UPDATE [User] SET IsLogin = '1 ' WHERE UserName = @UserName; ");
sbSql.Append( " SELECT 1; ");
sbSql.Append( " END ");
sbSql.Append( " ELSE ");
sbSql.Append( " BEGIN ");
sbSql.Append( " SELECT 2; ");
sbSql.Append( " END ");
string strSql = sbSql.ToString();
Database db = DatabaseFactory.CreateDatabase();
DbCommand cmdSql = db.GetSqlStringCommand(strSql);
db.AddInParameter(cmdSql, "UserName ", DbType.String, UserName);
db.AddInParameter(cmdSql, "Password ", DbType.String, Password);
return Convert.ToInt32(db.ExecuteScalar(cmdSql)) == 1;
}
catch (Exception)
{
return false;
}
}
第二个方法:
public bool GetLogin(string UserName, string Password)
{