Tutorials to .com

Tutorials to .com » Dotnet » Database » Operation of a database of examples of category (b. Provider category)

Operation of a database of examples of category (b. Provider category)

Print View , by: iSee ,Total views: 8 ,Word Count: 746 ,Date: Wed, 10 Jun 2009 Time: 6:24 PM

using System;
namespace com.joybase.DB
(
/ / / <summary>
/ / / database connection Class
/ / / </ Summary>
public class Provider
(
/ / A static link interface;
private static System.Data.IDbConnection conn;
/ / / <summary>
/ / / Constructor, set the private type, is to prevent invalid external reference;
/ / / </ Summary>
protected Provider ()
(
/ /
/ / TODO: Add constructor logic here
/ /
System.Data.IDbCommand x = new System.Data.OleDb.OleDbCommand ();

)
/ / / / / <summary>
/ / / / / database connection type, that is, judgments are System.Data.SqlClient type or types of System.Data.OleDB.
/ / / / / </ Summary>
/ / Private static DBType DataBaseType
/ / (
/ / Get
/ / (
/ / / / From the configuration file which read the database type string
/ / String m_DBType = System.Configuration.ConfigurationSettings.AppSettings [ "DataBase.Type"];
/ / / / If you do not set a default for the type of connection System.Data.SqlClient
/ / If (m_DBType == null)
/ / (
/ / Return DBType.SqlClient;
/ /)
/ / Else
/ / (
/ / / / If empty or MSSQLServer2000, using a type of connection System.Data.SqlClient
/ / If (m_DBType.Trim ()==""|| m_DBType.Trim (). ToUpper ()==" MSSQLSERVER2000 "| | m_DBType.Trim (). ToUpper ()==" MSSQLSERVER7")
/ / (
/ / Return DBType.SqlClient;
/ /)
/ / / / Other is the return type of connection System.Data.OleDB
/ / Else
/ / (
/ / If (m_DBType.Trim (). ToUpper ()==" OLEDB ")
/ / (
/ / Return DBType.OleDB;
/ /)
/ /)
/ /
/ /)
/ / Return DBType.OleDB;
/ /
/ /)
/ /
/ /
/ /)
/ / / <summary>
/ / / Overloaded getConn (string) method, when the connection string for the label's "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 Config file to set up connection string of the label </ 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 are in front of some code changes, in view of the connection System.Data.SqlClient
* String which will not happen "Provider" word, so we have the Provider in accordance with whether or not the words to determine
* The connection is based on the System.Data.OleDB or System.Data.SqlClient.
* Reference:
* ConnectionString property can be set to a single unit. (Not specified SqlConnection object Provider property.)
* - Or --
*
* You can set a single attribute (DataSource, Database, UserName, etc.). If you set a single attribute, it will generate for your connection string.
* Note in the connection string stored in a user name and password are the means that security settings. For more information, please refer to 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>
/ / / Enumerated type, that is, a database connection type of the enumeration
/ / / </ Summary>
enum DBType
(
SqlClient = 0,
OleDB = 1
)
)


.Net Database Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.