In the project, you only need to import Microsoft. Practices. enterpriselibrary. Data. dll.
Configuration:
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>
<Section name = "dataconfiguration" type = "Microsoft. practices. enterpriselibrary. data. configuration. databasesettings, Microsoft. practices. enterpriselibrary. data, version = 5.0.414.0, culture = neutral, publickeytoken = 31bf3856ad364e35 "requirepermission =" true "/>
</Configsections>
<Dataconfiguration defaultdatabase = "spare"> </dataconfiguration>
<Connectionstrings>
<Add name = "spare" connectionstring = "uid = sa; Pwd = 123456; initial catalog = dbit_documentspare; server =." providername = "system. Data. sqlclient"/>
</Connectionstrings>
</Configuration>
Reference namespace:
Using Microsoft. Practices. enterpriselibrary. Data;
Protected database = NULL;
Protected sqlconnection connection = NULL;
Database = databasefactory. createdatabase ();
Connection = database. createconnection () as sqlconnection;
Executescalar:
String literal text = "insert into [errorlog] values (@ p_createdate, @ p_thread, @ p_level, @ p_logger, @ p_message, @ p_exception); select @ identity ;";
Sqlcommand cmd = new sqlcommand (plain text, connection );
Database. addinparameter (CMD, "@ p_createdate", dbtype. datetime, entity. createdate );
Database. addinparameter (CMD, "@ p_thread", dbtype. String, entity. Thread );
Database. addinparameter (CMD, "@ p_level", dbtype. String, entity. Level );
Database. addinparameter (CMD, "@ p_logger", dbtype. String, entity. Logger );
Database. addinparameter (CMD, "@ p_message", dbtype. String, entity. Message );
Database. addinparameter (CMD, "@ p_exception", dbtype. String, entity. Exception );
Object o = database. executescalar (CMD );
Entity. ID = convert. toint64 (O );
Executenonquery:
String plain text = "Update [errorlog] Set createdate = @ p_createdate, thread = @ p_thread, level = @ p_level, logger = @ p_logger, message = @ p_message, exception = @ p_exception where id = @ p_id ";
Sqlcommand cmd = new sqlcommand (plain text, connection );
Database. addinparameter (CMD, "@ p_createdate", dbtype. datetime, entity. createdate );
Database. addinparameter (CMD, "@ p_thread", dbtype. String, entity. Thread );
Database. addinparameter (CMD, "@ p_level", dbtype. String, entity. Level );
Database. addinparameter (CMD, "@ p_logger", dbtype. String, entity. Logger );
Database. addinparameter (CMD, "@ p_message", dbtype. String, entity. Message );
Database. addinparameter (CMD, "@ p_exception", dbtype. String, entity. Exception );
Database. addinparameter (CMD, "@ p_id", dbtype. int64, entity. ID );
Int I = database. executenonquery (CMD );
Executereader:
String plain text = "select * from [errorlog] Where id = @ p_id ";
Sqlcommand cmd = new sqlcommand (plain text, connection );
Database. addinparameter (CMD, "@ p_id", dbtype. int64, ID );
Idatareader DR = database. executereader (CMD );
Errorlog result = NULL;
Try
{
If (Dr! = NULL & dr. Read ())
{
Result = new errorlog ();
Result. createdate = dr. getdatetime (1 );
Result. ID = ID;
Result. Thread = dr. getstring (2 );
Result. Level = dr. getstring (3 );
Result. Logger = dr. getstring (4 );
Result. Message = dr. getstring (5 );
Result. Exception = dr. getstring (6 );
}
}
Finally
{
Dr. Close ();
Dr. Dispose ();
}