Enterprise Library 5 Data Application

Source: Internet
Author: User
Tags connectionstrings

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 ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.