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 松松
请按任意键继续. . .
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。