簡體   English   中英

有助於減少與數據庫的連接數

[英]help with reducing the number of connections to the database

我有一個字符串列表,它們只是發票編號。 我將通過此列表進行枚舉,以從數據庫中獲取每張發票的詳細信息。 該列表的大小很容易為700到1000。 我現在這樣做的方式導致700-1000連接到數據庫。 這需要太長時間才能完成是否有更好的方法來做到這一點,我只是不知道? 任何指針都會很棒。

這是我的枚舉的一個例子

foreach(string i in invoiceList)
{
  Invoice inv = invoiceData.GetInvoice(i);
  //do something with the invoice
}

那么這是我使用ado.net的數據訪問方法的一個例子

public Invoice GetInvoice(string invoice)
{
      SqlConnection con = new SqlConnection(//connection string);
      SqlCommand cmd = new SqlCommand("dbo.getInvoices", con);
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.Parameters.Add("invoice", SqlDbType.VarChar).Value = invoice;
      SqlDataReader dr;
      Invoice inv = new Invoice();
      try{
            con.Open();
            dr = cmd.ExecuteReader
            while(dr.read())
            {
                 //assign values from the database fields
            }


      }
      catch{}
      finally{con.close();}

}

所以基本上getInvoice方法每次都會調用1000次打開一個新連接。 什么是更好(更快)的方法來做到這一點。 謝謝!

您可以將連接打開和關閉代碼放在循環之外。 這將使您只有一個連接到數據庫。 但是這一個連接將暫時開放。 這是權衡。 一個連接打開很長時間或許多連接打開和關閉。

我也注意到你沒有在try代碼中關閉你的連接。 也許試試這個。

public Invoice GetInvoice(string invoice)
{
      SqlConnection con = new SqlConnection(//connection string);
      SqlCommand cmd = new SqlCommand("dbo.getInvoices", con);
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.Parameters.Add("invoice", SqlDbType.VarChar).Value = invoice;
      SqlDataReader dr;
      Invoice inv = new Invoice();
      try{
            con.Open();
            dr = cmd.ExecuteReader
            while(dr.read())
            {
                 //assign values from the database fields
            }
      }
      catch{}
      finally
      {
        con.Close();
      }
}

我在你的try-block中缺少conn.Close()

如果它確實缺失,那可能是你的問題:你一直在建立新的連接。 所以,在try / finally塊中關閉它

但如果這是發布代碼中的拼寫錯誤,那么我認為您的問題與Connection無關,ADO.NET使用ConnectionPooling,因此您可以保持“真正的”連接打開,即使您說conn也是如此。關()。

另一個問題是對每張發票進行查詢。 那也很貴。 但是既然你似乎使用了SP,那就不容易克服了。 這里有用的是一個以WHERE Id IN (a, b, c, d)結尾的SELECT語句。 這將允許您批量發票(通過1個查詢獲得5或20)。

只需將所有發票編號放在IN語句中,然后在單個連接中運行此select語句即可。

這樣的事情可能會有所改善。

public List<Invoice> GetInvoices(List<string> invoiceList) {
  List<Invoice> invoices = new List<Invoice>();

  Invoice inv;
  SqlDataReader dr;

  using (SqlConnection con = new SqlConnection(//connection string)) {
    using(SqlCommand cmd = new SqlCommand("dbo.getInvoices", con)) {
      cmd.CommandType = CommandType.StoredProcedure;
      SqlParameter param = cmd.Parameters.Add("invoice", SqlDbType.VarChar);

      foreach(string i in invoiceList) {
        inv = new Invoice();
        param.Value = i;
        using (dr = cmd.ExecuteReader()) {
          while(dr.read())
          {
            // assign values from the database fields
            inv.Property = dr.GetString(0);

            // Add invoice to the result list
            invoices.Add(inv);
          }
        }
      }
    }
  }

  return invoices;
}

然后就可以像這樣使用這種方法......

var invoiceList = new List<string> { "123", "456", "789" };
var invoices = GetInvoices(invoiceList);
foreach(var i in invoices) {
  Console.WriteLine(i.SomeInvoiceProperty);
}

我相信如果您一直處理700到1000個或更多發票號碼,為什么不在一個查詢中發送所有發票號碼而不是多個單獨的查詢,您可能想要考慮不同的方法。 作為示例,您可以使用列表中的sql來執行此操作,如下所示。

select
 *
from
 ivoice_table
where
 invoice_table.invoice_number in (123,124,125,126,127,128 etc....)

請享用!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM