2014-04-10 80 views
-2
sql = "insert into reservation (train_no,trainame,from,to,name1,age) values(" & 
     Val(lbl3.Caption) & ",' " & lbl4.Caption & "','" & findtrain.srce.Text & 
     "','" & findtrain.dstn.Text & "','" & Form3.Text1.Text & "'," & 
     Val(Form3.Text3.Text) & ")" 

這是什麼語法錯誤?使用Visual Basic 6的SQL數據庫

+0

您的序列'&「,'」&'可能不應該在單引號(它屬於之前)之後有空格。但是,這不是一個語法問題。你並沒有用引號括住'Val(lbl3.Caption)'或'Val(Form3.Text3.Text)' - 除非Val'函數做了什麼。你應該擔心整個SQL注入。 –

+1

在MsgBox中添加一行顯示'sql'的內容,以查看語句生成後的最終輸出結果。 –

+1

鑑於您所寫的內容,我沒有看到語法錯誤*,但嵌入語句中的值可能很容易導致語法錯誤。其中一個字符串中的任何撇號(單引號)都會破壞您的語法。 –

回答

0

爲什麼不在ADO中使用參數?它將更容易調試。

Public Function Insert(ByVal Values As String) As Integer 
    Dim sCon As String = ConnectionString() 
    Dim sel As String 

    sel = "INSERT INTO " & NombreTabla & _ 
     "(Columns) " & _ 
     "VALUES " & _ 
     "(@Params) " & _ 
     "SELECT @@Identity" 

    Using con As New SqlConnection(sCon) 
     Dim cmd As New SqlCommand(sel, con) 
     cmd.Parameters.AddWithValue("@Params", Values) 


     con.Open() 
     Dim t As Integer = CInt(cmd.ExecuteScalar()) 
     con.Close() 

     Return t 
    End Using 
End Function