Microsoft provides developers with an enterprise library for error handling, logging, AOP, database access, permission control, and more. The database access module is used here to implement database access to SQL Server.
First open the EntLibConfig.exe configuration program under the installation directory and select Add Data Settings
You can set the connection string name, the connection string content, and the type of connection database. Set to save as a profile later, the contents of the configuration file are:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Configuration>3 <configsections>4 < Sectionname= "Dataconfiguration"type= "Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, version=5.0.414.0, culture=neutral, publickeytoken=31bf3856ad364e35 " requirepermission= "true" />5 </configsections>
<--! Configure the default connection string name--6 <dataconfigurationDefaultDatabase= "ConnStr" />7 <connectionStrings>8 <Addname= "ConnStr"connectionString= "Data source=.;i Nitial catalog=tsinghua;integrated security=true "9 ProviderName= "System.Data.SqlClient" />Ten </connectionStrings> One </Configuration>
When the configuration is complete, The introduction of Microsoft.Practices.EnterpriseLibrary.Data.dll and Microsoft.Practices.EnterpriseLibrary.Data.SqlCe.dll two files. So the environment is built.
Before you perform a database operation, you need to create a DB class that creates the following method:
// the default connection string in configuration information Database db = databasefactory.createdatabase (); // using the custom connection string Database db = Databasefactory.createdatabase ("connstr");
In this way, you can invoke various methods in the DB object to manipulate the database, and all methods will be prompted at the time of the call, and this will not be discussed here.
Learn the Microsoft Enterpriselibrary 5.0 Data Access Application Block