MySQL的 - 多个结果集 [英] MySQL - Multiple result sets

查看:137
本文介绍了MySQL的 - 多个结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用.NET连接器连接到MySQL。 在我的应用程序不存在使用相同的连接线程数,因此,如果MySqlDataReader中还没有关闭,有些线程试图执行一个查询它给出了错误:

  

目前已与此连接,必须先关闭相关联的打开的DataReader。

会不会有永远支持MySQL的多个结果集或者不管它叫什么名字?

我的数据库管理类:

 公共类的DatabaseConnection
{
    私人的MySqlConnection康涅狄格州;

    公共无效连接(字符串服务器,用户字符串,字符串密码,串数据库,INT端口= 3306)
    {
        字符串connStr =的String.Format(服务器= {0};用户= {1};数据库= {2};端口= {3};密码= {4};字符集= UTF8,
            服务器,用户,数据库,端口,密码);
        康恩=新的MySqlConnection(connStr);
        conn.Open();
    }

    私人的MySqlCommand prepareQuery(查询字符串,对象[]参数)
    {
        的MySqlCommand CMD =新的MySqlCommand();
        cmd.Connection =康涅狄格州;
        的for(int i = 0; I< args.Length;我++)
        {
            字符串参数={+ I +};
            字符串paramName配置=@DBVar_+我;
            查询= query.Replace(参数,paramName配置);
            cmd.Parameters.AddWithValue(paramName配置,ARGS [I]);
        }
        cmd.CommandText =查询;
        返回CMD;
    }

    公开名单<字典<字符串,对象>>查询(查询字符串,params对象[]参数)
    {
        的MySqlCommand CMD = prepareQuery(查询,参数);
        MySqlDataReader中读= cmd.ExecuteReader();
        名单<字典<字符串,对象>>行=新的名单,其中,字典<字符串,对象>>();
        而(reader.Read())
        {
            字典<字符串,对象>行=新字典<字符串,对象>();
            的for(int i = 0; I< reader.FieldCount;我++)
            {
                row.Add(reader.GetName(i)中,reader.GetValue(ⅰ));
            }
            rows.Add(行);
        }
        reader.Close();
        返回行;
    }

    公共对象ScalarQuery(查询字符串,params对象[]参数)
    {
        的MySqlCommand CMD = prepareQuery(查询,参数);
        返回cmd.ExecuteScalar();
    }

    公共无效执行(查询字符串,params对象[]参数)
    {
        的MySqlCommand CMD = prepareQuery(查询,参数);
        cmd.ExecuteNonQuery();
    }

    公共无效关闭()
    {
        conn.Close();
    }
}
 

我是如何用这样一个例子:

 的DatabaseConnection康恩=新的DatabaseConnection();
Conn.Connect(本地主机,根,,foogle);
VAR行= conn.Query(SELECT * FROM`posts` WHERE`ID` = {0},postID);
的foreach(行VAR行)
{
   Console.WriteLine(行[标题]); //写这篇文章的标题(例如)
}
 

解决方案

多个结果集的指单个查询或查询批处理返回多个行集。这些结果是通过所述一个和唯一的DataReader该连接访问。

什么你问的是完全不同的东西。你需要的能力进行一个连接的多个同步查询。 AFAIK .NET不支持,不为SQL Server或其它任何驱动程序。

共享多个线程之间的连接是一个坏主意,完全没有必要。 .NET将使用连接池来限制连接总数所以这是完全安全的,以获取每个(组)查询要执行一个新的连接。限制一个线程的连接的范围,您的问题将持续下去。

I'm using .NET Connector to connect to MySQL. In my application there are few threads using the same connection, so if a MySQLDataReader is not closed yet and some thread is trying execute a query it gives that error:

There is already an open DataReader associated with this Connection which must be closed first.

Will there ever be support in MySQL for multiple result sets or however it's called?

My database management class:

public class DatabaseConnection
{
    private MySqlConnection conn;

    public void Connect(string server, string user, string password, string database, int port = 3306)
    {
        string connStr = String.Format("server={0};user={1};database={2};port={3};password={4};charset=utf8",
            server, user, database, port, password);
        conn = new MySqlConnection(connStr);
        conn.Open();
    }

    private MySqlCommand PrepareQuery(string query, object[] args)
    {
        MySqlCommand cmd = new MySqlCommand();
        cmd.Connection = conn;
        for (int i = 0; i < args.Length; i++)
        {
            string param = "{" + i + "}";
            string paramName = "@DBVar_" + i;
            query = query.Replace(param, paramName);
            cmd.Parameters.AddWithValue(paramName, args[i]);
        }
        cmd.CommandText = query;
        return cmd;
    }

    public List<Dictionary<string, object>> Query(string query, params object[] args)
    {
        MySqlCommand cmd = PrepareQuery(query, args);
        MySqlDataReader reader = cmd.ExecuteReader();
        List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
        while (reader.Read())
        {
            Dictionary<string, object> row = new Dictionary<string, object>();
            for (int i = 0; i < reader.FieldCount; i++)
            {
                row.Add(reader.GetName(i), reader.GetValue(i));
            }
            rows.Add(row);
        }
        reader.Close();
        return rows;
    }

    public object ScalarQuery(string query, params object[] args)
    {
        MySqlCommand cmd = PrepareQuery(query, args);
        return cmd.ExecuteScalar();
    }

    public void Execute(string query, params object[] args)
    {
        MySqlCommand cmd = PrepareQuery(query, args);
        cmd.ExecuteNonQuery();
    }

    public void Close()
    {
        conn.Close();
    }
}

An example of how I use this:

DatabaseConnection Conn = new DatabaseConnection();
Conn.Connect("localhost", "root", "", "foogle");
var rows = conn.Query("SELECT * FROM `posts` WHERE `id` = {0}", postID);
foreach (var row in rows)
{
   Console.WriteLine(row["title"]); // Writes the post's title (example)
}

解决方案

Multiple result sets refers to a single query or query batch returning multiple row sets. Those results are accessed through the one and only DataReader for that connection.

What you're asking for is something quite different. You need the ability to performing multiple simultaneous queries of a single connection. Afaik .NET does not support that, not for SQL Server or any other driver.

Sharing a connection between multiple threads is a bad idea and totally unnecessary. .NET will use a connection pool to limit the total number of connections so It's perfectly safe to get a new connection for each (set of) queries you want to execute. Limit the scope of a connection to a thread and your problem will go away.

这篇关于MySQL的 - 多个结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