成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

一個(gè)有趣的問(wèn)題,你知道SqlDataAdapter中的Fill是怎么實(shí)現(xiàn)的嗎

數(shù)據(jù)庫(kù) 其他數(shù)據(jù)庫(kù)
如何將數(shù)據(jù)塞入到 DataTable? 中的呢?也是用的 SqlDataReader 嗎?而且 Fill 還有好幾個(gè)擴(kuò)展方法,哈哈,本篇就逐個(gè)聊一聊,就當(dāng)回顧經(jīng)典啦!

一、背景

講故事

最近因?yàn)楦鞣矫嬖驌Q了一份工作,去了一家主營(yíng)物聯(lián)柜的公司,有意思的是物聯(lián)柜上的終端是用 wpf 寫(xiě)的,代碼也算是年久失修,感覺(jué)技術(shù)債還是蠻重的,前幾天在調(diào)試一個(gè)bug的時(shí)候,看到了一段類(lèi)似這樣的代碼:

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

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

二、對(duì)Fill方法的探究

1. 使用 dnspy 查看Fill源碼

dnspy小工具大家可以到GitHub上面去下載一下,這里就不具體說(shuō)啦,接下來(lái)追一下Fill的最上層實(shí)現(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)鍵的一個(gè)地方就是 IDbCommand selectCommand = this._IDbDataAdapter.SelectCommand; 這里的 SelectCommand 來(lái)自于哪里呢?來(lái)自于你 new SqlDataAdapter 的時(shí)候塞入的構(gòu)造函數(shù) SqlCommand,如下代碼:

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

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

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;
        }

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

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ì)研讀一下上面的代碼,挺有意思的,至少你可以獲取以下兩點(diǎn)信息:

  • 從各個(gè) finally 中可以看到,當(dāng)數(shù)據(jù) fill 到 datatable 中之后,操作數(shù)據(jù)庫(kù)的幾大對(duì)象 Connection,Transaction,DataReader 都會(huì)進(jìn)行關(guān)閉,你根本不需要操心。
  • this.Fill(datatables, dataReader, startRecord, maxRecords) 中可以看到,底層不出意外也是通過(guò) dataReader.read() 一行一行讀取然后塞到 DataTable中去的,不然它拿這個(gè) dataReader 干嘛呢?不信的話(huà)可以繼續(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 判斷是否還有批語(yǔ)句sql,從而方便生成多個(gè) datatable 對(duì)象,最后就是填充 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)該意識(shí)到:DataReader 的性能肯定比 Fill 到 DataTable 要高的太多,所以它和靈活性?xún)烧咧g看您取舍了哈。

三、Fill 的其他重載方法

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

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) 開(kāi)始讀,然后最多讀取 maxRecords 條記錄,很好理解,我們知道 reader() 是只讀向前的,然后一起看一下源碼底層是怎么實(shí)現(xiàn)的。

圖片圖片

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

2. dataSet 和 srcTable

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

圖片圖片

從上圖中你應(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é)

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

責(zé)任編輯:武曉燕 來(lái)源: 一線碼農(nóng)聊技術(shù)
相關(guān)推薦

2020-10-16 15:06:59

開(kāi)發(fā)技術(shù)方案

2021-10-14 06:52:47

算法校驗(yàn)碼結(jié)構(gòu)

2024-11-26 00:45:29

free區(qū)域字段

2020-04-08 08:35:20

JavaScript模塊函數(shù)

2022-05-09 10:47:08

登錄SpringSecurity

2013-02-27 10:27:44

GitHub

2024-12-04 08:40:19

2024-02-19 00:00:00

Docker輕量級(jí)容器

2023-04-28 07:44:44

MyBatis查詢(xún)SQL

2021-01-21 09:09:18

時(shí)區(qū)轉(zhuǎn)換程序

2024-02-22 09:21:09

.NETActionOptions

2024-01-08 08:45:07

Spring容器Bean

2010-08-18 08:53:53

Scala

2022-06-29 08:32:04

游標(biāo)MySQL服務(wù)器

2022-09-22 14:55:31

前端JavaScripthis

2022-09-26 13:10:17

JavaScriptthis

2020-12-08 09:25:41

死鎖MySQL數(shù)據(jù)庫(kù)

2024-12-11 08:19:34

2023-04-27 08:42:50

效果

2023-11-26 18:35:25

Python編程語(yǔ)言
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 欧美一级欧美一级在线播放 | 国产精品综合一区二区 | 亚洲成人自拍 | 国产精品久久国产精品99 | 三区在线观看 | 午夜免费网站 | 欧美一区二区在线 | 超碰在线免费av | 中文字幕国产第一页 | 免费av观看 | 国产精品欧美精品日韩精品 | 精品自拍视频在线观看 | 一区二区三区中文字幕 | 精品一区久久 | 国产在线视频一区 | 午夜欧美一区二区三区在线播放 | 亚洲精品不卡 | 一区二区三区电影网 | 精品国产91| 午夜天堂精品久久久久 | 成人免费观看男女羞羞视频 | 国产成人99av超碰超爽 | 一区二区av | 国产精品视频不卡 | 成人二区| 久久久久亚洲视频 | 午夜私人影院在线观看 | 国产精品美女久久久久久免费 | 日日日日日日bbbbb视频 | 欧美一区二区三区精品免费 | 久草在线青青草 | 久久久久久国产精品mv | 色综久久 | 亚洲欧美日韩中文字幕一区二区三区 | 蜜臀网站 | 夜夜爽99久久国产综合精品女不卡 | 国产精品视屏 | 毛片电影 | 日韩欧美成人一区二区三区 | 精品网站999| 91成人在线|