最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c# SqlDataAdapter中的Fill是怎么實現(xiàn)的

 更新時間:2020年07月22日 10:02:41   作者:一線碼農(nóng)  
這篇文章主要介紹了c# SqlDataAdapter中的Fill是怎么實現(xiàn)的,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下

1. 講故事

最近因為各方面原因換了一份工作,去了一家主營物聯(lián)柜的公司,有意思的是物聯(lián)柜上的終端是用 wpf 寫的,代碼也算是年久失修,感覺技術(shù)債還是蠻重的,前幾天在調(diào)試一個bug的時候,看到了一段類似這樣的代碼:

  var dt = new DataTable();
  SqlDataAdapter adapter = new SqlDataAdapter(new SqlCommand());
  adapter.Fill(dt);

是不是很眼熟哈,或許你也已經(jīng)多年不見了,猶記得那時候為了能從數(shù)據(jù)庫獲取數(shù)據(jù),第一種方法就是采用 SqlDataReader 一行一行從數(shù)據(jù)庫讀取,而且還要操心 Reader 的 close 問題,第二種方法為了避免麻煩,就直接使用了本篇說到的 SqlDataAdapter ,簡單粗暴,啥也不用操心,對了,不知道您是否和我一樣對這個 Fill 方法很好奇呢?,它是如何將數(shù)據(jù)塞入到 DataTable 中的呢? 也是用的 SqlDataReader 嗎? 而且 Fill 還有好幾個擴展方法,哈哈,本篇就逐個聊一聊,就當(dāng)回顧經(jīng)典啦!

二:對Fill方法的探究

1. 使用 dnspy 查看Fill源碼

dnspy小工具大家可以到GitHub上面去下載一下,這里就不具體說啦,接下來追一下Fill的最上層實現(xiàn),如下代碼:

public int Fill(DataTable dataTable)
{
	IntPtr intPtr;
		Bid.ScopeEnter(out intPtr, "<comm.DbDataAdapter.Fill|API> %d#, dataTable\n", base.ObjectID);
		int result;
		try
		{
			DataTable[] dataTables = new DataTable[]
			    {
					dataTable
				};
				IDbCommand selectCommand = this._IDbDataAdapter.SelectCommand;
				CommandBehavior fillCommandBehavior = this.FillCommandBehavior;
				result = this.Fill(dataTables, 0, 0, selectCommand, fillCommandBehavior);
			}
			finally
			{
				Bid.ScopeLeave(ref intPtr);
			}
			return result;
		}

上面的代碼比較關(guān)鍵的一個地方就是 IDbCommand selectCommand = this._IDbDataAdapter.SelectCommand; 這里的 SelectCommand 來自于哪里呢? 來自于你 new SqlDataAdapter 的時候塞入的構(gòu)造函數(shù) SqlCommand,如下代碼:

public SqlDataAdapter(SqlCommand selectCommand) : this()
	{
		this.SelectCommand = selectCommand;
		                  }

然后繼續(xù)往下看 this.Fill 方法,代碼簡化后如下:

		protected virtual int Fill(DataTable[] dataTables, int startRecord, int maxRecords, IDbCommand command, CommandBehavior behavior)
		{
      result = this.FillInternal(null, dataTables, startRecord, maxRecords, null, command, behavior);
			
			return result;
		}

上面這段代碼沒啥好說的,繼續(xù)往下追蹤 this.FillInternal 方法,簡化后如下:

		private int FillInternal(DataSet dataset, DataTable[] datatables, int startRecord, int maxRecords, string srcTable, IDbCommand command, CommandBehavior behavior)
		{
			int result = 0;
			try
			{
				IDbConnection connection = DbDataAdapter.GetConnection3(this, command, "Fill");
				try
				{
					IDataReader dataReader = null;
					try
					{
						dataReader = command.ExecuteReader(behavior);
						result = this.Fill(datatables, dataReader, startRecord, maxRecords);
					}
					finally
					{
						if (dataReader != null)	dataReader.Dispose();
					}
				}
				finally
				{
					DbDataAdapter.QuietClose(connection, originalState);
				}
			}
			finally
			{
				if (flag)
				{
					command.Transaction = null;
					command.Connection = null;
				}
			}
			return result;
		}

大家可以仔細(xì)研讀一下上面的代碼,挺有意思的,至少你可以獲取以下兩點信息:

  • 從各個 finally 中可以看到,當(dāng)數(shù)據(jù) fill 到 datatable 中之后,操作數(shù)據(jù)庫的幾大對象 Connection,Transaction,DataReader 都會進行關(guān)閉,你根本不需要操心。
  • this.Fill(datatables, dataReader, startRecord, maxRecords) 中可以看到,底層不出意外也是通過 dataReader.read() 一行一行讀取然后塞到 DataTable中去的,不然它拿這個 dataReader 干嘛呢? 不信的話可以繼續(xù)往下追。
		protected virtual int Fill(DataTable[] dataTables, IDataReader dataReader, int startRecord, int maxRecords)
		{
			try
			{
				int num = 0;
				bool flag = false;
				DataSet dataSet = dataTables[0].DataSet;
				int num2 = 0;
				while (num2 < dataTables.Length && !dataReader.IsClosed)
				{
					DataReaderContainer dataReaderContainer = DataReaderContainer.Create(dataReader, this.ReturnProviderSpecificTypes);
					if (num2 == 0)
					{
						bool flag2;
						do
						{
							flag2 = this.FillNextResult(dataReaderContainer);
						}
						while (flag2 && dataReaderContainer.FieldCount <= 0);	
						}
					}
				result = num;
			}
			return result;
		}

