简体   繁体   English

如何从c#连接mysql?

[英]how to connect mysql from c#?

I downloaded mysql.dll . 我下载了mysql.dll When I add this to my solution, I get an error like 当我将此添加到我的解决方案时,我得到一个错误

Please make sure that file is accesssble and that it is a valid assembly or com component... 请确保该文件是可访问的,并且它是有效的程序集或com组件...

Please tell me how to connect to MySql from C# and any problems I may encounter while using MySql in C#. 请告诉我如何从C#连接到MySql以及在C#中使用MySql时可能遇到的任何问题。 Will it run on every system? 它会在每个系统上运行吗?

http://www.mysql.com/downloads/connector/net/ http://www.mysql.com/downloads/connector/net/

You can download MySql connector and use it 您可以下载MySql连接器并使用它

you should reference MySql.Data.dll, not mysql.dll 你应该引用MySql.Data.dll,而不是mysql.dll

To open database connection, you can use something like this 要打开数据库连接,您可以使用类似的东西

        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
        }
    }

About supported platforms, the documentation says: 关于支持的平台,文档说:

Connector/NET runs on any platform that supports the .NET framework. Connector / NET可在任何支持.NET框架的平台上运行。 The .NET framework is primarily supported on recent versions of Microsoft Windows, and is supported on Linux through the Open Source Mono framework .NET框架主要在最新版本的Microsoft Windows上受支持,并且在Linux上通过Open Source Mono框架支持

First you have to Add MySQL.data.dll into References 首先,您必须将MySQL.data.dll添加到参考中

Second, here is example code to connect to MySQL server and select a simple data from server 其次,这是连接到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);> } } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM