我查看了几个站点 CodeProject、CSharpCorner、MSDN、CSharpPearls 等,包括一个 StackOverflow 链接:-
如何创建单个数据访问层以访问 asp.net 中的两个不同数据源
但我对答案很不满意。
我想在 ASP.net C# 中为我的网站创建一个 DAL,我在其中使用 Web.Config 来获取 ConnectionString。但事情是假设今天我连接到 SQLSERVER,我的 DAL 能够连接到 SQLSERVER。但是假设将来我再添加一个连接字符串,现在用一个连接字符串连接到 SQLSERVER,用另一个连接字符串连接到 MYSQL,我的 DAL 必须能够毫无问题地连接到所有类型的数据库。
到目前为止,我所做的是为 SQLServer 工作的 SQLServer,但我希望它是通用的,并且适用于 OLEDB、MYSQL、SQLCLIENT、(BIGTABLE & CASANDRA IF POSSIBLE)和所有其他提供程序。
这是我到目前为止所尝试的:-
namespace MyDAL
{
namespace DB
{
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// BaseDataManager is used to connect to database
/// </summary>
[Serializable()]
public class BaseDataManager : IDisposable
{
private bool _disposedValue = false;
private SqlConnection _connectionObject = null;
private SqlCommand _commandObject = null;
public BaseDataManager()
{ }
/// <summary>
/// Provide ConnectionString
/// </summary>
public BaseDataManager(string connectionString)
{
this.SqlConnectionString = connectionString;
}
/// <summary>
/// if config is true provide connectionstring name in Web.config
/// else if config is false provide connectionstring rather than providing
///name
/// </summary>
public BaseDataManager(String connectionString_Name, Boolean config)
{
if (config == true)
{
this.SqlConnectionString = ConfigurationManager.ConnectionStrings[connectionString_Name].ConnectionString;
}
else if (config == false)
{
this.SqlConnectionString = connectionString_Name;
}
else
{
Console.Out.WriteLine("Error in Connection String, Check Web.Config ");
}
}
/// <summary>
/// Provide data source=; as connection string, username and password of
/// database
/// </summary>
public BaseDataManager(string DataSource, string InitialCatalog, bool IntegratedSecurity)
{
if (IntegratedSecurity == true)
{
this.SqlConnectionString += "Data Source=" + DataSource + "InitialCatalog=" + InitialCatalog + ";Integrated Security=" + IntegratedSecurity;
}
}
/// <summary>
/// Provide data source=; as connection string, username and password of
/// database
/// </summary>
public BaseDataManager(string DataSource,string InitialCatalog, string username, string password)
{
this.SqlConnectionString += "Data Source="+DataSource+"InitialCatalog="+InitialCatalog+";User ID=" + username + ";Password=" + password;
}
public string SqlConnectionString
{
get;
set;
}
public virtual SqlConnection connection
{
get
{
if (_connectionObject == null && !String.IsNullOrEmpty (this.SqlConnectionString))
_connectionObject = new SqlConnection (this.SqlConnectionString);
return _connectionObject;
}
set
{
_connectionObject = value;
}
}
public virtual SqlCommand command
{
get
{
if (_commandObject == null)
_commandObject = new SqlCommand();
return _commandObject;
}
set
{
_commandObject = value;
}
}
public SqlConnection getOpenConnection()
{
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
return connection;
}
public SqlCommand getCommand()
{
return command;
}
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
//-------------------------------------------------------------------------
// Close the connection object prior to setting it to nothing
//----------------------------------------------------------------------
if (_connectionObject != null)
{
_connectionObject.Close();
_connectionObject.Dispose();
}
if (_commandObject != null)
{
_commandObject.Cancel();
_commandObject.Dispose();
}
}
_disposedValue = true;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
GC.Collect();
}
~BaseDataManager()
{
Dispose(false);
}
}
}
}
请帮我..