C#访问MYSQL的问题
using MySql.Data.MySqlClient;
string myConnectionString = "Database=zttest;Data Source=192.168.0.2;User Id=root;Password= ";
MySqlConnection mycn = new MySqlConnection(myConnectionString);
mycn.Open();
MySqlCommand mycm = new MySqlCommand( "select * from users ",mycn);
MySqlDataReader msdr = mycm.ExecuteReader();
while(msdr.Read())
{
(msdr.HasRows)
{
Console.WriteLine(msdr.GetString(0));
}
}
msdr.Close();
mycn.Close();
代码如上,访问的时候总是出下面的提示,请问是怎么回事
由于套接字没有连接并且(当使用一个 sendto 调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受。
错在这一行,mycn.Open();
------解决方案--------------------是不是string myConnectionString 格式不正确?
另外可以尝试用ODBC访问
------解决方案--------------------MySQL ODBC connection strings
Open connection to local MySQL database using MySQL ODBC 3.51 Driver
"Provider=MSDASQL; DRIVER={MySQL ODBC 3.51Driver}; SERVER= localhost; DATABASE=Your_MySQL_Database; UID= Your_Username; PASSWORD=Your_Password; OPTION=3 "
MySQL OLE DB & OleDbConnection (.NET framework) connection strings
Open connection to MySQL database:
"Provider=MySQLProv;Data Source=Your_MySQL_Database;User Id=Your_Username; Password=Your_Password; "
------解决方案--------------------MySQL 连接字符串..
> > ODBC
1.Local database:
"Driver={mySQL};Server=mySrvName;Option=16834;Database=mydatabase; "
2.Remote database:
"Driver={mySQL};Server=data.domain.com;Port=3306;Option=131072;Stmt=;Database=my-database;Uid=username;Pwd=password; "
> > OLE DB, OleDbConnection (.NET)
1.Standard:
"Provider=MySQLProv;Data Source=mydb;User Id=UserName;Password=asdasd; "
> > MySqlConnection (.NET)
eInfoDesigns.dbProvider:
"DataSource=server;Database= mydb;UserID=username;Password=pwd;Command Logging=false "
Declare the MySqlConnection:
C#:
using eInfoDesigns.dbProvider.MySqlClient;
MySqlConnection oMySqlConn = new MySqlConnection();
oMySqlConn.ConnectionString = "my connectionstring ";
oMySqlConn.Open();
> > SevenObjects MySqlClient (.NET)
1.Standard:
"Host=server; UserName= myusername;Password=mypassword;Database=mydb; "
> > Core Labs MySQLDirect (.NET)
1.Standard:
"User ID=root; Password=pwd; Host=localhost; Port= 3306;Database=test;Direct=true; Protocol=TCP; Compress=false; Pooling=true; Min Pool Size=0;Max Pool Size=100; Connection Lifetime=0 "