MySQL DataCONnection 和 ADO.NET

发布于 2024-08-24 06:44:48 字数 348 浏览 2 评论 0原文

我想根据条件使用 mysqlconnection、命令和数据读取器或 sqlconnection、命令和数据读取器。我想对两种类型的连接使用相同的变量

例如:

Currently i have
dim _c as New mySqlConnection()
dim _cmd as New mysqlCommand()
dim _reader as New mysqlreader()

_reader = _cmd.executereader()
'loop through _reader
 -----------------

我如何更改前三个以根据选择情况使用 sql/mysql。这可能吗?

I want to use mysqlconnection, command and datareader or sqlconnection, command and datareader based on a condition. I want to use same variables for both type of connection

Ex:

Currently i have
dim _c as New mySqlConnection()
dim _cmd as New mysqlCommand()
dim _reader as New mysqlreader()

_reader = _cmd.executereader()
'loop through _reader
 -----------------

How can i change first three to use sql/mysql based on select case. Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小瓶盖 2024-08-31 06:44:48

是的,这是可能的。 ADO.NET 有一个用于此目的的提供程序 API。你必须做这样的事情:

string providerName;

if (true/*add your logic here*/)
   providerName = "System.Data.SqlClient";
else
   providerName = "MySql.Data.MySqlClient"

DbProviderFactory factory = DbProviderFactories.GetFactory(providerName);

DbConnection connection = factory.CreateConnection();
DbCommand command = factory.CreateCommand();

// ... and so on

Yes, it is possible. ADO.NET has a provider API for that. You have to do something like this:

string providerName;

if (true/*add your logic here*/)
   providerName = "System.Data.SqlClient";
else
   providerName = "MySql.Data.MySqlClient"

DbProviderFactory factory = DbProviderFactories.GetFactory(providerName);

DbConnection connection = factory.CreateConnection();
DbCommand command = factory.CreateCommand();

// ... and so on
反目相谮 2024-08-31 06:44:48

编写一个返回数据读取器的方法。将您的选择案例放在方法中,无论您使用什么条件来构建查询,都将返回数据读取器。

完成后请确保关闭数据读取器。

Write a method that returns the datareader. Place your select case in the method, regardless of the conditions you use to build the query, the data reader will be returned.

Make sure you close the data reader when you are done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文