An instance of a database operation class (two. Provider Class)

Source: Internet
Author: User
Tags tag name trim
Data | database using System;
Namespace Com.joybase.DB
{
<summary>
Database connection Provider Class
</summary>
public class Provider
{
A static connection interface;
private static System.Data.IDbConnection Conn;
<summary>
Construction method, set to private type, is a reference to prevent invalid external;
</summary>
Protected Provider ()
{
//
Todo:add constructor Logic here
//
System.Data.IDbCommand x=new System.Data.OleDb.OleDbCommand ();

}
<summary>
The type of database connection, that is, the System.Data.SqlClient type or the System.Data.OleDB type.
</summary>
private static DBType DatabaseType
//  {
Get
//   {
Read the database type string from the configuration file
String m_dbtype=system.configuration.configurationsettings.appsettings["Database.type"];
If a connection with the default System.Data.SqlClient type is not set
if (m_dbtype==null)
//    {
return dbtype.sqlclient;
//    }
Else
//    {
If NULL or MSSQLServer2000, use System.Data.SqlClient type of connection
if (m_dbtype.trim () = = "" | | M_dbtype.trim (). ToUpper () = = "MSSQLSERVER2000" | | M_dbtype.trim (). ToUpper () = = "MSSQLSERVER7")
//     {
return dbtype.sqlclient;
//     }
Others return the System.Data.OleDB type of connection
Else
//     {
if (M_dbtype.trim (). ToUpper () = "OLE DB")
//      {
return dbtype.oledb;
//      }
//     }
//
//    }
return dbtype.oledb;
//
//   }
//
//
//  }
<summary>
Overload Getconn (String) method, at which time the label name of the connection string is "database.connectionstring"
<seealso cref= "Getconn (String)"/>
</summary>
<returns> return a connection </returns>
public static System.Data.IDbConnection Getconn ()
{
Return Provider.getconn ("");
}
<summary>
Get Database Connection interface
</summary>
<param name= "P_connstringsetname" > a tag name that sets the connection string in the config file </param>
<returns></returns>
public static System.Data.IDbConnection Getconn (String p_connstringsetname)
{

if (conn==null)
//   {
String Connstr= "";
if (P_connstringsetname.trim () = "")
{
connstr=system.configuration.configurationsettings.appsettings["Database.connectionstring"];
}
Else
{
Connstr=system.configuration.configurationsettings.appsettings[p_connstringsetname];
}
if (connstr==null| | connstr== "")
{
throw new Exception ("Not find connection string!");
}
DBType M_dbtype;//=provider.databasetype;
/*
* Note: We have made some modifications to the previous code, in view of the System.Data.SqlClient connection
* The word "Provider" is not possible in the string, so we judge according to whether there are Provider words
* The connection is based on System.Data.SqlClient or System.Data.OleDB.
Resources
* You can set the ConnectionString property to a single cell. (You cannot specify a Provider property for a SqlConnection object.) )
*– or –
*
* You can set individual properties (DataSource, Database, UserName, and so on). If you set a single property, the connection string is generated for you.
* Note that storing user names and passwords in the connection string has a security setting. For more information, see Introduction to Ado.net Connection design Tools.
*
*/
if (Connstr.tolower (). IndexOf ("provider") ==-1) m_dbtype=dbtype.sqlclient;
else M_dbtype=dbtype.oledb;
Try
{
if (m_dbtype==dbtype.sqlclient)
{
Conn=new System.Data.SqlClient.SqlConnection (CONNSTR);
}
Else
{
Conn=new System.Data.OleDb.OleDbConnection (CONNSTR);
}
}
Catch
{
throw new Exception ("Error to connect db!");
}
//   }
IF (Conn. state!=system.data.connectionstate.closed) Conn. Close ();
Return conn;

}

}
<summary>
Enumeration type, that is, an enumeration of database connection types
</summary>
Enum DBType
{
Sqlclient=0,
Oledb=1
}
}


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.