日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#操作DataReader类

發(fā)布時(shí)間:2025/3/17 C# 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#操作DataReader类 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、常用屬性

名稱說明
Depth獲取一個(gè)值,用于指示當(dāng)前行的嵌套深度
FieldCount獲取當(dāng)前行中的列數(shù)
HasRows獲取一個(gè)值,該值指示?SqlDataReader?是否有行
IsClosed指定的SqlDataReader?實(shí)例是否已關(guān)閉
Item[Int32]獲取指定列(數(shù)字索引),通常在While.Read()中使用
Item[String]獲取指定列(字符串索引),?通常在While.Read()中使用
RecordsAffected獲取執(zhí)行 T-SQL 語句所更改、插入或刪除的行數(shù)
VisibleFieldCount獲取?SqlDataReader?中未隱藏的字段的數(shù)目
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); //創(chuàng)建連接DbCommand cmd = conn.CreateCommand(); //創(chuàng)建SqlCommand對(duì)象cmd.CommandText = "SELECT * FROM `t_s_base_user` LIMIT 3";conn.Open(); //打開連接using (DbDataReader reader = cmd.ExecuteReader()){Console.WriteLine(reader.FieldCount); //2 獲取列數(shù)Console.WriteLine(reader.Depth); //0 嵌套深度Console.WriteLine(reader.HasRows); //true 是否包含行Console.WriteLine(reader.IsClosed); //false SqlDataReader是否關(guān)閉 Console.WriteLine(reader.RecordsAffected); //-1 執(zhí)行T-SQL語句所插入、修改、刪除的行數(shù)Console.WriteLine(reader.VisibleFieldCount); //2 未隱藏的字段數(shù)目(一共就兩列)while (reader.Read()){Console.WriteLine(reader["realname"]);//Console.WriteLine(reader[1]); 通過數(shù)字索引或字符串索引訪問 }}conn.Close(); //關(guān)閉連接 Console.ReadKey();}} } View Code

?

二、常用方法

名稱說明
Read()前進(jìn)到下一記錄,異步版本ReadAsync
GetString()返回指定類型的值,其他的都類似
NextResult()當(dāng)處理批處理的T-SQL語句時(shí),跳到下一結(jié)果,異步版本NextResultAsync
GetValue()獲得該列的值,返回object類型
GetValues()使用當(dāng)前列指來填充參數(shù)中的對(duì)象數(shù)組
Close()關(guān)閉 SqlDataReader 對(duì)象
using MySql.Data.MySqlClient; using System; using System.Data.Common; using System.Threading; using System.Threading.Tasks;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); //創(chuàng)建連接DbCommand cmd = conn.CreateCommand(); //創(chuàng)建SqlCommand對(duì)象cmd.CommandText = "SELECT username,realname FROM `t_s_base_user` LIMIT 3";conn.Open(); //打開連接//Console.WriteLine("======同步讀取開始=====");//NormalRead(cmd);//Console.WriteLine("======同步讀取接收====="); cmd.CommandText = "SELECT username,realname FROM `t_s_base_user`";CancellationTokenSource tokenSource = new CancellationTokenSource();CancellationToken cancellToken = tokenSource.Token;Console.WriteLine("======異步讀取開始=====");AsyncRead(cmd, cancellToken);Console.WriteLine("======異步讀取結(jié)束=====");Console.WriteLine("按下任意鍵即可取消任務(wù)!");Console.ReadKey();tokenSource.Cancel();Console.WriteLine("按下任意鍵即可關(guān)閉連接!");Console.ReadKey();conn.Close(); //關(guān)閉連接 }/// <summary>/// 正常讀取/// </summary>/// <param name="cmd"></param>static void NormalRead(DbCommand cmd){using (DbDataReader reader = cmd.ExecuteReader()){while (reader.Read()){Console.WriteLine("reader.IsDBNull={0}", reader.IsDBNull(1)); //是否是null值Console.WriteLine("reader.GetString={0}", reader.GetString(1)); //Get什么類型就返回什么類型,這沒啥好說的。 }Console.WriteLine("reader.GetName={0}", reader.GetName(1)); //realname 由數(shù)字獲得列名Console.WriteLine("reader.GetOrdinal={0}", reader.GetOrdinal("realname")); //1 由列名獲取其在reader中的數(shù)字索引if (reader.NextResult()){Console.WriteLine(reader.GetString(1));}}}async static void AsyncRead(DbCommand cmd, CancellationToken cancellToken){using (DbDataReader reader = cmd.ExecuteReader()){try{while (await reader.ReadAsync(cancellToken)){Console.WriteLine("reader.IsDBNull={0}", reader.IsDBNull(1)); //是否是null值Console.WriteLine("reader.GetString={0}", reader.GetString(1)); //Get什么類型就返回什么類型,這沒啥好說的。await Task.Delay(500);}}catch (TaskCanceledException canceleEx){Console.WriteLine("編輯被取消:{0}", canceleEx.Message);}catch (Exception ex){Console.WriteLine("遍歷報(bào)錯(cuò):{0}", ex.Message);}bool NextResult = await reader.NextResultAsync();if (NextResult){Console.WriteLine(reader.GetString(1));}}}} } View Code

?

轉(zhuǎn)載于:https://www.cnblogs.com/scmail81/p/9357635.html

總結(jié)

以上是生活随笔為你收集整理的C#操作DataReader类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。