2016-03-15 79 views
1
st = "SELECT YearLevel FROM Students WHERE StudentID = " & txtStudentID.Text 
    da = New OleDbDataAdapter(st, conn) 
    da.Fill(ds, "Students") 
    YearLevel = ds.Tables("Students").Rows(0).Item("YearLevel") 

    st1 = "SELECT TuitionFee, BookFee, MiscellaneousFee, OtherFee FROM Expenses WHERE YearLevel = " & YearLevel 

    da1 = New OleDbDataAdapter(st1, conn) 
    da1.Fill(ds1, "Expenses") 

所以我有那個代碼,然後我有我的第二個查詢中缺少運算符的錯誤,可能的解決方案是什麼?加入兩個表

+0

'YearLevel'的值是多少?你可以添加一個消息框來查看該值是什麼? –

+0

值爲'1級' – silentstorm21

回答

0

您會收到此錯誤消息,因爲YearLevel的值爲Grade 1,因此需要用引號將其轉義。所以,你希望你的RDBMS來執行實際的查詢可能看起來是這樣的:

SELECT TuitionFee, BookFee, MiscellaneousFee, OtherFee 
FROM Expenses 
WHERE YearLevel = 'Grade 1' 

您可以直接在字符串st1中添加引號,但是這可能讓門打開SQL注入。這裏是一個更加安全的方式去這樣做:

MyCommand = New SqlCommand("SELECT TuitionFee, BookFee, MiscellaneousFee, OtherFee FROM Expenses WHERE YearLevel = @YearLevel", conn) 
MyCommand.Parameters.AddWithValue("@YearLevel", YearLevel) 

VB.net應該感覺到參數是字符類型,並自動報價爲你逃避它。

+0

我想首先從另一個表中調用YearlLevel,它將匹配另一個表中的YearLevel – silentstorm21

+0

您使用的邏輯看起來很好,但我認爲語法是關閉的。試試我的答案,讓我知道它是否有效。 –

+0

我試着用你的代碼。但它不會連接到我使用的其他查詢 – silentstorm21