| 一个数据库操作类的实例(二.Provider类) |
| 日期:2001年11月5日 作者:开心就好 人气: [大
中 小]
|
using System; namespace com.joybase.DB { /// <summary> /// 数据库连接提供类 /// </summary> public class Provider { //一个静态的连接接口; private static System.Data.IDbConnection conn; /// <summary> /// 构造方法,设为私有类型,是防止外部无效的引用; /// </summary> protected Provider() { // // TODO: Add constructor logic here // System.Data.IDbCommand x=new System.Data.OleDb.OleDbCommand(); } // /// <summary> // /// 数据库连接类型,即判断是System.Data.SqlClient类型或者是System.Data.OleDB类型的。 // /// </summary> // private static DBType DataBaseType // { // get // { // //从配置文件当中读取数据库类型字符串 // string m_DBType=System.Configuration.ConfigurationSettings.AppSettings["DataBase.Type"]; // //如果未设置默认为System.Data.SqlClient类型的连接 // if(m_DBType==null) // { // return DBType.SqlClient; // } // else // { // //如果为空或者为MSSQLServer2000,则使用System.Data.SqlClient类型的连接 // if(m_DBType.Trim()==""||m_DBType.Trim().ToUpper()=="MSSQLSERVER2000"||m_DBType.Trim().ToUpper()=="MSSQLSERVER7") // { // return DBType.SqlClient; // } // //其它则返回System.Data.OleDB类型的连接 // else // { // if(m_DBType.Trim().ToUpper()=="OLEDB") // { // return DBType.OleDB; // } // } // // } // return DBType.OleDB; // // } // // // } /// <summary> /// 重载getConn(string)方法,此时连接字符串的标签名将为“DataBase.ConnectionString” /// <seealso cref="getConn(string) "/> /// </summary> /// <returns>返回一个连接</returns> public static System.Data.IDbConnection getConn() { return Provider.getConn(""); } /// <summary> /// 获得数据库连接接口 /// </summary> /// <param name="p_ConnStringSetName">一个在Config文件中设置连接字符串的标签名</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; /* * 注释:我们对前面的编码进行了部分的修改,鉴于System.Data.SqlClient的连接 * 字符串当中不可能出现"Provider"字样,所以我们根据是否有Provider字样来判断 * 该连接是基于System.Data.SqlClient的或者System.Data.OleDB的。 * 参考资料: * 可以将 ConnectionString 属性设置为单个单元。(不能为 SqlConnection 对象指定 Provider 属性。) * –或– * * 可以设置单个属性(DataSource、Database、UserName 等等)。如果设置单个属性,则将为您生成连接字符串。 * 注意 在连接字符串中存储用户名和密码有安全性设置的意味。有关详细信息,请参阅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> /// 枚举类型,即一个数据库连接类型的枚举 /// </summary> enum DBType { SqlClient=0, OleDB=1 } } (出处:开发者俱乐部) |
| 相关文章: |
|
|
|
| 相关软件: |
|
|
|
|
|
|
| 说明:本站部分内容收集于网络,如有侵犯您的权益请来信告知,我们会第一时间进行处理,谢谢 |