开发者

How to connect to MySQL database through c#?

开发者 https://www.devze.com 2023-01-24 14:30 出处:网络
I read that I have to download t开发者_运维知识库his http://dev.mysql.com/downloads/mirror.php?id=13427#mirrors, but it says that I can\'t install it because I need .NET Framework. I already have 4.0?

I read that I have to download t开发者_运维知识库his http://dev.mysql.com/downloads/mirror.php?id=13427#mirrors, but it says that I can't install it because I need .NET Framework. I already have 4.0?!


Use this link, it will work if you have VS.NET 2010 http://dev.mysql.com/downloads/connector/net/


Installer checking for .Net 3.5 or 2.0 mabey =


You should use MySQL Connector/Net 6.3.5 available at mentioned location (http://dev.mysql.com/downloads/connector/net/)


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();
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消