用c#实现的数据库抽象工厂(二)
以下3个类分别是factory针对sqlserver专用连接、oledb连接和odbc连接时的具体实现:
(3)sqlfactory.cs
using system;
using system.data;
using system.data.sqlclient;
using system.configuration;
namespace dbservice
{
/// <summary>
/// 针对sqlserver专用连接的工厂
/// </summary>
public class sqlfactory : abstractdbfactory
{
/// <summary>
/// 构造函数
/// </summary>
public sqlfactory()
{
}
/// <summary>
/// 建立默认connection对象
/// </summary>
/// <returns>connection对象</returns>
public idbconnection createconnection()
{
return new sqlconnection();
}
/// <summary>
/// 根据连接字符串建立connection对象
/// </summary>
/// <param name="strconn">连接字符串</param>
/// <returns>connection对象</returns>
public idbconnection createconnection(string strconn)
{
return new sqlconnection(strconn);
}
/// <summary>
/// 建立command对象
/// </summary>
/// <returns>command对象</returns>
public idbcommand createcommand()
{
return new sqlcommand();
}
/// <summary>
/// 建立dataadapter对象
/// </summary>
/// <returns>dataadapter对象</returns>
public idbdataadapter createdataadapter()
{
return new sqldataadapter();
}
/// <summary>
/// 根据connection建立transaction
/// </summary>
/// <param name="mydbconnection">connection对象</param>
/// <returns>transaction对象</returns>
public idbtransaction createtransaction(idbconnection mydbconnection)
{
return mydbconnection.begintransaction();
}
/// <summary>
/// 根据command建立datareader
/// </summary>
/// <param name="mydbcommand">command对象</param>
/// <returns>datareader对象</returns>
public idatareader createdatareader(idbcommand mydbcommand)
{
return mydbcommand.executereader();
}
/// <summary>
/// 获得连接字符串
/// </summary>
/// <returns>连接字符串</returns>
public string getconnectionstring()
{
string strserver = configurationsettings.appsettings["sqlserverserver"];
string strdatabase = configurationsettings.appsettings["sqlserverdatabase"];
string struid = configurationsettings.appsettings["sqlserveruid"];
string strpwd = configurationsettings.appsettings["sqlserverpwd"];
string strconnectionstring = "server = " + strserver + "; database = " + strdatabase + "; uid = " + struid + "; pwd = " + strpwd + ";";
return strconnectionstring;
}
}
}
(4)oledbfactory.cs
using system;
using system.data;
using system.data.oledb;
using system.configuration;
namespace dbservice
{
/// <summary>
/// 针对oledb连接的工厂
/// </summary>
public class oledbfactory : abstractdbfactory
{
/// <summary>
/// 构造函数
/// </summary>
public oledbfactory()
{
}
/// <summary>
/// 建立默认connection对象
/// </summary>
/// <returns>connection对象</returns>
public idbconnection createconnection()
{
return new oledbconnection();
}
/// <summary>
/// 根据连接字符串建立connection对象
/// </summary>
/// <param name="strconn">连接字符串</param>
/// <returns>connection对象</returns>
public idbconnection createconnection(string strconn)
{
return new oledbconnection(strconn);
}
/// <summary>
/// 建立command对象
/// </summary>
/// <returns>command对象</returns>
public idbcommand createcommand()
{
return new oledbcommand();
}
/// <summary>
/// 建立dataadapter对象
/// </summary>
/// <returns>dataadapter对象</returns>
public idbdataadapter createdataadapter()
{
return new oledbdataadapter();
}
/// <summary>
/// 根据connection建立transaction
/// </summary>
/// <param name="mydbconnection">connection对象</param>
/// <returns>transaction对象</returns>
public idbtransaction createtransaction(idbconnection mydbconnection)
{
return mydbconnection.begintransaction();
}
/// <summary>
/// 根据command建立datareader
/// </summary>
/// <param name="mydbcommand">command对象</param>
/// <returns>datareader对象</returns>
public idatareader createdatareader(idbcommand mydbcommand)
{
return mydbcommand.executereader();
}
/// <summary>
/// 获得连接字符串
/// </summary>
/// <returns>连接字符串</returns>
public string getconnectionstring()
{
string strprovider = configurationsettings.appsettings["oledbprovider"];
string strdatasource = configurationsettings.appsettings["oledbdatasource"];
string strconnectionstring = "provider = " + strprovider + ";data source = " + strdatasource + ";";
return strconnectionstring;
}
}
}
(5)odbcfactory.cs
using system;
using system.data;
using system.data.odbc;
using system.configuration;
namespace dbservice
{
/// <summary>
/// 针对odbc连接的工厂
/// </summary>
public class odbcfactory : abstractdbfactory
{
/// <summary>
/// 构造函数
/// </summary>
public odbcfactory()
{
}
/// <summary>
/// 建立默认connection对象
/// </summary>
/// <returns>connection对象</returns>
public idbconnection createconnection()
{
return new odbcconnection();
}
/// <summary>
/// 根据连接字符串建立connection对象
/// </summary>
/// <param name="strconn">连接字符串</param>
/// <returns>connection对象</returns>
public idbconnection createconnection(string strconn)
{
return new odbcconnection(strconn);
}
/// <summary>
/// 建立command对象
/// </summary>
/// <returns>command对象</returns>
public idbcommand createcommand()
{
return new odbccommand();
}
/// <summary>
/// 建立dataadapter对象
/// </summary>
/// <returns>dataadapter对象</returns>
public idbdataadapter createdataadapter()
{
return new odbcdataadapter();
}
/// <summary>
/// 根据connection建立transaction
/// </summary>
/// <param name="mydbconnection">connection对象</param>
/// <returns>transaction对象</returns>
public idbtransaction createtransaction(idbconnection mydbconnection)
{
return mydbconnection.begintransaction();
}
/// <summary>
/// 根据command建立datareader
/// </summary>
/// <param name="mydbcommand">command对象</param>
/// <returns>datareader对象</returns>
public idatareader createdatareader(idbcommand mydbcommand)
{
return mydbcommand.executereader();
}
/// <summary>
/// 获得连接字符串
/// </summary>
/// <returns></returns>
public string getconnectionstring()
{
string strdriver = configurationsettings.appsettings["odbcdriver"];
string strdbq = configurationsettings.appsettings["odbcdbq"];
string strconnectionstring = "driver={" + strdriver + "}; dbq=" + strdbq + ";";
return strconnectionstring;
}
}
}
待续……