從上面代碼可以看到, dataReader 被封裝到了 DataReaderContainer 中,用 FillNextResult 判斷是否還有批語句sql,從而方便生成多個 datatable 對象,最后就是填充 DataTable ,當(dāng)然就是用 dataReader.Read()啦,不信你可以一直往里面追嘛,如下代碼:

		private int FillLoadDataRow(SchemaMapping mapping)
		{
			int num = 0;
			DataReaderContainer dataReader = mapping.DataReader;
			
			while (dataReader.Read())
			{
				mapping.LoadDataRow();
				num++;
			}
			return num;
		}

到這里你應(yīng)該意識到: DataReader 的性能肯定比 Fill 到 DataTable 要高的太多,所以它和靈活性兩者之間看您取舍了哈。

二:Fill 的其他重載方法

剛才給大家介紹的是帶有 DataTable 參數(shù)的重載,其實除了這個還有另外四種重載方法,如下圖:

	public override int Fill(DataSet dataSet);
	public int Fill(DataSet dataSet, string srcTable);
	public int Fill(DataSet dataSet, int startRecord, int maxRecords, string srcTable);
	public int Fill(int startRecord, int maxRecords, params DataTable[] dataTables);

1. startRecord 和 maxRecords

從字面意思看就是想從指定的位置 (startRecord) 開始讀,然后最多讀取 maxRecords 條記錄,很好理解,我們知道 reader() 是只讀向前的,然后一起看一下源碼底層是怎么實現(xiàn)的。

從上圖中可以看出,還是很簡單的哈,踢掉 startRecord 個 reader(),然后再只讀向前獲取最多 maxRecords 條記錄。

2. dataSet 和 srcTable

這里的 srcTable 是什么意思呢? 從 vs 中看是這樣的: The name of the source table to use for table mapping. 乍一看也不是特別清楚,沒關(guān)系,我們直接看源碼就好啦,反正我也沒測試,嘿嘿。

從上圖中你應(yīng)該明白大概意思就是給你 dataset 中的 datatable 取名字,比如:name= 學(xué)生表, 那么database中的的 tablename依次是: 學(xué)生表,學(xué)生表1,學(xué)生表2 ..., 這樣你就可以索引獲取表的名字了哈,如下代碼所示:

    DataSet dataSet = new DataSet();
    dataSet.Tables.Add(new DataTable("學(xué)生表"));
    var tb = dataSet.Tables["學(xué)生表"];

四:總結(jié)

本篇就聊這么多吧,算是解了多年之前我的一個好奇心,希望本篇對您有幫助。

以上就是c# SqlDataAdapter中的Fill是怎么實現(xiàn)的的詳細(xì)內(nèi)容,更多關(guān)于c# SqlDataAdapter的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#圖片處理如何生成縮略圖的實現(xiàn)

    C#圖片處理如何生成縮略圖的實現(xiàn)

    本文主要介紹了C#圖片處理如何生成縮略圖的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • 詳解C#對XML、JSON等格式的解析

    詳解C#對XML、JSON等格式的解析

    這篇文章主要介紹了詳解C#對XML、JSON等格式的解析,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2016-12-12
  • C#實現(xiàn)文件篩選讀取并翻譯的自動化工具

    C#實現(xiàn)文件篩選讀取并翻譯的自動化工具

    這篇文章主要為大家詳細(xì)介紹了如何利用C#實現(xiàn)文件篩選及讀取內(nèi)容,并翻譯的自動化工具,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-03-03
  • c# 網(wǎng)絡(luò)編程之tcp

    c# 網(wǎng)絡(luò)編程之tcp

    這篇文章主要介紹了c# 網(wǎng)絡(luò)編程之tcp的的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-02-02
  • C# DatagridView常用操作匯總

    C# DatagridView常用操作匯總

    這篇文章主要介紹了C# DatagridView常用操作匯總,羅列了一些常用的用法與技巧,需要的朋友可以參考下
    2014-07-07
  • C#中Response.Write常見問題匯總

    C#中Response.Write常見問題匯總

    這篇文章主要介紹了C#中Response.Write常見問題匯總,總結(jié)了C#中Response.Write的常用技巧,非常實用,需要的朋友可以參考下
    2014-09-09
  • C#泛型集合類型實現(xiàn)添加和遍歷

    C#泛型集合類型實現(xiàn)添加和遍歷

    這篇文章介紹了C#泛型集合類型實現(xiàn)添加和遍歷的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-08-08
  • C#實現(xiàn)List.Sort()使用小計

    C#實現(xiàn)List.Sort()使用小計

    在C#開發(fā)中,List是常見的一種集合類型,其提供了一個 Sort() 方法來實現(xiàn)對集合的排序,本文主要介紹了C#實現(xiàn)List.Sort()使用小計,具有一定的參考價值,感興趣的可以了解一下
    2023-12-12
  • C#定時任務(wù)框架Quartz.NET介紹與用法

    C#定時任務(wù)框架Quartz.NET介紹與用法

    這篇文章介紹了C#定時任務(wù)框架Quartz.NET的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-02-02
  • C#網(wǎng)絡(luò)編程之Socket編程

    C#網(wǎng)絡(luò)編程之Socket編程

    本文詳細(xì)講解了C#網(wǎng)絡(luò)編程的Socket編程,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-02-02

最新評論

兴城市| 汉阴县| 宁陵县| 双江| 城步| 尉犁县| 邻水| 农安县| 板桥市| 丹凤县| 中牟县| 利辛县| 香河县| 温州市| 武陟县| 清河县| 偃师市| 黔江区| 江口县| 湟源县| 门头沟区| 永善县| 松阳县| 诏安县| 唐山市| 山西省| 呼和浩特市| 南充市| 清远市| 江油市| 卢湾区| 乐东| 安平县| 岚皋县| 平果县| 犍为县| 鹤岗市| 封丘县| 濮阳县| 祁连县| 陆良县|