Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MySqlConnection()方法在DB消失时抛出异常的速度非常慢#_C#_Mysql - Fatal编程技术网

C# MySqlConnection()方法在DB消失时抛出异常的速度非常慢#

C# MySqlConnection()方法在DB消失时抛出异常的速度非常慢#,c#,mysql,C#,Mysql,我正在创建一个C#服务器,它打开一个命名管道并收集客户端发送给它的数据。基本上,我使用.NETMySQL连接器和MySqlConnection类将收集的信息存储在mysql数据库中。我还添加了一个回退规则,在数据库无法访问的情况下,将客户端发送的信息缓冲在一个SQL文件中,并在数据库再次可用时将其导入数据库。 一切正常,除了数据库完全消失(例如,运行数据库的服务器由于崩溃而不再在网络上时),Connect()方法会永远抛出异常。这是不可接受的,因为客户端发送了大量数据。有办法解决这个问题吗?如果

我正在创建一个C#服务器,它打开一个命名管道并收集客户端发送给它的数据。基本上,我使用.NETMySQL连接器和MySqlConnection类将收集的信息存储在mysql数据库中。我还添加了一个回退规则,在数据库无法访问的情况下,将客户端发送的信息缓冲在一个SQL文件中,并在数据库再次可用时将其导入数据库。 一切正常,除了数据库完全消失(例如,运行数据库的服务器由于崩溃而不再在网络上时),Connect()方法会永远抛出异常。这是不可接受的,因为客户端发送了大量数据。有办法解决这个问题吗?如果查询失败是因为不存在表或类似的东西,它会立即返回错误

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using MySql.Data.MySqlClient;

namespace PipeServer
{
    class MySqlConnector
    {
        //register logger
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public delegate void MessageReceivedHandler(string message);
        public event MessageReceivedHandler MessageReceived;

        private string host;
        private string database;
        private string user;
        private string password;
        private MySqlConnection connection;
        private List<string> rows = new List<string>();



        public MySqlConnector()
        {
            this.host = Settings.Default.db_host;
            this.database = Settings.Default.db_schema;
            this.user = Settings.Default.db_user;
            this.password = Settings.Default.db_pw;
            string myConnectionString = "SERVER=" + this.host + ";" +
                        "DATABASE=" + this.database + ";" +
                        "UID=" + this.user + ";" +
                        "PASSWORD=" + this.password + ";";

            connection = new MySqlConnection(myConnectionString);




        }

        public bool Connect()
        {
            if (connection.State != System.Data.ConnectionState.Open)
            {
                try
                {
                    connection.Open();
                    this.MessageReceived("Connected to Database");
                    return true;
                }
                catch (MySqlException ex)
                {
                    log.Error("failed to connect! mysql error: " + ex);
                    this.MessageReceived("No Database connection - Error: "+ex);
                    return false;
                }

            }
            else
            {
                this.MessageReceived("Connected to Database");
                return true;
            }

        }
        .
        .
        .
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用log4net;
使用MySql.Data.MySqlClient;
命名空间管道服务器
{
类MySqlConnector
{
//寄存器记录器
私有静态只读ILog log=LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
公共委托无效消息ReceivedHandler(字符串消息);
公共事件消息ReceivedHandler消息Received;
私有字符串主机;
私有字符串数据库;
私有字符串用户;
私有字符串密码;
私有MySqlConnection;
私有列表行=新列表();
公共MySqlConnector()
{
this.host=Settings.Default.db\u host;
this.database=Settings.Default.db_schema;
this.user=Settings.Default.db\u user;
this.password=Settings.Default.db_pw;
string myConnectionString=“SERVER=“+this.host+”;”+
“DATABASE=“+this.DATABASE+”;”+
“UID=“+this.user+”;”+
“PASSWORD=“+this.PASSWORD+”;”;
连接=新的MySqlConnection(myConnectionString);
}
公共布尔连接()
{
if(connection.State!=System.Data.ConnectionState.Open)
{
尝试
{
connection.Open();
此.MessageReceived(“已连接到数据库”);
返回true;
}
捕获(MySqlException-ex)
{
log.Error(“连接失败!mysql错误:+ex”);
此.MessageReceived(“无数据库连接-错误:+ex”);
返回false;
}
}
其他的
{
此.MessageReceived(“已连接到数据库”);
返回true;
}
}
.
.
.
}
}

任何提示都将不胜感激!提前谢谢

当你指的是永远,你指的是30岁左右

您可能正在寻找将超时设置添加到ConnectionString中