Windows上的MySQL在连接尝试时崩溃

时间:2013-05-12 01:56:12

标签: c# mysql database debugging crash

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

但是,当我从应用程序中注释掉信息时,它会再次运行,但只有应用程序启动,我再也无法连接了?

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

继承我测试的代码,应用程序不断崩溃。

我很茫然。

    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;
            }
        }
    }

更新:

System.ArgumentException:初始化字符串的格式不符合规范开始于索引54在System.Data.Common.DBConnectionOptions.GetKeyValuePair(字符串的connectionString,的Int32 currentPosition,StringBuilder的缓冲液,布尔useOdbcRules,字符串&安培;键名,字符串&安培;键值)在System.Data.Common.DbConnectionOptions.ParseInternal(哈希表parsetable,字符串的connectionString,布尔buildChain,哈希表同义词,布尔firstkey)在System.Data.Common.DbConnectionOptions..ctor(字符串的connectionString,哈希表同义词,布尔使用OdbcRules )在System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(字符串值)MySql.Data.MySqlClient.MySqlConnectionStringBuilder..ctor(串connStr)在MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString(字符串值)MySql.Data.MySqlClient .MySqlConnection..ctor(字符串的connectionString)在DBtest.Form1..ctor()在C:\用户\ AlexMoreno \ Dcouments \的Visual Studio \ 2008 \项目\ DBtest \ Form1.c s:第38行

1 个答案:

答案 0 :(得分:2)

围绕语句包装try / catch以打开连接,并获取可能被抛出和未处理的异常的详细信息:

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());
        }
    }
}

您可以通过其他方式输出错误的详细信息 - 例如记录它们,如果要在Visual Studio中调试应用程序,则将它们输出到Debug.Console;接受你的选择;你只需要现实检查异常是否未处理并获得其细节继续。

另外,请尝试发表评论DBInfo db = new DBInfo();。无论如何你似乎没有使用它。

<强>更新

您的连接字符串中缺少= - 就在Password之后。所以......

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

......应该是:

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