在访问中拆分大型查询字符串

时间:2012-04-17 17:41:17

标签: c# .net sql tsql

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

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

            }

有没有办法拆分查询,如果数据集被调用,这个函数是否有效?

2 个答案:

答案 0 :(得分: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))");

答案 1 :(得分: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));";