Spring.net + FluorineFx 项目搭建 之 四、Spring.net数据库调用写法

四、数据库调用

4.1 Dao类中的方法

4.1.1新增数据写法

新增一个人员,传入数据为:人员姓名,所属机构key;返回新增人员后的key。

/// <summary>
/// 新建人员,返回key
/// </summary>
/// <returns></returns>
public decimal CreatPersonForPersonName(String personName, decimal unitKey)
{
    return AdoTemplate.Execute<decimal>
        (
             delegate(IDbDataAdapter dataAdapter)
             {

                 if (dataAdapter is OracleDataAdapter)
                 {
                     OracleCommand commandToUse = (dataAdapter.SelectCommand as OracleCommand);
                     commandToUse.CommandText = CommandSQL.CreatPersonForPersonName;
                     commandToUse.BindByName = true;
                     commandToUse.Parameters.Add(":PERSON_NAME", OracleDbType.NVarchar2, ParameterDirection.Input).Value = personName;
                     commandToUse.Parameters.Add(":UNIT_KEY", OracleDbType.Decimal, ParameterDirection.Input).Value = unitKey;

                     commandToUse.Parameters.Add(":PERSON_KEY", OracleDbType.Decimal, ParameterDirection.Output);
                     if (commandToUse.ExecuteNonQuery() != 1)
                     {
                         throw new Exception("Dao内部出现错误");
                     }

                     return commandToUse.Parameters[":PERSON_KEY"].Value.ToDecimal(); ;
                 }
                 else
                 {
                     throw new Exception("Dao内部出现错误");
                 }
             }
        );
}

 

/* Formatted on 2010-5-20 11:22:02 (QP5 v5.114.809.3010) */
INSERT INTO PERSON_TBL PT (PT.PERSON_KEY,
                           PT.PERSON_NAME,
                           PT.PERSON_STATE,
                           PT.UNIT_KEY)
   VALUES   (GLOBAL_SEQUENCE.NEXTVAL,
             :PERSON_NAME,
             1,
             :UNIT_KEY)
RETURNING   PT.PERSON_KEY      INTO   :PERSON_KEY

 
 

4.1.2查询数据写法

/// <summary>
/// 得到person对象,根据key
/// </summary>
/// <param name="personKey"></param>
/// <returns></returns>
public Person GetPersonForPersonKey(decimal personKey)
{
    DataTable dataTable = AdoTemplate.Execute<DataTable>
        (
            delegate(IDbDataAdapter dataAdapter)
            {
                if (dataAdapter is OracleDataAdapter)
                {
                    OracleCommand commandToUse = (dataAdapter.SelectCommand as OracleCommand);
                    commandToUse.CommandText = CommandSQL.GetPersonForPersonKey;
                    commandToUse.BindByName = true;
                    commandToUse.Parameters.Add(":PERSON_KEY", OracleDbType.Decimal, ParameterDirection.Input).Value = personKey;
                    DataSet dataSet = new DataSet();
                    dataAdapter.Fill(dataSet);
                    return dataSet.Tables[0];
                }
                else
                {
                    throw new Exception("Dao内部出现错误");
                }
            }
        );
    return (from dataRow in dataTable.AsEnumerable()
            select new Person
            {
                PersonKey = dataRow.Field<decimal?>("PERSON_KEY"),
                PersonName = dataRow.Field<string>("PERSON_NAME").toNullString(),
                PersonNickname = dataRow.Field<string>("PERSON_NICKNAME").toNullString(),
                PersonPhoto = dataRow.Field<byte[]>("PERSON_PHOTO").Base64Encode(),
                PersonState = dataRow.Field<decimal?>("PERSON_STATE"),
                UnitKey = dataRow.Field<decimal?>("UNIT_KEY"),
            }).FirstOrDefault();
}

 

