久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <legend id='4PYoS'><style id='4PYoS'><dir id='4PYoS'><q id='4PYoS'></q></dir></style></legend>
      <bdo id='4PYoS'></bdo><ul id='4PYoS'></ul>
  1. <tfoot id='4PYoS'></tfoot>

    1. <small id='4PYoS'></small><noframes id='4PYoS'>

      <i id='4PYoS'><tr id='4PYoS'><dt id='4PYoS'><q id='4PYoS'><span id='4PYoS'><b id='4PYoS'><form id='4PYoS'><ins id='4PYoS'></ins><ul id='4PYoS'></ul><sub id='4PYoS'></sub></form><legend id='4PYoS'></legend><bdo id='4PYoS'><pre id='4PYoS'><center id='4PYoS'></center></pre></bdo></b><th id='4PYoS'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='4PYoS'><tfoot id='4PYoS'></tfoot><dl id='4PYoS'><fieldset id='4PYoS'></fieldset></dl></div>

      為什么我需要手動打開這個 .NET Core 數據庫連接,

      Why do I need to manually open this .NET Core database connection when usually Dapper auto opens it?(為什么我需要手動打開這個 .NET Core 數據庫連接,而通常 Dapper 會自動打開它?) - IT屋-程序員軟件開發技術分享
    2. <i id='JZxz2'><tr id='JZxz2'><dt id='JZxz2'><q id='JZxz2'><span id='JZxz2'><b id='JZxz2'><form id='JZxz2'><ins id='JZxz2'></ins><ul id='JZxz2'></ul><sub id='JZxz2'></sub></form><legend id='JZxz2'></legend><bdo id='JZxz2'><pre id='JZxz2'><center id='JZxz2'></center></pre></bdo></b><th id='JZxz2'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='JZxz2'><tfoot id='JZxz2'></tfoot><dl id='JZxz2'><fieldset id='JZxz2'></fieldset></dl></div>
          <tbody id='JZxz2'></tbody>
          <legend id='JZxz2'><style id='JZxz2'><dir id='JZxz2'><q id='JZxz2'></q></dir></style></legend>
            <bdo id='JZxz2'></bdo><ul id='JZxz2'></ul>
            <tfoot id='JZxz2'></tfoot>

            <small id='JZxz2'></small><noframes id='JZxz2'>

                本文介紹了為什么我需要手動打開這個 .NET Core 數據庫連接,而通常 Dapper 會自動打開它?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                限時送ChatGPT賬號..

                我正在嘗試使用 async/await 將一些記錄插入到數據庫中.如果我不手動打開連接,我會收到 Exception.如果我添加一些代碼來打開連接,那么一切正常.

                這是當前代碼(拋出異常):

                using (var connection = new SqlConnection(ConnectionString)){var tasks = new List();sellUrls.ForEach(url => tasks.Add(InsertUrlAsync(connection, url)));rentUrls.ForEach(url => tasks.Add(InsertUrlAsync(connection, url)));等待 Task.WhenAll(tasks);}...私有異步任務 InsertUrlAsync(IDbConnection 連接,字符串 url){const string query = "INSERT INTO ..";return await connection.ExecuteAsync(query, new { .. });}

                異常:

                <塊引用>

                消息:System.InvalidOperationException:操作無效.連接已關閉.

                但是當我將代碼更改為以下內容時,它起作用了:

                var tasks = new List();等待 connection.OpenAsync();sellUrls.ForEach(....) .. 等等...

                類似的問題:

                • Task.WhenAll,連接正在關閉
                • SqlConnection 在 using 語句中意外關閉

                解決方案

                Dapper 為您打開連接;但它也會在工作完成后關閉它.

                現在,您正在使用一個連接運行兩個獨立的 async 任務.

                <塊引用>

                soldUrls.ForEach(url => tasks.Add(InsertUrlAsync(connection, url)));rentUrls.ForEach(url => tasks.Add(InsertUrlAsync(connection, url)));

                兩個任務同時運行.當一項任務的工作完成時,它關閉連接.但是其他任務仍在運行,它不再有權訪問打開的連接,因此出現了您提到的異常.

                如您所說,如果您自己打開連接,Dapper 不會關閉它,一切正常.

                順便說一下,在并發使用連接實例時,可能會出現意想不到的問題.請參閱此問題了解更多詳情.

                I'm trying to use async/await to insert a number of records into a database. If I don't manually open the connection, I get an Exception. If I add some code to open the connection, then everything works ok.

                Here's the current code (which throws an Exception):

                using (var connection = new SqlConnection(ConnectionString))
                {
                    var tasks = new List<Task>();
                    soldUrls.ForEach(url => tasks.Add(InsertUrlAsync(connection, url))); 
                    rentUrls.ForEach(url => tasks.Add(InsertUrlAsync(connection, url)));
                    await Task.WhenAll(tasks);
                }
                
                ...
                
                private async Task InsertUrlAsync(IDbConnection connection, string url)
                {
                    const string query = "INSERT INTO ..";
                    return await connection.ExecuteAsync(query, new { .. });
                }
                

                the exception:

                Message: System.InvalidOperationException : Invalid operation. The connection is closed.

                but when I change the code to the following, it works:

                var tasks = new List<Task>();
                await connection.OpenAsync();
                soldUrls.ForEach(....) .. etc ... 
                
                

                Similar SO questions:

                • Task.WhenAll, connection is closing
                • SqlConnection closes unexpectedly inside using statement

                解決方案

                Dapper opens the connection for you; but it also closes it when work is done.

                Now, you are running two independent async tasks using one single connection.

                soldUrls.ForEach(url => tasks.Add(InsertUrlAsync(connection, url))); 
                rentUrls.ForEach(url => tasks.Add(InsertUrlAsync(connection, url)));
                

                Both the tasks are running simultaneously. When work of one task finish, it closes the connection. But other task is still running which does not have access to open connection anymore and hence the exception you mentioned in question.

                As you said, if you open the connection yourself, Dapper does not close it and everything just works fine.

                By the way, while using connection instance concurrently, there may be unexpected issues. Please refer to this question for more details.

                這篇關于為什么我需要手動打開這個 .NET Core 數據庫連接,而通常 Dapper 會自動打開它?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                Can I figure out a list of databases and the space used by SQL Server instances without writing SQL queries?(我可以在不編寫 SQL 查詢的情況下找出數據庫列表和 SQL Server 實例使用的空間嗎?) - IT屋-程序員軟件開發
                How to create a login to a SQL Server instance?(如何創建對 SQL Server 實例的登錄?)
                How to know the version and edition of SQL Server through registry search(如何通過注冊表搜索知道SQL Server的版本和版本)
                Why do I get a quot;data type conversion errorquot; with ExecuteNonQuery()?(為什么會出現“數據類型轉換錯誤?使用 ExecuteNonQuery()?)
                How to show an image from a DataGridView to a PictureBox?(如何將 DataGridView 中的圖像顯示到 PictureBox?)
                WinForms application design - moving documents from SQL Server to file storage(WinForms 應用程序設計——將文檔從 SQL Server 移動到文件存儲)
                  <bdo id='lwwTb'></bdo><ul id='lwwTb'></ul>
                      <tbody id='lwwTb'></tbody>
                    • <i id='lwwTb'><tr id='lwwTb'><dt id='lwwTb'><q id='lwwTb'><span id='lwwTb'><b id='lwwTb'><form id='lwwTb'><ins id='lwwTb'></ins><ul id='lwwTb'></ul><sub id='lwwTb'></sub></form><legend id='lwwTb'></legend><bdo id='lwwTb'><pre id='lwwTb'><center id='lwwTb'></center></pre></bdo></b><th id='lwwTb'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='lwwTb'><tfoot id='lwwTb'></tfoot><dl id='lwwTb'><fieldset id='lwwTb'></fieldset></dl></div>

                        <small id='lwwTb'></small><noframes id='lwwTb'>

                          <tfoot id='lwwTb'></tfoot>

                        1. <legend id='lwwTb'><style id='lwwTb'><dir id='lwwTb'><q id='lwwTb'></q></dir></style></legend>
                          主站蜘蛛池模板: 久久亚洲精品国产精品紫薇 | 午夜影院官网 | 在线观看三级av | 免费超碰| 91婷婷韩国欧美一区二区 | 三级视频网站 | 国产精品99久久久精品免费观看 | 韩日一区二区三区 | www亚洲精品 | 自拍视频在线观看 | 久久综合九色综合欧美狠狠 | 爱爱视频在线观看 | 国产精品久久久久久一级毛片 | 免费在线观看av网址 | 欧美日韩电影一区二区 | 久久久国产一区二区三区 | 国产在线精品一区二区 | 欧美大片在线观看 | 蜜臀网| 99一区二区 | 久久久久国产一区二区三区不卡 | 国产黄视频在线播放 | 国产精品1 | 一级全黄少妇性色生活免费看 | 国产精品久久久久久亚洲调教 | 日韩成人免费视频 | 毛片a级毛片免费播放100 | 99精品网站 | h视频免费在线观看 | av在线一区二区三区 | 欧美中文字幕一区二区三区亚洲 | 国产精品国产三级国产aⅴ中文 | 国产精品久久一区 | 国产精品一区在线观看 | 亚洲高清一区二区三区 | 老司机久久 | 欧美国产一区二区三区 | 亚洲经典一区 | 国产精品18hdxxxⅹ在线 | 精品av| 成人久久18免费网站 |