【问题标题】:spliting large query string in acess在访问中拆分大查询字符串
【发布时间】:2012-04-17 17:41:16
【问题描述】:

我在访问中有一个很长的查询,并试图将其分成多行,以便我可以在调试期间检查它们,我尝试了通过谷歌找到的步骤,但失败并显示如下信息。

    public DataSet showallCompanyPaymentbyjobcode(int jobpk ,int confirmquotationpk)
        {

            string query=SELECT companypaymentmastertable.paymentpk, companypaymentmastertable.cmpinvoice, companypaymentmastertable.jobcode, companypaymentmastertable.customercode, confirmquotationmastertable.quotationcode, companypaymentmastertable.customerName, companypaymentmastertable.ischeque, companypaymentmastertable.isCash, companypaymentmastertable.amount, companypaymentmastertable.chequenumber, companypaymentmastertable.bankname, companypaymentmastertable.chequedate, companypaymentmastertable.chequereleasedate, companypaymentmastertable.companypaymentdate
FROM confirmquotationmastertable INNER JOIN companypaymentmastertable ON confirmquotationmastertable.confirmpk=companypaymentmastertable.confirmpk
WHERE (((companypaymentmastertable.confirmpk)=[?]) AND ((companypaymentmastertable.jobpk)=15));


                               OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, Program.ConnStr);
                DataSet ds = new DataSet();
                dAdapter.Fill(ds, "tblpayview");

                if (ds.Tables.Count <= 0)
                {
                    ds = null;    
                }

                return ds;

            }

在另一个类中我称之为它

 public void fillpaymenttable()
        {
            DataSet ds= new DataSet();
            ds= companytransaction.showallCompanyPaymentbyjobcode(cmbjobcode.SelectedValue,cmbQuotationcode.SelectedValue);

             tblpaymentview.DataSource = ds.Tables["tblpayview"].DefaultView;

                if (ds.Tables.Count <= 0)
                {
                    lblstatus.Text = "No Payment Details Present";
                    clearcontrols();
                }

            }

如果这样调用数据集,有什么方法可以拆分查询以及此功能是否有效?

【问题讨论】:

    标签: c# .net sql tsql


    【解决方案1】:

    如果您只是想将代码实际拆分为单独的行,请使用StringBuilder?请注意,如果您将参数传递给查询,则情况并非如此,因为您很容易受到 SQL 注入的攻击

    var query = new StringBuilder();
    
    query.Append("SELECT companypaymentmastertable.paymentpk, companypaymentmastertable.cmpinvoice, ");
    query.Append("companypaymentmastertable.jobcode, companypaymentmastertable.customercode, ");
    query.Append("confirmquotationmastertable.quotationcode, companypaymentmastertable.customerName, ");
    query.Append("companypaymentmastertable.ischeque, companypaymentmastertable.isCash, ");
    query.Append("companypaymentmastertable.amount, companypaymentmastertable.chequenumber, ");
    query.Append("companypaymentmastertable.bankname, companypaymentmastertable.chequedate, ");
    query.Append(" companypaymentmastertable.chequereleasedate, companypaymentmastertable.companypaymentdate ");
    query.Append("FROM confirmquotationmastertable INNER JOIN companypaymentmastertable ");
    query.Append("ON confirmquotationmastertable.confirmpk=companypaymentmastertable.confirmpk ");
    query.Append("WHERE (((companypaymentmastertable.confirmpk)=[?]) ");
    query.Append("AND ((companypaymentmastertable.jobpk)=15))");
    

    【讨论】:

    • query.Append("WHERE (((companypaymentmastertable.confirmpk)=[?]) "); query.Append("AND ((companypaymentmastertable.jobpk)=15))");我将在这里使用 oledbcmnd.parameter .adwithvalue(); 从用户那里获取参数
    • 好的,只需将 [?] 更改为您的参数名称,将 15 更改为您的其他参数名称,然后您可以对这些参数执行 AddWithValue。
    • @sreenathsreenath 请注意,使用 stringbuilder 会在这里引入很多不必要的开销。 StringBuilder 最适用于循环和其他在运行时确定字符串组件的情况。
    • 你确定吗?我打算只做基本的字符串操作,但我认为它的性能更差?
    • 其实你是对的,虽然差别不是很大geekswithblogs.net/BlackRabbitCoder/archive/2010/05/10/…
    【解决方案2】:

    这将比使用 stringbuilder 更有效,因为字符串连接将在编译时执行,而不是在运行时执行:

    string query="SELECT companypaymentmastertable.paymentpk, companypaymentmastertable.cmpinvoice, "
     + "companypaymentmastertable.jobcode, companypaymentmastertable.customercode, "
     + "confirmquotationmastertable.quotationcode, companypaymentmastertable.customerName, "
     + "companypaymentmastertable.ischeque, companypaymentmastertable.isCash, companypaymentmastertable.amount, "
     + "companypaymentmastertable.chequenumber, companypaymentmastertable.bankname, companypaymentmastertable.chequedate, "
     + "companypaymentmastertable.chequereleasedate, companypaymentmastertable.companypaymentdate "
     + "FROM confirmquotationmastertable INNER JOIN companypaymentmastertable ON "
     + "confirmquotationmastertable.confirmpk=companypaymentmastertable.confirmpk "
     + "WHERE (((companypaymentmastertable.confirmpk)=[?]) AND ((companypaymentmastertable.jobpk)=15));"
    

    或者,您可以使用“逐字字符串”:

        string query= @"SELECT companypaymentmastertable.paymentpk, companypaymentmastertable.cmpinvoice, 
    companypaymentmastertable.jobcode, companypaymentmastertable.customercode, 
    confirmquotationmastertable.quotationcode, companypaymentmastertable.customerName, 
    companypaymentmastertable.ischeque, companypaymentmastertable.isCash, companypaymentmastertable.amount, 
    companypaymentmastertable.chequenumber, companypaymentmastertable.bankname, companypaymentmastertable.chequedate, 
    companypaymentmastertable.chequereleasedate, companypaymentmastertable.companypaymentdate    
    FROM confirmquotationmastertable 
    INNER JOIN companypaymentmastertable ON confirmquotationmastertable.confirmpk=companypaymentmastertable.confirmpk    
    WHERE (((companypaymentmastertable.confirmpk)=[?]) AND ((companypaymentmastertable.jobpk)=15));";
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    相关资源
    最近更新 更多