查询花费比预期的odbc更长的时间

时间:2018-09-11 15:23:49

标签: c# sage50

我正在做一个简单的选择,上面有一个日期过滤器,其范围是一个月,其中仅存在32条记录,但是要花15秒时间查询和返回我正在使用Sage 50的数据,正如您可能会讲到的和C#一样。我正在使用odbc创建查询,如果我使用odbc查询工具,则可以发现速度。

enter image description here

这是用于前向选择的选择,通过odbc返回数据的时间应该不会太长。

  

String SQL = string.Format(“ SELECT'ORDER_NUMBER','ORDER_OR_QUOTE',   'ANALYSIS_1','ACCOUNT_REF','ORDER_DATE','NAME',   “ COURIER_NUMBER”,“ COURIER_NAME”,“ CUST_TEL_NUMBER”   ,“ DESPATCH_DATE”,“ ACCOUNT_REF”,“ DEL_NAME”,“ DEL_ADDRESS_1”,   'DEL_ADDRESS_2','DEL_ADDRESS_3','DEL_ADDRESS_4','DEL_ADDRESS_5',   'INVOICE_NUMBER','ORDER_DATE','INVOICE_NUMBER_NUMERIC',   “ CONTACT_NAME”,“ CONSIGNMENT”,“ NOTES_1”,“ ITEMS_NET”   ,“ ITEMS_GROSS”,“ QUOTE_STATUS”来自ORDER_DATE的SALES_ORDER

     
    

='{0}'和ORDER_DATE <='{1}'“,从D到toD);

  
public List<SalesOrders> GetSalesOrders()
{
        List<SalesOrders> _salesOrdersList = new List<SalesOrders>();

        try
        {


            string sageDsn = ConfigurationManager.AppSettings["SageDSN"];
            string sageUsername = ConfigurationManager.AppSettings["SageUsername"];
            string sagePassword = ConfigurationManager.AppSettings["SagePassword"];
            //int totalRecords = GetSalesOrdersount();
            int counter = 0;
             //using (var connection = new OdbcConnection("DSN=SageLine50v24;Uid=Manager;Pwd=;"))
            using (var connection = new OdbcConnection(String.Format("DSN={0};Uid={1};Pwd={2};", sageDsn, sageUsername, sagePassword)))
            {

                connection.Open();

                //string sql = string.Format(getInvoiceSql, customerCode, DateTime.Today.AddMonths(-1).ToString("yyyy-MM-dd"));
                string fromD = dtpFrom.Value.ToString("yyyy-MM-dd");
                string toD = dtpTo.Value.ToString("yyyy-MM-dd");

                String SQL = string.Format("SELECT 'ORDER_NUMBER', 'ORDER_OR_QUOTE', 'ANALYSIS_1','ACCOUNT_REF','ORDER_DATE','NAME', 'COURIER_NUMBER','COURIER_NAME','CUST_TEL_NUMBER' ,'DESPATCH_DATE','ACCOUNT_REF',  'DEL_NAME', 'DEL_ADDRESS_1', 'DEL_ADDRESS_2', 'DEL_ADDRESS_3', 'DEL_ADDRESS_4', 'DEL_ADDRESS_5',  'INVOICE_NUMBER','ORDER_DATE','INVOICE_NUMBER_NUMERIC', 'CONTACT_NAME','CONSIGNMENT', 'NOTES_1', 'ITEMS_NET' ,'ITEMS_GROSS','QUOTE_STATUS' FROM SALES_ORDER WHERE ORDER_DATE >='{0}' and ORDER_DATE <='{1}'", fromD, toD);
                using (var command = new OdbcCommand(SQL, connection))
                {


                    using (var reader = command.ExecuteReader())

                    {

                        while (reader.Read())
                        {
                            counter++;

                            backgroundWorker1.ReportProgress(counter);

                            var salesOrders = new SalesOrders();
                            salesOrders.ACCOUNT_REF = Convert.ToString(reader["ACCOUNT_REF"]);
                            salesOrders.RecordIdentifier = "";
                            salesOrders.ShipmmentId = Convert.ToString(reader["ORDER_NUMBER"]);
                            salesOrders.OrderDate = Convert.ToDateTime(reader["ORDER_DATE"]);
                            salesOrders.OrderNumber = Convert.ToString(reader["ORDER_NUMBER"]);

                            salesOrders.Company = "";
                            salesOrders.Carrier = Convert.ToString(reader["COURIER_NUMBER"]);
                            salesOrders.CarrierService = Convert.ToString(reader["COURIER_NAME"]);
                            salesOrders.CustomerName = Convert.ToString(reader["NAME"]);
                            salesOrders.ShipToAddress1 = Convert.ToString(reader["DEL_ADDRESS_1"]);
                            salesOrders.ShipToAddress2 = Convert.ToString(reader["DEL_ADDRESS_2"]);
                            salesOrders.ShipToAddress3 = Convert.ToString(reader["DEL_ADDRESS_3"]);
                            salesOrders.ShipToAddress4 = Convert.ToString(reader["DEL_ADDRESS_4"]);
                            salesOrders.ShipToAddress5 = Convert.ToString(reader["DEL_ADDRESS_5"]);
                            salesOrders.ShiptoAttention = Convert.ToString(reader["DEL_NAME"]);
                            salesOrders.ShiptoPhoneNo = Convert.ToString(reader["CUST_TEL_NUMBER"]);
                            salesOrders.Country = Convert.ToString(reader["ANALYSIS_1"]);
                            salesOrders.ShiptoEmail = "";
                            salesOrders.MakeAddressDefault = "Y";
                            bool isProcessed = _sqlManager.hasbeenProcessed(salesOrders.OrderNumber);
                            if (isProcessed == true)
                                salesOrders.Exported = true;
                            _salesOrdersList.Add(salesOrders);


                        }
                    }
                }

            }

        return _salesOrdersList.OrderByDescending(o => o.OrderDate).ToList();
    }

1 个答案:

答案 0 :(得分:1)

请勿使用{0},{1}将值嵌入字符串...通过参数添加

               String SQL = 
@"SELECT 
      ORDER_NUMBER, 
      ORDER_OR_QUOTE, 
      ANALYSIS_1,
      ACCOUNT_REF,
      ORDER_DATE,
      `NAME`, 
      COURIER_NUMBER,'
      OURIER_NAME,
      CUST_TEL_NUMBER,
      DESPATCH_DATE,
      ACCOUNT_REF,  
      DEL_NAME, 
      DEL_ADDRESS_1,
      DEL_ADDRESS_2,
      DEL_ADDRESS_3,
      DEL_ADDRESS_4,
      DEL_ADDRESS_5,
      INVOICE_NUMBER,
      ORDER_DATE,
      INVOICE_NUMBER_NUMERIC,
      CONTACT_NAME,
      CONSIGNMENT,
      NOTES_1,
      ITEMS_NET,
      ITEMS_GROSS,
      QUOTE_STATUS
   FROM 
      SALES_ORDER 
   WHERE 
          ORDER_DATE >= ?
      and ORDER_DATE <= ?
   ORDER BY
      ORDER_DATE DESC";

                using (var command = new OdbcCommand(SQL, connection))
                {
                   // assuming fields are actually date data types fields
                   command.Parameters.Add( "parmFromDate", fromD );
                   command.Parameters.Add( "parmToDate", toD );

“?”在查询中是由ODBC进程处理的参数值的占位符。在using()部分中添加的参数将以与其各自的占位符部分相同的顺序位置添加。我刚刚分配了参数名称,以便为以后查看它的任何人提供上下文。

查询本身应该非常快,具体取决于您要提取的日期范围。甚至以降序添加了SQL Order,因此它也按照您想要的顺序预先拉下来。