www.pudn.com > 20071920285172.rar > LoginAction.cs


using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Data.SqlClient; 
using System.Data.OleDb; 
using System.Data.Common; 
using System.Data; 
using Login.DataSource; 
 
namespace Login.Logic 
{ 
    public class LoginAction 
    { 
        ///  
        /// 身份验证 
        ///  
        /// 用户名 
        /// 密码 
        /// 登录成功返回用户名,失败返空串 
        public static string canLogin(string name, string pwd) 
        { 
            Login.DataSource.DataSource.toreadxml(); 
            MD5E md5 = new MD5E(); 
            if (Login.DataSource.DataSource.DataType == "1") 
            { 
                ///  
                /// SQL逻辑部分 
                ///  
                try 
                { 
                    SqlConnection conn = Login.DataSource.DataSource.gSqlConnect(); 
                    conn.Open(); 
                    string sql = "SELECT * FROM users WHERE username='" + name + "' and userpwd='" + pwd + "'"; 
                    SqlDataAdapter da = new SqlDataAdapter(sql, conn); 
                    DataSet ds = new DataSet(); 
                    da.Fill(ds); 
                    if (ds.Tables[0].Rows.Count == 1) 
                    { 
                        conn.Close(); 
                        return name; 
                    } 
                    else 
                    { 
                        conn.Close(); 
                        return ""; 
                    } 
                } 
                catch (Exception ex) 
                { 
                    throw ex; 
                } 
            } 
            else 
            { 
                ///  
                /// ACCESS逻辑部分 
                ///  
                try 
                { 
                    OleDbConnection myConn = Login.DataSource.DataSource.gAccessConnect(); 
                    string sqlstring = "SELECT * FROM users WHERE username='" + name + "' and userpwd='" + md5.MD5(pwd) + "'"; 
                    OleDbCommand objCommand = new OleDbCommand(sqlstring, myConn); 
                    objCommand.CommandType = CommandType.Text; 
                    myConn.Open(); 
                    OleDbDataReader objDataReader = objCommand.ExecuteReader(); 
                    if (objDataReader.Read()) 
                    { 
                        myConn.Close(); 
                        return name; 
                    } 
                    else 
                    { 
                        myConn.Close(); 
                        return ""; 
                    }   
                } 
                catch (Exception ex) 
                { 
                    throw ex; 
                } 
            }  
        } 
    } 
}