C#操作MySql数据库范例代码

2021-11-02 16:06:05 浏览数 (1)

首先到网上下载MySql.Data.MySqlClient.dll,然后添加引用到项目

代码语言:javascript复制
using MySql.Data.MySqlClient;

      string connectString = @"server=localhost;userid=root;password=xxx;database=sharejs";   /*由于我的数据库在本地就用localhost了,可以替换成IP地址*/

        MySqlConnection conn = new MySqlConnection(connectString);

        conn.Open ();

        MySqlCommand cmd = conn.CreateCommand();

        cmd.CommandText = "select * from info";

        cmd.CommandType = CommandType.Text;

        using (MySqlDataReader reader = cmd.ExecuteReader())

        {

            while (reader.Read())

              Console.WriteLine(reader[0].ToString());

        }</pre> 

0 人点赞