C#MySQL连接多个select语句

时间:2021-07-02 23:03:17

I can connect to MySQL database from my WinForms app fine. The question is once I am logged in how can I perform multiple select statements without having to login again?

我可以从我的WinForms应用程序连接到MySQL数据库。问题是,一旦我登录,如何在不必再次登录的情况下执行多个select语句?

MySqlConnection connection = new MySqlConnection(MyConString);
connection.Open();

MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;

command.CommandText = "select id from user ";

Then I want to perform a select statement for another table without having to create connection again. How do I dothis? I can't seem to just do connection.CreateCommand.

然后我想为另一个表执行select语句,而不必再次创建连接。我该怎么做呢?我好像不能只做连接.CreateCommand。

2 个解决方案

#1


2  

As long as the queries are within the same block, you can use the same connection.. However, once closed, you need to re-open it.

只要查询在同一个块中,您就可以使用相同的连接。但是,一旦关闭,您需要重新打开它。

using( YourConnectionObject )
{
   ... open connection ...
   ... create your sql querying object... and command 
   SQLCommand.Connection = YourConnectionObject;

   Execute your Query

   SQLCommand.CommandText = "a new sql-select statement";

   Execute your NEW query while connection still open/active

   SQLCommand.CommandText = "a third sql-select statement";

   Execute your THIRD query while connection still open/active

   ... close your connection
}

However, in your application, you can have a single "connection" object like at the application level, or at a form level with the necessary login / connection settings stuff. Then, internally to each form, you can

但是,在您的应用程序中,您可以拥有一个“连接”对象,例如在应用程序级别,或者在表单级别具有必要的登录/连接设置。然后,在每个表单的内部,您可以

Open
Run Query
Run Query
Run Query
Close

as needed.

#2


0  

I see you're using a DataReader. You can only have 1 DataReader open at a time per connection. using blocks come in handy for those:

我看到你正在使用DataReader。每个连接一次只能打开1个DataReader。使用块派对派上用场:

using( var reader = myCommand.ExecuteReader() ) {
    while (reader.Read()) {
        // get values, do stuff
    }
}// reader gets closed

You only hint at it in the code in your question (currently), but it's possible that is part of your problem. You haven't shown how you're using the DataReader, so I'm not certain. Just a possibility.

您只在问题中的代码中提示(当前),但这可能是您问题的一部分。你还没有展示你如何使用DataReader,所以我不确定。只是一种可能性。

#1


2  

As long as the queries are within the same block, you can use the same connection.. However, once closed, you need to re-open it.

只要查询在同一个块中,您就可以使用相同的连接。但是,一旦关闭,您需要重新打开它。

using( YourConnectionObject )
{
   ... open connection ...
   ... create your sql querying object... and command 
   SQLCommand.Connection = YourConnectionObject;

   Execute your Query

   SQLCommand.CommandText = "a new sql-select statement";

   Execute your NEW query while connection still open/active

   SQLCommand.CommandText = "a third sql-select statement";

   Execute your THIRD query while connection still open/active

   ... close your connection
}

However, in your application, you can have a single "connection" object like at the application level, or at a form level with the necessary login / connection settings stuff. Then, internally to each form, you can

但是,在您的应用程序中,您可以拥有一个“连接”对象,例如在应用程序级别,或者在表单级别具有必要的登录/连接设置。然后,在每个表单的内部,您可以

Open
Run Query
Run Query
Run Query
Close

as needed.

#2


0  

I see you're using a DataReader. You can only have 1 DataReader open at a time per connection. using blocks come in handy for those:

我看到你正在使用DataReader。每个连接一次只能打开1个DataReader。使用块派对派上用场:

using( var reader = myCommand.ExecuteReader() ) {
    while (reader.Read()) {
        // get values, do stuff
    }
}// reader gets closed

You only hint at it in the code in your question (currently), but it's possible that is part of your problem. You haven't shown how you're using the DataReader, so I'm not certain. Just a possibility.

您只在问题中的代码中提示(当前),但这可能是您问题的一部分。你还没有展示你如何使用DataReader,所以我不确定。只是一种可能性。