PDF.NETフレームワーク学習編のSQL-MAPは、格納プロセスを使用する.

26787 ワード

最近は「深藍先生」のPDF.NETフレームワークを勉強しています.基礎知識は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); 
}