app连接mysql 太快 崩溃,在Windows上的MySQL崩溃连接尝试

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 + ";";

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值