我下载了mysql.dll
。当我将此添加到我的解决方案时,我收到类似
请确保该文件是可访问的,并且它是有效的程序集或com组件...
请告诉我如何从C#连接到MySql以及在C#中使用MySql时可能遇到的任何问题。它会在每个系统上运行吗?
答案 0 :(得分:1)
http://www.mysql.com/downloads/connector/net/
您可以下载MySql连接器并使用它
答案 1 :(得分:1)
你应该引用MySql.Data.dll,而不是mysql.dll
要打开数据库连接,您可以使用类似这样的内容
internal static MySqlConnection GetConnection(string dbserver, string username, string password, string databasename)
{
try
{
MySqlConnection conn = new MySqlConnection("server=" + dbserver + ";User Id=" + username + ";Password=" + password + ";Persist Security Info=True;database=" + databasename);
conn.Open();
return conn;
}
catch (Exception ex)
{
// Code to handle exception
}
}
关于支持的平台,文档说:
Connector / NET可在任何支持.NET框架的平台上运行。 最近的版本支持.NET框架 Microsoft Windows,并通过开源在Linux上受支持 单声道框架
答案 2 :(得分:0)
首先,您必须将MySQL.data.dll添加到参考
中其次,这是连接到MySQL服务器并从服务器
中选择简单数据的示例代码private string _server; private string _port; private string _database; private string _uid; private string _pwd; //Constructor public MySQL() { Initialize(); } //Initialize connection string private void Initialize() { _server = "10.11.12.13"; _database = "ABCDEF"; _port = "3306"; _uid = "root"; _pwd = "raat"; var connectionString = "SERVER=" + _server + ";" + "DATABASE=" + _database + ";" + "PORT=" + _port + ";" + "UID=" + _uid + ";" + "PASSWORD=" + _pwd + ";";> connection = new MySqlConnection(connectionString); } //Connect to server private bool OpenConnection() { try { if (connection.State != System.Data.ConnectionState.Open) connection.Open(); return true; } catch (MySqlException ex) { //When handling errors, you can your application's response based //on the error number. //The two most common error numbers when connecting are as follows: //0: Cannot connect to server. //1045: Invalid user name and/or password. switch (ex.Number) { case 0: Global._logger.Info("Cannot connect to server. Contact administrator"); break; case 1045: Global._logger.Info("Invalid username/password, please try again"); break; } Global._logger.Info(ex.Message + " / " + ex.StackTrace); return false; } } //Close connection private bool CloseConnection() { try { if (connection.State != System.Data.ConnectionState.Closed) connection.Close(); return true; } catch (MySqlException e) { Global._logger.Info(e.Message + " / " + e.StackTrace); return false; } } //Create query to select data from Server public string GetStudentName(string studentId) { var query = "SELECT * FROM Student WHERE StudentID = '" + studentId; if (OpenConnection()) { //Create command var cmd = new MySqlCommand(query, connection); //Create data reader and excute datareader MySqlDataReader dataReader = null;> try { dataReader = cmd.ExecuteReader(); } catch (MySqlException mySQLEx) { Global._logger.Error(mySQLEx); } string studentName = ""; try { if (dataReader != null) { while (dataReader.Read()) { studentName= Convert.ToInt32(dataReader["Name"].ToString()); } dataReader.Close(); CloseConnection(); return studentName; } } catch (Exception e) { Global._logger.Info(e.Message + e.Source);> } } }