简体   繁体   English

如何通过C#连接到MySQL数据库?

[英]How to connect to MySQL database through c#?

I read that I have to download this http://dev.mysql.com/downloads/mirror.php?id=13427#mirrors , but it says that I can't install it because I need .NET Framework. 我读到我必须下载此http://dev.mysql.com/downloads/mirror.php?id=13427#mirrors ,但是它说我无法安装它,因为我需要.NET Framework。 I already have 4.0?! 我已经有4.0吗?!

使用此链接,如果您具有VS.NET 2010 http://dev.mysql.com/downloads/connector/net/ ,该链接将起作用

安装程序正在检查.Net 3.5或2.0 mabey =

您应该使用上述位置(http://dev.mysql.com/downloads/connector/net/)上可用的MySQL Connector / Net 6.3.5。

I pieced this together by copy/paste from an existing project then sanitizing it...so it's not been compiled and tested, but you get the idea. 我通过复制/粘贴来自现有项目的方式将其拼凑在一起,然后对其进行清理...因此尚未对其进行编译和测试,但是您明白了。 So here's some sample code to get you started: 因此,这里有一些示例代码可以帮助您入门:

using MySql.Data.Types;
using MySql.Data.MySqlClient;    

private void Function()
    {
                    //Set up connection, SqlHost/etc are classwide and declared elsewhere:
                    MySql connection = new MySqlConnection("SERVER=" + SqlHost + ";DATABASE=" + DatabaseName + ";UID=" + user + ";PASSWORD=" + password + ";pooling=false");

                    //Setup query:
                    MySqlCommand command = connection.CreateCommand();
                    MySqlDataReader Reader;    
                    command.CommandText = "your query here";

                    //Connect to relation system and execute query:
                    connection.Open();
                    Reader = command.ExecuteReader();
                    while(Reader.Read())
                    {
                        MessageBox.Show("here's a row from the query response: " + Reader[0].ToString());
                    }

                    //Clean up:
                    connection.Close();
                    Reader.Close();
    }

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

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