2013-02-28 73 views
0

我傳遞SQL和的參數(從用戶輸入的拍攝)的陣列,以一個OleDbDataAdapter的像這樣:參數化SQL查詢拋出錯誤 - 語法不正確?

Dim sql As New StringBuilder 
Dim theparams As String(,) 
sql.Append(" select distinct acc.?, ") 
sql.Append(" isnull(acc.?, acc.?) ?, ") 
sql.Append(_ 
    " Case when ptree.brandid=220 then 99 else ptree.brandgroupid end brandgroupid, Case when ptree.brandid=220 then 'Multibrand Prepacks' else isnull(ptree.brandgroupname,'1') end brandgroupname, isnull(ptree.brandgroup2,'1') brandgroup2 , ptree.brandgroup3, ptree.brandid, ptree.brandname ") 
sql.Append(" from ") 
sql.Append(" (select acc.?, ") 
sql.Append(" acc.? from ") 
sql.Append(" vw_ACCOUNT_TREE acc") 
sql.Append(" UNION") 
sql.Append(" SELECT 'Unknown','Unknown'") 
sql.Append(") acc") 
sql.Append(" CROSS JOIN vw_Product_tree ptree ") 
sql.Append(" WHERE (BRANDGROUPNAME <> 'Unknown' or ptree.brandid=220) ") 
sql.Append(" and brandenabled=1 ") 

theparams = New String(5, 2) {} 
theparams(0, 0) = "@0" 
theparams(0, 1) = Level 
theparams(0, 2) = "OleDbType.String" 
theparams(1, 0) = "@1" 
theparams(1, 1) = Level + "Description" 
theparams(1, 2) = "OleDbType.String" 
theparams(2, 0) = "@2" 
theparams(2, 1) = Level 
theparams(2, 2) = "OleDbType.String" 
theparams(3, 0) = "@3" 
theparams(3, 1) = Level + "Description" 
theparams(3, 2) = "OleDbType.String" 
theparams(4, 0) = "@4" 
theparams(4, 1) = Level 
theparams(4, 2) = "OleDbType.String" 
theparams(5, 0) = "@5" 
theparams(5, 1) = Level + "Description" 
theparams(5, 2) = "OleDbType.String" 

我然後傳遞SQL和參數的陣列以下面的方法,它應創建一個數據表:

Public Function GetTable(ByVal sql As String, ByVal qParameters(,) As String) As DataTable 
    Dim dt As DataTable = New DataTable() 
    Try 
     If sql.Length > 0 Then 
      If ConnectToDB() Then 
       Using oDa As New OleDbDataAdapter(sql, _conn) 
        If Not qParameters.GetValue(i, 0) Is Nothing Then 
         For i As Integer = 0 To (qParameters.Length/3 - 1) 
          oDa.SelectCommand.Parameters.AddWithValue(qParameters(i, 0), qParameters(i, 1)) 
          oDa.SelectCommand.Parameters(qParameters(i, 0)).OleDbType = GetOleParameterType(qParameters(i, 2)) 
         Next 
        End If 
        Using oDs As New DataSet 
         oDa.Fill(oDs) 
         dt = oDs.Tables(0) 
        End Using 
       End Using 
      End If 

     End If 
    Catch ex As Exception 
     _error = "Exception in GetTablewithParameters ~ " & ex.Message & " ~ " & sql 
    End Try 
    Return dt 
End Function 

但是,它正在迎頭趕上但下列情況除外:

Line 1: Incorrect syntax near ')'. 
Line 1: Incorrect syntax near '@P5'. 
Line 1: Incorrect syntax near '@P1'. 

什麼是錯我的代碼?如果我用實際字符串替換參數並刪除每個和每個sql.Append()的打開和關閉,則查詢在SQL Server Management Studio中正常工作。我錯誤地使用參數?

編輯:更多定義的錯誤:

Line 1: Incorrect syntax near '@P1'. 
Line 8: Incorrect syntax near '@P5'. 
Line 12: Incorrect syntax near ')'. 

下面是一行細分代碼:

1. select distinct acc.?, 
2. isnull(acc.?, acc.?) ?, 
3. Case when ptree.brandid=220 then 99 else ptree.brandgroupid end brandgroupid, 
4. Case when ptree.brandid=220 then 'Multibrand Prepacks' 
5. else isnull(ptree.brandgroupname,'1') end brandgroupname, isnull(ptree.brandgroup2,'1') 
6. brandgroup2 , ptree.brandgroup3, ptree.brandid, ptree.brandname 
7. from 
8. (select acc.?, 
9. acc.? from 
10. vw_ACCOUNT_TREE acc 
11. UNION 
12. SELECT 'Unknown','Unknown') acc 
13. CROSS JOIN vw_Product_tree ptree 
14. WHERE (BRANDGROUPNAME <> 'Unknown' or ptree.brandid=220) 
15. and brandenabled=1 

回答

1

如果以下這將是更容易調試了很多。

  1. 每次追加後添加換行符(CR LF)。然後你會一直得到合理而有用的行號而不是行1。
  2. 在調試日誌中輸出實際的字符串。然後你可以看到錯誤發生在哪一行。
+0

非常好的建議,謝謝。我現在知道錯誤發生在第1,8和12行。我將進一步診斷,但知道發生錯誤的位置要容易得多。 – TimeBomb006 2013-02-28 16:30:31