一、常用属性
| 名称 | 说明 |
| Depth | 获取一个值,用于指示当前行的嵌套深度 |
| FieldCount | 获取当前行中的列数 |
| HasRows | 获取一个值,该值指示 SqlDataReader 是否有行 |
| IsClosed | 指定的SqlDataReader 实例是否已关闭 |
| Item[Int32] | 获取指定列(数字索引),通常在While.Read()中使用 |
| Item[String] | 获取指定列(字符串索引), 通常在While.Read()中使用 |
| RecordsAffected | 获取执行 T-SQL 语句所更改、插入或删除的行数 |
| VisibleFieldCount | 获取 SqlDataReader 中未隐藏的字段的数目 |
using MySql.Data.MySqlClient; using System; using System.Data.Common; namespace ConsoleApp { class Program { static void Main(string[] args) { string str = string.Format("Server={0};Port={1};Database={2};Uid={3};Pwd={4};", "localhost",3306, "wisdompurchase","root","1234"); DbConnection conn = new MySqlConnection(str); //创建连接 DbCommand cmd = conn.CreateCommand(); //创建SqlCommand对象 cmd.CommandText = "SELECT * FROM `t_s_base_user` LIMIT 3"; conn.Open(); //打开连接 using (DbDataReader reader = cmd.ExecuteReader()) { Console.WriteLine(reader.FieldCount); //2 获取列数 Console.WriteLine(reader.Depth); //0 嵌套深度 Console.WriteLine(reader.HasRows); //true 是否包含行 Console.WriteLine(reader.IsClosed); //false SqlDataReader是否关闭 Console.WriteLine(reader.RecordsAffected); //-1 执行T-SQL语句所插入、修改、删除的行数 Console.WriteLine(reader.VisibleFieldCount); //2 未隐藏的字段数目(一共就两列) while (reader.Read()) { Console.WriteLine(reader["realname"]); //Console.WriteLine(reader[1]); 通过数字索引或字符串索引访问 } } conn.Close(); //关闭连接 Console.ReadKey(); } } }