我在我的代码隐藏中使用下面的类连接到我的数据库。我如何(重载?)这个类在多个连接字符串(数据库)之间进行选择?
或者我应该为每个连接分别设置一个类?
public class DataClass
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseName"].ToString());
public DataClass()
{
}
public DataSet ds(SqlCommand cmd)
{
cmd.Connection = cn;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
//等
感谢您的投入。
答案 0 :(得分:2)
使用DbProviderFactory,从web.config获取提供程序字符串。
自从我使用它们以来已经有一段时间了(我现在大部分时间都使用EF或NHibernate),但是iirc就是这样的:
string providerName = ConfigurationManager.ConnectionStrings["myconnectionname"].ProviderName;
DbProviderFactory provider =
DbProviderFactories.GetFactory(providerName);
IDbConnection conn = provider.CreateConnection();
IDbCommand command = provider.CreateCommand();
等
答案 1 :(得分:1)
您需要使用DbProviderFactory
和DbConnection
,have a look into
答案 2 :(得分:0)
这听起来像DataClass
对IDbConnection
将IDbConnection参数添加到数据类的构造函数中,然后只传入您的方法应访问的数据库的相应连接对象。
您的新代码可能如下所示:
public class DataClass {
private IDbConnection cn;
public DataClass()
: this(new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseName"].ToString()) {}
public DataClass(IDbConnection conn) {
cn = conn;
}
public DataSet ds(SqlCommand cmd)
{
cmd.Connection = cn;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
答案 3 :(得分:0)
您可以创建DataFactory类,您可以在其中使用枚举DataStores和GetConenctionString方法。我正在使用Enterprice库连接数据库。
public class DataFactory
{
public enum DataStores
{
ReadOnly=1,
ReadWrite=2,
ReadWriteProTest=3
}
public static string GetConnectionString(DataStores dataStore)
{
Database currentDatabase = null;
switch (dataStore)
{
case (DataStores.ReadOnly):
currentDatabase = DatabaseFactory.CreateDatabase("ReadOnlyDB");
break;
case (DataStores.ReadWrite):
currentDatabase = DatabaseFactory.CreateDatabase("ReadWriteDB");
break;
case (DataStores.ReadWriteProTest):
currentDatabase = DatabaseFactory.CreateDatabase("ReadWriteProdTest");
break;
default:
currentDatabase = DatabaseFactory.CreateDatabase("ReadOnlyDB");
break;
}
return currentDatabase.ConnectionString;
}
}
在Web.Config中,您可以为不同的连接字符串指定值:
<connectionStrings>
<add name="ReadWriteDB" connectionString="Your connection string"/>
<add name="ReadWriteProdTest" connectionString="Your connection string"/>
<add name="ReadOnlyDB" connectionString="Your connection string"/>
答案 4 :(得分:0)
我为我工作的小东西做了这个,他们仍在使用ADO.Net,我们基本上想要删除它,但我们正在慢慢转换应用程序,并作为一个小的解决方法,使事情更顺利,同时抽象它(我意识到)它不是很干,但它暂时的工作非常好)。 ConnectionConstant是可能的连接字符串的枚举,ConnectionstringManager使用switch case获取适当的连接字符串
using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Data.SqlClient;
namespace xxx.Data {
public static class SqlConnectionManager
{
public static int ExecuteNonQuery(ConnectionConstant connectionConstant, string sql, params SqlParam[] sqlParameters)
{
try
{
var connectionString = ConnectionStringManager.GetConnectionString(connectionConstant);
int result;
using (var sqlConnection = new SqlConnection(connectionString))
{
using (var sqlCommand = new SqlCommand())
{
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = sql;
foreach (var parameter in sqlParameters)
{
sqlCommand.Parameters.Add(parameter.ParameterName, parameter.SqlDbType).Value = (parameter.Value).ToString() == "" ? DBNull.Value : parameter.Value;
}
sqlConnection.Open();
result = sqlCommand.ExecuteNonQuery();
}
}
return result;
}
catch (SqlException sqlException)
{
throw;
}
catch (Exception ex)
{
throw;
}
}
public static DataTable ExecuteReader(ConnectionConstant connectionConstant, string sql, params SqlParam[] sqlParameters)
{
try
{
var connectionString = ConnectionStringManager.GetConnectionString(connectionConstant);
var result = new DataTable();
using (var sqlConnection = new SqlConnection(connectionString))
{
using (var sqlCommand = new SqlCommand())
{
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = sql;
foreach (var parameter in sqlParameters)
{
sqlCommand.Parameters.Add(parameter.ParameterName, parameter.SqlDbType).Value = (parameter.Value).ToString() == "" ? DBNull.Value : parameter.Value;
}
sqlConnection.Open();
using(var dataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection))
{
result.Load(dataReader);
}
}
}
return result;
}
catch (SqlException sqlException)
{
throw;
}
catch (Exception ex)
{
throw;
}
}
public static string ExecuteScalar(ConnectionConstant connectionConstant, string sql, params SqlParam[] sqlParameters)
{
try
{
var connectionString = ConnectionStringManager.GetConnectionString(connectionConstant);
object result = null;
using (var sqlConnection = new SqlConnection(connectionString))
{
using (var sqlCommand = new SqlCommand())
{
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = sql;
foreach (var parameter in sqlParameters)
{
sqlCommand.Parameters.Add(parameter.ParameterName, parameter.SqlDbType).Value = (parameter.Value).ToString() == "" ? DBNull.Value : parameter.Value;
}
sqlConnection.Open();
result = sqlCommand.ExecuteScalar();
}
}
if (result != null)
return result.ToString();
else
return string.Empty;
}
catch (SqlException sqlException)
{
throw;
}
catch (Exception ex)
{
throw;
}
}
}
}