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);
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();
db = DatabaseFactory.CreateDatabase();
DataSet ds
=
db.ExecuteDataSet(CommandType.Text,
"
SELECT * From Customers
"
);
dataGrid1.DataSource = ds.Tables[ 0 ];
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);
}
}
... {
// 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);
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);
}
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));
}
... {
// 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);
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
);
string .Format( " Calculating next 9 digits from {0} " , args.StartingAt),
Category.General,
Priority.Low
);
请参考logger.Write的其他重载方法
2. Exception Handling Block