2012-04-17 3 views
0

나는 긴 쿼리를 액세스 할 수 있었고 여러 줄로 만들어 디버깅 중에 확인할 수 있었으며 Google에서 발견 한 단계를 시도해보고 표시된 정보로 실패했습니다 이하. 다른 클래스에서acess에서 큰 쿼리 문자열 분할하기

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

      } 

과 같이 호출이 함수 작업 데이터 세트 경우를 쿼리를 분할 여부를 어떤 방법이 있나요이라고?

+1

문장 부호를 사용하십시오. – phoog

답변

1

SQL 주입에 취약 할 것 같은 문자열 연결이 수행되기 때문에이 케이스되지 않을 것합니다 하지 런타임에 컴파일 시간에 :

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));"; 
+0

+1 phoog. 너무 많은 사람들이 StringBuilder가 항상 문자열 연결보다 낫다고 생각합니다. –

+0

그래, 정말로 포함 시켰어. 나는 고쳤다. :). 그것은 가까운 전화 : P 출처 : http://geekswithblogs.net/BlackRabbitCoder/archive/2010/05/10/c-string-compares-and-concatenations.aspx – mattytommo

2

실제로 코드를 별도의 줄로 나누려면 StringBuilder을 사용 하시겠습니까? 쿼리에 매개 변수를 전달한다면 당신은 이것은 모두 StringBuilder를 사용하는 것보다 더 효율적

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))"); 
+0

query.Append ("WHERE ((companypaymentmastertable.confirmpk) = [?])"); query.Append ("AND ((companypaymentmastertable.jobpk) = 15))"); 여기서 oledbcmnd.parameter .adwithvalue();를 사용하여 사용자로부터 매개 변수를 가져옵니다. –

+0

좋습니다. 괜찮습니까? [?]를 매개 변수 이름으로, 15를 다른 매개 변수 이름으로 변경하면 해당 매개 변수에 대해 AddWithValue를 수행 할 수 있습니다. – mattytommo

+0

@sreenathsreenath stringbuilder를 사용하면 많은 불필요한 오버 헤드가 발생합니다. StringBuilder는 주로 루프 및 문자열의 구성 요소가 런타임에 결정되는 기타 상황에 유용합니다. – phoog