最近一直在學習“深藍醫生”的PDF.NET框架,對Sql-Map使用儲存過程有了點小小的體會。基礎知識請到http://www.cnblogs.com/bluedoctor/archive/2010/07/03/1769890.html中檢視。
本文通過一個簡單的例子以說明通過Sql-Map使用儲存過程,並返回一個實體類。
功能實現:通過儲存過程驗證登入使用者的合法性。
1.建立資料庫表:
//系統使用者維護表
CREATE TABLE T_USER ( [USER_ID] [int] PRIMARY KEY, [LOGIN_NAME] [nvarchar](50) NULL, [REAL_NAME] [nvarchar](50) NULL, [PASSWORD] [nvarchar](50) NULL, [USER_STATE] [int] NULL, --0:未啟用 1:啟用 [REMARK] [nvarchar](200) NULL )
2.建立使用者驗證儲存過程:
//功能:驗證使用者的合法性。如果驗證通過則返回當前登入使用者的全部資訊
CREATE PROCEDURE up_user_verify ( @login_name nvarchar(50), @password nvarchar(50), @error nvarchar(500) output --如果驗證未通過,輸出錯誤提示資訊。 ) AS declare @user_id int, @pwd nvarchar(50), @user_state int --判斷是否存在使用者,此處對使用者的登入名和登入密碼進行大小寫區分 select @user_id = [user_id], @pwd = [password], @user_state = [user_state]
from t_user
where cast([login_name] as varbinary) = cast(@login_name as varbinary)
--登入名驗證 if @user_id is null begin set @error = '使用者登入名:'+@login_name+'不存在' return null end --登入密碼驗證 if cast(@password as varbinary) != cast(@pwd as varbinary) begin set @error = '登入密碼不正確,請重新輸入' return null end --使用者狀態驗證 if @user_state = 0 begin set @error = '使用者:' +@login_name+'未啟用' return null end
--通過驗證,返回使用者的資訊 select * from t_user where [login_name] = @login_name
3.生成表T_USER對應的實體類:
[Serializable()] public partial class Tb_User : EntityBase { public Tb_User() { TableName = "T_USER"; EntityMap=EntityMapType.Table; PrimaryKeys.Add("USER_ID"); } protected override void SetFieldNames() { PropertyNames = new string[] { "USER_ID","LOGIN_NAME","REAL_NAME","PASSWORD","USER_STATE","REMARK" }; } /// <summary> /// 使用者ID /// </summary> public System.Int32 USER_ID { get{return getProperty<System.Int32>("USER_ID");} set{setProperty("USER_ID",value );} } /// <summary> /// 登入名稱 /// </summary> public System.String LOGIN_NAME { get{return getProperty<System.String>("LOGIN_NAME");} set{setProperty("LOGIN_NAME",value ,50);} } /// <summary> /// 真實姓名 /// </summary> public System.String REAL_NAME { get{return getProperty<System.String>("REAL_NAME");} set{setProperty("REAL_NAME",value ,50);} } /// <summary> /// 登入密碼 /// </summary> public System.String PASSWORD { get{return getProperty<System.String>("PASSWORD");} set{setProperty("PASSWORD",value ,50);} } /// <summary> /// 使用者狀態 /// </summary> public System.String USER_STATE { get{return getProperty<System.String>("USER_STATE");} set{setProperty("USER_STATE",value ,50);} } /// <summary> /// 備註資訊 /// </summary> public System.String REMARK { get{return getProperty<System.String>("REMARK");} set{setProperty("REMARK",value ,200);} } }
4.Sql-Map配置指令碼:
<CommandClass Name="UserManage" Class="UserInfoManageDAL" Description="系統使用者資訊維護" Interface=""> <Select CommandName="VerifyUser" CommandType="StoredProcedure" Method="" Description="驗證使用者的合法性"
ResultClass="EntityObject" ResultMap="DAL.Entitys.Tb_User"> <![CDATA[[up_user_verify] #login_name:String,String,50,Input# #password:String,String,50,Input# #error:String,String,500,Output#]]> </Select>
</CommandClass>
5.SQL-MAP的DAL程式:
public partial class UserInfoManageDAL : DBMapper { /// <summary> /// 預設建構函式 /// </summary> public UserInfoManageDAL() { Mapper.CommandClassName = "UserManage"; Mapper.EmbedAssemblySource = "DAL,DAL.SqlMap.config"; }/// <summary> /// 使用者合法性驗證 /// </summary> /// <param name="loginName">登入名</param> /// <param name="password">登入密碼</param> /// <param name="error">驗證失敗的錯誤資訊</param> /// <returns>驗證通過,返回使用者實體類例項</returns> public Tb_User VerifyUser(string loginName, string password, ref string error) { CommandInfo cmdInfo = Mapper.GetCommandInfo("VerifyUser"); cmdInfo.DataParameters[0].Value = loginName; cmdInfo.DataParameters[1].Value = password; cmdInfo.DataParameters[2].Value = error; Tb_User user = EntityQuery<Tb_User>.QueryObject(CurrentDataBase.ExecuteDataReader(cmdInfo.CommandText, cmdInfo.CommandType, cmdInfo.DataParameters)); error = user == null ? cmdInfo.DataParameters[2].Value.ToString() : string.Empty; return user; } /// <summary> /// 獲取全部使用者資訊 /// </summary> /// <returns>使用者實體列表</returns> public List<Tb_User> GetAllUser() { return OQL.From<Tb_User>().Select().END.ToList<Tb_User>(); } /// <summary> /// 通過使用者ID獲取使用者資訊 /// </summary> /// <param name="userId">使用者ID</param> /// <returns></returns> public Tb_User GetUserById(int userId) { Tb_User user = new Tb_User(); user.USER_ID = userId; OQL q = OQL.From(user).Select().Where(user.USER_ID).END; return EntityQuery<Tb_User>.QueryObject(q); } }
6.測試程式
private void button1_Click(object sender, EventArgs e) { UserInfoManageDAL dal = new UserInfoManageDAL(); string name = "admin"; string pwd = "abc"; string error = string.Empty; Tb_User user = dal.VerifyUser(name, pwd, ref error); if (user == null) MessageBox.Show(error); else MessageBox.Show(user.USER_ID.ToString() + ":" user.LOGIN_NAME + ":" + user.REAL_NAME);
}