0

我下载了mysql.dll。当我将此添加到我的解决方案中时,我收到一个错误,例如

请确保该文件是可访问的,并且它是有效的程序集或 com 组件...

请告诉我如何从 C# 连接到 MySql 以及我在 C# 中使用 MySql 时可能遇到的任何问题。它会在每个系统上运行吗?

4

3 回答 3

1

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

您可以下载 MySql 连接器并使用它

于 2012-11-08T07:05:45.520 回答
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 上得到支持,并通过 Open Source Mono 框架在 Linux 上得到支持

于 2012-11-08T07:19:14.270 回答
0

首先,您必须将 MySQL.data.dll 添加到 References

其次,这里是连接到 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);>              
            }
        }          
    }
于 2012-11-08T07:17:32.280 回答