1

我有一个正在为朋友制作的 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(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue) 在 System .Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstkey) 在 System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean use OdbcRules) 在 System.Data .Common.DbConnectionStringBuilder.set_ConnectionString(String value) 在 MySql.Data.MySqlClient.MySqlConnectionStringBuilder..ctor(string connStr) 在 MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString(String value) 在 MySql.Data.MySqlClient。MySqlConnection..ctor(String connectionString) at DBtest.Form1..ctor() 在 C:\Users\AlexMoreno\Dcouments\Visual Studio\2008\Projects\DBtest\Form1.cs:line 38

4

1 回答 1

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

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

另外,尝试注释掉DBInfo db = new DBInfo();. 无论如何,您似乎都没有使用它。

更新:

您的连接字符串中缺少一个=- 紧随其后Password。所以...

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

...应该:

connectionString = "Server=" + server + ";" + "Database=" + database + ";"
            + "Uid=" + uid + ";" + "Password=" + password + ";";
于 2013-05-12T02:08:27.263 回答