当前位置: 代码迷 >> 综合 >> C# ExecuteReader
  详细解决方案

C# ExecuteReader

热度:14   发布时间:2024-01-15 04:16:03.0
 class Program{static void Main(string[] args){//连接数据库using( MySqlConnection conn = new MySqlConnection("Server=localhost; Database=study; uid=root; pwd=root; Charset=utf8;") )//创建数据库对象using (MySqlCommand cmd = conn.CreateCommand()){conn.Open();cmd.CommandText = "SELECT * FROM t_customers";using (MySqlDataReader read = cmd.ExecuteReader()){while (read.Read())//读取每行数据
                    {//获取字段信息int id = read.GetInt32("Id");string name = read.GetString("Name");string age = read.GetString("Age");Console.WriteLine(id + name + age);}}}Console.ReadKey();}}