Application Block 2.0 的学习笔记

1.   DataAccess Block
Execute Scalar
Database db  =   null ;
db 
=  DatabaseFactory.CreateDatabase( " QuickStarts Instance " ); 
int  count  =  ( int )db.ExecuteScalar( CommandType.Text,  " SELECT COUNT(*) FROM Customers " ); 
string  message  =   string .Format(  " There are {0} customers in the database " ,count);

Execute  DataSet
Database db  =   null ;
 db 
=  DatabaseFactory.CreateDatabase();
 DataSet ds  =  db.ExecuteDataSet(CommandType.Text,  " SELECT * From Customers " ); 
dataGrid1.DataSource 
=  ds.Tables[ 0 ];
using  (IDataReader dataReader  =  _db.ExecuteReader( " GetCategories " ))
    
{
        
// Processing code
        while (dataReader.Read())
        
{
            Category item 
= new Category(
                dataReader.GetInt32(
0),
                dataReader.GetString(
1),
                dataReader.GetString(
2));

            
this.cmbCategory.Items.Add(item);
        }

    }

执行存储过程

_db.LoadDataSet(  " GetProductsByCategory " ,   this .dsProducts,    new   string []  "Products" } ,  selectedCategory.CategoryId);

执行更新

System.Data.Common.DbCommand insertCommand  =   null ;
    insertCommand 
=  _db.GetStoredProcCommand( " HOLAddProduct " );
    _db.AddInParameter(insertCommand, 
" ProductName " ,
        DbType.String, 
" ProductName " , DataRowVersion.Current);
    _db.AddInParameter(insertCommand, 
" CategoryID " ,
        DbType.Int32, 
" CategoryID " , DataRowVersion.Current);
    _db.AddInParameter(insertCommand, 
" UnitPrice " ,
        DbType.Currency, 
" UnitPrice " , DataRowVersion.Current);  

    System.Data.Common.DbCommand deleteCommand 
=   null ;
    deleteCommand 
=  _db.GetStoredProcCommand( " HOLDeleteProduct " );
    _db.AddInParameter(deleteCommand, 
" ProductID " ,
        DbType.Int32, 
" ProductID " , DataRowVersion.Current);
    _db.AddInParameter(deleteCommand, 
" LastUpdate " ,
        DbType.DateTime, 
" LastUpdate " , DataRowVersion.Original); 

    System.Data.Common.DbCommand updateCommand 
=   null ;
    updateCommand 
=  _db.GetStoredProcCommand( " HOLUpdateProduct " );
    _db.AddInParameter(updateCommand, 
" ProductID " ,
        DbType.Int32, 
" ProductID " , DataRowVersion.Current);
    _db.AddInParameter(updateCommand, 
" ProductName " ,
        DbType.String, 
" ProductName " , DataRowVersion.Current);
    _db.AddInParameter(updateCommand, 
" CategoryID " ,
        DbType.Int32, 
" CategoryID " , DataRowVersion.Current);
    _db.AddInParameter(updateCommand, 
" UnitPrice " ,
        DbType.Currency, 
" UnitPrice " , DataRowVersion.Current);
    _db.AddInParameter(updateCommand, 
" LastUpdate " ,
        DbType.DateTime, 
" LastUpdate " , DataRowVersion.Current); 

    
int  rowsAffected  =  _db.UpdateDataSet(
        
this .dsProducts,
        
" Products " ,
        insertCommand,
        updateCommand,
        deleteCommand,
        UpdateBehavior.Standard);

加密连接字符串

string  provider  =   " RsaProtectedConfigurationProvider " ;
    Configuration config 
=   null ;
    config 
=  ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    ConfigurationSection section 
=  config.ConnectionStrings;
    
if  ((section.SectionInformation.IsProtected  ==   false &&
        (section.ElementInformation.IsLocked 
==   false ))
    
{
        
// Protect (encrypt) the "connectionStrings" section.
        section.SectionInformation.ProtectSection(provider);

  
// Save the encrypted section.
        section.SectionInformation.ForceSave = true;
        config.Save(ConfigurationSaveMode.Full);
    }


 

异常处理有对模块的异常处理和全局的异常处理两种

try
    
{
        
// TODO: Handle exceptions
        PuzzlerService.Dictionary.AddWord(txtWordToCheck.Text);
        errorProvider1.SetError(txtWordToCheck, 
"");
    }

    
catch  (Exception ex)
    
{
        
bool rethrow = ExceptionPolicy.HandleException(ex, "UI Policy");
        
if (rethrow)
            
throw;

        MessageBox.Show(
string.Format(
            
"Failed to add word {0}, please contact support.",
            txtWordToCheck.Text));
    }

3.   Logging Block

1)

   LogEntry log  =   new  LogEntry();
    log.Message 
=   string .Format( " Calculated PI to {0} digits " , args.Digits);
    log.Categories.Add(Category.General);
    log.Priority 
=  Priority.Normal;

    Logger.Write(log);

2)

 Logger.Write(
        
string .Format( " Calculating next 9 digits from {0} " , args.StartingAt),
        Category.General,
        Priority.Low
        );

请参考logger.Write的其他重载方法

2.   Exception Handling Block
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值