在Windows上的MySQL崩溃连接尝试 [英] MySQL on Windows crashes on connection attempts

查看:293
本文介绍了在Windows上的MySQL崩溃连接尝试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows应用程序,我为一个朋友,但由于某种原因,当我尝试连接,该应用程序崩溃。一个消息框弹出并说DBTest hastopped工作,Windows正在检查解决方案的问题...5秒后,它关闭,应用程序不启动。



但是,当我注释掉从应用程序的信息,它再次工作,但只有应用程序启动,我不能再连接??



我检查了MySQL工作台上的连接,它让我连接到网站的数据库,我可以远程连接到网站数据库,但它不允许我这样做在应用程序。



遇到测试代码,应用程序会崩溃。



我在亏本。

  public partial class Form1:Form 
{
public MySqlConnection connection;
public Form1()
{

InitializeComponent();

DBInfo db = new DBInfo();

字符串服务器;
string database;
string uid;
string password;

server =XXX;
database =XXX;
uid =XXX;
password =XXX;
string connectionString;
connectionString =Server =+ server +; +Database =+ database +;
+Uid =+ uid +; +密码+密码+;;

connection = new MySqlConnection(connectionString);

try
{
connection.Open();
}
catch(MySqlException ex)
{
switch(ex.Number)
{
case 0:
MessageBox.Show(无法连接到服务器。
break;

case 1045:
MessageBox.Show(Invalid Username / Password,Please try again。
break;
}
}
}

UPDATE:



System.ArgumentException:初始化字符串的格式不符合从索引54开始的规范。System.Data.Common.DBConnectionOptions.GetKeyValuePair(String connectionString,Int32 currentPosition,StringBuilder buffer,在System.Data.Common.DbConnectionOptions.ctor(String)中的System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable,String connectionString,Boolean buildChain,Hashtable同义词,布尔firstkey)的布尔值useOdbcRules,String& keyname,String& keyvalue (String connStr)在MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString(String)中的System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)中的连接字符串(例如connectionString,Hashtable同义词,Boolean使用OdbcRules)值)在MySql.Data.MySqlClient.MySqlConnection..ctor(String connectionString)at DBtest.Form1..ctor()在C:\Users\AlexMoreno\Dcouments\Visual Studio\2008\Projects\ DBtest\Form1.cs:第38行

解决方案

在语句周围打开try / catch打开连接,可能被抛出和处理的异常的详细信息:

  public partial class Form1:Form 
{
public MySqlConnection connection;
public Form1()
{

InitializeComponent();

// DBInfo db = new DBInfo(); //会注释掉,因为你不使用它

string server;
string database;
string uid;
string password;

server =XXX;
database =XXX;
uid =XXX;
password =XXX;
string connectionString;
connectionString =Server =+ server +; +Database =+ database +;
+Uid =+ uid +; +密码+密码+;;

// connection = new MySqlConnection(connectionString); //不在这里 - 用于排除故障至少

try
{
connection = new MySqlConnection(connectionString); //从上面重新定位以进行故障诊断
connection.Open();
}
(Exception ex)//是,异常 - 直到你知道发生了什么。
{
MessageBox.Show(ex.ToString());
}
}
}

错误另一种方式 - 例如记录它们,如果你要在Visual Studio中调试应用程序,则将它们输出到 Debug.Console 拿你的选择



此外,尝试注释 DBInfo db = new DBInfo() );



您在连接字符串中缺少 = - 紧跟密码后面。所以...

  connectionString =Server =+ server +; +Database =+ database +; 
+Uid =+ uid +; +密码+密码+;;

...应为:

  connectionString =Server =+ server +; +Database =+ database +; 
+Uid =+ uid +; +Password =+密码+;;


I have a windows application that I am making for a friend, but for some reason, when I try to connect, the app crashes. a message box pops up and says "DBTest hastopped working, windows is checking for a solution to the problem..." after 5 seconds, it closes and the app doesn't launch.

However, when I comment out the info from the app, it works again, but only the app launches, and I can't connect anymore??

I checked the connection on MySQL workbench, and it lets me connect to the website's database, and I can connect to the sites DB remotely, but it will not allow me to do so in the application.

Heres the code that im testing, and the app keeps crashing.

I'm at a loss.

    public partial class Form1 : Form
{
    public MySqlConnection connection;
    public Form1()
    {

        InitializeComponent();

        DBInfo db = new DBInfo();

        string server;
        string database;
        string uid;
        string password;

        server = "XXX";
        database = "XXX";
        uid = "XXX";
        password = "XXX";
        string connectionString;
        connectionString = "Server=" + server + ";" + "Database=" + database + ";"
                + "Uid=" + uid + ";" + "Password" + password + ";";

        connection = new MySqlConnection(connectionString);

        try
        {
            connection.Open();
        }
        catch (MySqlException ex)
        {
            switch (ex.Number)
            {
                case 0:
                    MessageBox.Show("Cannot connect to Server. Contact Admin.");
                    break;

                case 1045:
                    MessageBox.Show("Invalid Username/Password, please try again.");
                    break;
            }
        }
    }

UPDATE:

System.ArgumentException: Format of the initialization string does not conform to specification starting at index 54. at System.Data.Common.DBConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue) at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstkey) at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean use OdbcRules) at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value) at MySql.Data.MySqlClient.MySqlConnectionStringBuilder..ctor(string connStr) at MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString(String value) at MySql.Data.MySqlClient.MySqlConnection..ctor(String connectionString) at DBtest.Form1..ctor() in C:\Users\AlexMoreno\Dcouments\Visual Studio\2008\Projects\DBtest\Form1.cs:line 38

解决方案

Wrap a try/catch around the statement to open the connection, and get the details of the exception that is likely being thrown and unhandled:

public partial class Form1 : Form
{
    public MySqlConnection connection;
    public Form1()
    {

        InitializeComponent();

        //DBInfo db = new DBInfo(); // would comment this out since you're not using it

        string server;
        string database;
        string uid;
        string password;

        server = "XXX";
        database = "XXX";
        uid = "XXX";
        password = "XXX";
        string connectionString;
        connectionString = "Server=" + server + ";" + "Database=" + database + ";"
                + "Uid=" + uid + ";" + "Password" + password + ";";

        //connection = new MySqlConnection(connectionString); // not here - for troubleshooting at least

        try
        {
            connection = new MySqlConnection(connectionString); // relocated from above for troubleshooting
            connection.Open();
        }
        (Exception ex) // Yes, Exception - until you know more about what is happening.
        {
            MessageBox.Show(ex.ToString());
        }
    }
}

You could output the details of the error another way - e.g. log them, output them to Debug.Console if you are going to debug the app in Visual Studio; take your pick; you just need to reality check that an exception is unhandled and get its details to proceed.

Also, try commenting out DBInfo db = new DBInfo();. You don't seem to be using it anyway.

UPDATE:

You are missing an = in your connection string - right after Password. So...

connectionString = "Server=" + server + ";" + "Database=" + database + ";"
            + "Uid=" + uid + ";" + "Password" + password + ";";

...should be:

connectionString = "Server=" + server + ";" + "Database=" + database + ";"
            + "Uid=" + uid + ";" + "Password=" + password + ";";

这篇关于在Windows上的MySQL崩溃连接尝试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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