I am trying to connect to my remote MySQL server, using the following code. Can you tell me what I am doing wrong as the replacement of database connection info with variables doesn't seem to be working.
我正在尝试使用以下代码连接到我的远程MySQL服务器。你能告诉我我做错了什么,因为用变量替换数据库连接信息似乎不起作用。
using MySql.Data.MySqlClient;
string db_Server = "10.0.0.0";
string db_Name = "myDatabase";
string db_User = "myUser";
string db_Pass = "myPassword";
// Connection String
MySqlConnection myConnection = new MySqlConnection("server = {0}; database = {1}; uid = {2}; pwd = {3}", db_server, db_Name, db_User, db_Pass);
As a PHP developer, I prefer to use the code above instead of the deliberate casting below:
作为PHP开发人员,我更喜欢使用上面的代码而不是下面的故意转换:
MySqlConnection myConnection = new MySqlConnection("server=10.0.0.0; database=myDatabase; uid=myUser; pwd=myPassword");
But as you can see in this image, I am getting a lot of red squiggles: http://screencast.com/t/xlwoG9by
但正如你在这张图片中看到的那样,我得到了很多红色的曲线:http://screencast.com/t/xlwoG9by
3 个解决方案
#1
10
Order of your parameter is wrong, it should be:
您的参数顺序错误,应该是:
db_server, db_Name, db_User, db_Pass
Currently it is:
目前它是:
"server = {0}; database = {1}; uid = {2}; pwd = {3}"
db_Server db_User db_Pass db_Name
So your statement should be:
所以你的陈述应该是:
MySqlConnection myConnection = new MySqlConnection(string.Format(
"server = {0}; database = {1}; uid = {2}; pwd = {3}",
db_Server,db_Name, db_User, db_Pass));
EDIT: based on the comments and discussion, the error you are getting is that you are trying all the stuff at class level. You should have these lines inside a method and call that method where you need it. Something like:
编辑:基于评论和讨论,你得到的错误是你正在尝试所有的课程级别的东西。您应该在方法中包含这些行,并在需要的地方调用该方法。就像是:
class MyClass
{
string db_Server = "10.0.0.0";
string db_User = "myUser";
string db_Pass = "myPassword";
string db_Name = "myDatabase";
public MySqlConnection GetConnection()
{
MySqlConnection myConnection = new MySqlConnection(string.Format(
"server = {0}; database = {1}; uid = {2}; pwd = {3}",
db_Server, db_Name, db_User, db_Pass));
return myConnection;
}
}
#2
2
Would you mind clicking this link, it tackles all about the creation of connection string of MySql Server :)
你能不能点击这个链接,它解决了MySql Server连接字符串的创建问题:)
#3
1
MySqlConnection myConnection = new MySqlConnection(string.Format("server = {0}; database = {1}; uid = {2}; pwd = {3}", db_Server, db_User, db_Pass, db_Name))
string.Format() is missing
string.Format()缺失
#1
10
Order of your parameter is wrong, it should be:
您的参数顺序错误,应该是:
db_server, db_Name, db_User, db_Pass
Currently it is:
目前它是:
"server = {0}; database = {1}; uid = {2}; pwd = {3}"
db_Server db_User db_Pass db_Name
So your statement should be:
所以你的陈述应该是:
MySqlConnection myConnection = new MySqlConnection(string.Format(
"server = {0}; database = {1}; uid = {2}; pwd = {3}",
db_Server,db_Name, db_User, db_Pass));
EDIT: based on the comments and discussion, the error you are getting is that you are trying all the stuff at class level. You should have these lines inside a method and call that method where you need it. Something like:
编辑:基于评论和讨论,你得到的错误是你正在尝试所有的课程级别的东西。您应该在方法中包含这些行,并在需要的地方调用该方法。就像是:
class MyClass
{
string db_Server = "10.0.0.0";
string db_User = "myUser";
string db_Pass = "myPassword";
string db_Name = "myDatabase";
public MySqlConnection GetConnection()
{
MySqlConnection myConnection = new MySqlConnection(string.Format(
"server = {0}; database = {1}; uid = {2}; pwd = {3}",
db_Server, db_Name, db_User, db_Pass));
return myConnection;
}
}
#2
2
Would you mind clicking this link, it tackles all about the creation of connection string of MySql Server :)
你能不能点击这个链接,它解决了MySql Server连接字符串的创建问题:)
#3
1
MySqlConnection myConnection = new MySqlConnection(string.Format("server = {0}; database = {1}; uid = {2}; pwd = {3}", db_Server, db_User, db_Pass, db_Name))
string.Format() is missing
string.Format()缺失