/* Formatted on 2010-5-24 16:53:30 (QP5 v5.114.809.3010) */
SELECT   PT.PERSON_KEY,
         PT.PERSON_NAME,
         PT.PERSON_NICKNAME,
         PT.PERSON_PHOTO,
         PT.PERSON_STATE,
         PT.UNIT_KEY
  FROM   PERSON_TBL PT
 WHERE   PT.PERSON_KEY  = :PERSON_KEY
 

 

4.1.3修改数据写法

/// <summary>
/// 更新用户基本信息
/// </summary>
/// <param name="personKey"></param>
/// <param name="personName"></param>
/// <param name="personNickname"></param>
/// <param name="personState"></param>
/// <param name="personPhoto"></param>
/// <param name="unitKey"></param>
/// <returns></returns>
public Boolean UpdatePersonForPersonKey(decimal personKey, String personName, String personNickname, decimal? personState, byte[] personPhoto, decimal? unitKey)
{
    return AdoTemplate.Execute<Boolean>
        (
             delegate(IDbDataAdapter dataAdapter)
             {
                 if (dataAdapter is OracleDataAdapter)
                 {
                     OracleCommand commandToUse = (dataAdapter.SelectCommand as OracleCommand);
                     commandToUse.CommandText = CommandSQL.UpdatePersonForKey;
                     commandToUse.BindByName = true;
                     commandToUse.Parameters.Add(":PERSON_KEY", OracleDbType.Decimal, ParameterDirection.Input).Value = personKey;
                     commandToUse.Parameters.Add(":PERSON_NAME", OracleDbType.NVarchar2, ParameterDirection.Input).Value = personName;
                     commandToUse.Parameters.Add(":PERSON_NICKNAME", OracleDbType.NVarchar2, ParameterDirection.Input).Value = personNickname;
                     commandToUse.Parameters.Add(":PERSON_STATE", OracleDbType.Decimal, ParameterDirection.Input).Value = personState;
                     commandToUse.Parameters.Add(":PERSON_PHOTO", OracleDbType.Blob, ParameterDirection.Input).Value = personPhoto;
                     commandToUse.Parameters.Add(":UNIT_KEY", OracleDbType.Decimal, ParameterDirection.Input).Value = unitKey;

                     if (commandToUse.ExecuteNonQuery() != 1)
                     {
                         throw new Exception("Dao内部出现错误");
                     }
                     return true;
                 }
                 else
                 {
                     throw new Exception("Dao内部出现错误");
                 }
             }
        );
}

 

/* Formatted on 2010-5-6 13:28:26 (QP5 v5.114.809.3010) */
UPDATE   PERSON_TBL PT
   SET   PT.PERSON_NAME = :PERSON_NAME,
         PT.PERSON_NICKNAME = :PERSON_NICKNAME,
         PT.PERSON_STATE = :PERSON_STATE,
         PT.PERSON_PHOTO = :PERSON_PHOTO,
         PT.UNIT_KEY = :UNIT_KEY
 WHERE   PT.PERSON_KEY = :PERSON_KEY

 

4.1.4删除数据写法

public Boolean DeleteUserForPersonKey(decimal personKey)
{
    return AdoTemplate.Execute<Boolean>
        (
             delegate(IDbDataAdapter dataAdapter)
             {

                 if (dataAdapter is OracleDataAdapter)
                 {
                     OracleCommand commandToUse = (dataAdapter.SelectCommand as OracleCommand);
                     commandToUse.CommandText = CommandSQL.DeleteUserForPersonKey;
                     commandToUse.BindByName = true;
                     commandToUse.Parameters.Add(":PERSON_KEY", OracleDbType.Decimal, ParameterDirection.Input).Value = personKey;

                     if (commandToUse.ExecuteNonQuery() != 1)
                     {
                         throw new Exception("Dao内部出现错误");
                     }

                     return true;
                 }
                 else
                 {
                     throw new Exception("Dao内部出现错误");
                 }
             }
        );
}

 

DELETE   USER_TBL UT
 WHERE   UT.PERSON_KEY = :PERSON_KEY

 
 

你可能感兴趣的:(DAO,spring,.net)