【发布时间】:2013-01-07 15:46:48
【问题描述】:
我查看了几个站点 CodeProject、CSharpCorner、MSDN、CSharpPearls 等,包括一个 StackOverflow 链接:-
how to create single Data Access Layer to access two different data source in 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);
}
}
}
}
请帮帮我..
【问题讨论】:
-
Raj 您提供的链接中的答案告诉您需要做什么
The key is to make sure that your other code, uses an interface, not an implementation, to access your DAL.您的问题似乎是什么......? -
@DJKRAZE 我已经为服务类而不是 BaseDataManager 创建了接口。我的接口是:-
namespace MyDAL { namespace DB { using System; using System.Data.SqlClient; public interface DAO { int I_U_D_Data(String query); SqlDataReader read_Data(String query); } } } -
如何使用实体框架,它可以与许多不同的数据库一起使用。
-
@JohnSaunders 不,我不能使用实体框架,因为我的大三学生不知道它。我只需要使用带有 DAL 的 ADO.NET
-
仅供参考,EF 是“ADO.NET 实体框架”。此外,它已经发布了四年或更长时间 - 可能是您的晚辈才过时两年。
标签: c# asp.net database-connection data-access-layer