gpt4 book ai didi

c# - 如何为类集属性传递参数(不是类声明)

转载 作者:行者123 更新时间:2023-11-30 17:09:15 25 4
gpt4 key购买 nike

背景:

我正在制作一个 c# 4.0 类以供重用,因此我可以将一个参数发送到我程序的大多数方法并传递一个“连接”对象,该对象可以包含以下内容:

  • 连接字符串
  • DbConnection(即 OleDbConnection 或 SQLConnection)
  • DataReader(即 OleDbDataReader 或 SQLDataReader)

理由是我最终会升级到 SQL Server 并且不想更改我的方法参数或代码太多。方法通常必须从同一个数据库中到处调用快速查询。

问题:

如何在以下行的方法中正确设置(和调用)DbType 的 DRMDbConnection“设置”参数(现在硬编码为整数 1)的右侧:

set { drmDbConnection = SetDRMDbConnection(1); }

示例代码:

public class Connections
{

public string DRMDbConnectionString { get; set; }
private object drmDbConnection;

public object DRMDbConnection
{
get { return drmDbConnection; }
set { drmDbConnection = SetDRMDbConnection(1); }
}

private object SetDRMDbConnection(int DbType)
{
if (DbType == 1)
return new System.Data.OleDb.OleDbConnection();
else
return new SqlConnection();
}

10 月 31 日编辑

我更新了类代码以使用推荐的 IDbConnection、IDbCommand 和 IDataReader。注意:如果不更改 SQL Server 连接字符串,您将收到错误消息:“已经有一个打开的 DataReader 与此命令关联,必须先将其关闭。”因此将其添加到连接字符串:“MultipleActiveResultSets=True;”

更新类代码

public class Connections
// reusuable object to hold all database Connections and a convenient DataReader and Command object.
// Its purpose is to also be able to handle your choice of database connection, i.e. OleDb or SQL Server.
{
public string DRMConnectionString { get; set; }
public IDbConnection DRMConnection;

public IDbCommand DRMCommand
{
get { return DRMConnection.CreateCommand(); }
set { }
}

public int DRMConnectionType
// must be called to setup database connection and command. The connectionstring must be previously set.
{
set
{
if (value == 1)
DRMConnection = new System.Data.OleDb.OleDbConnection(DRMConnectionString);
else
DRMConnection = new SqlConnection(DRMConnectionString);
}
}

public void CloseConnections()
{
if (DRMCommand != null)
DRMCommand.Dispose();

if ((DRMConnection != null) && (DRMConnection.State != ConnectionState.Closed))
DRMConnection.Close();
}
}

调用代码

var conn = new Connections();
conn.DRMConnectionString = "my connection string";
conn.DRMConnectionType = 2;
conn.DRMConnection.Open();
try
{
using (var cmd = conn.DRMCommand)
{
cmd.CommandText = "SELECT * FROM MyTable";
using (var rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
CallSubMethodThatAlsoOpensDataReader(conn);
}
}
}
}
finally
{
conn.CloseConnections();
}


CallSubMethodThatAlsoOpensDataReader(Connections Conn)
{
using (var cmd = Conn.DRMCommand)
{
cmd.CommandText = "SELECT * FROM MyOtherTable";
using (var rdr = cmd.ExecuteReader()) // Error: There is already an open DataReader associated with this Command which must be closed first.
{
; // do stuff
}
}
}

最佳答案

您可以按如下方式使其工作:

public IDbConnection DRMDbConnection // Note the change of the type
{
get { return drmDbConnection; }
}
public int DrmDbConnectionType
{
set {
if (vale== 1)
drmDbConnection = new System.Data.OleDb.OleDbConnection();
else
drmDbConnection = new SqlConnection();
}
}

The reasoning is that I will eventually upgrade to SQL Server and don’t want to change my method parameters or code so much.

我认为这是您要解决的关键问题。与其针对特定类编程,不如针对接口(interface)编程。具体来说,使用 IDbConnection代替 SqlConnectionOleDbConnection 比将其封装在 DRMDbConnection 中更能让您隐藏确切的类型。

关于c# - 如何为类集属性传递参数(不是类声明),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13092958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com