首页 > 代码库 > ADO.NET使用MySqlCommand实例获取数据

ADO.NET使用MySqlCommand实例获取数据

using MySql.Data.MySqlClient;using System;namespace ConsoleTest{    class Program    {        static string connectionString =            "server=localhost;user id=lqs2011;password=******;database=toucai";        static void Main(string[] args)        {            MySqlConnection connection = new MySqlConnection(connectionString);            connection.Open();            if (connection.State == System.Data.ConnectionState.Open)                Console.WriteLine("connection opened!");            MySqlCommand cmd = new MySqlCommand();            cmd.Connection = connection;            cmd.CommandText = "SELECT empID, empNickName FROM employee";            cmd.CommandType = System.Data.CommandType.Text;            MySqlDataReader dataReader = cmd.ExecuteReader();            if (dataReader.HasRows)                Console.WriteLine("get data successfully!");              while (dataReader.Read())                Console.WriteLine(dataReader.GetString(0)+ \t + dataReader.GetString(1));        }    }}
输出结果:

connection opened!
get data successfully!
MY001 兰瓜
MY002 小倩
MY003 松松
请按任意键继续. . .

 

 

ADO.NET使用MySqlCommand实例获取数据