0

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

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

            }

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

4

2 回答 2

2

如果您只是想将代码实际拆分为单独的行,请使用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))");
于 2012-04-17T17:56:27.590 回答
1

这将比使用 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));";
于 2012-04-17T19:14:21.170 回答