2016-08-20 136 views
-1

我正在使用Microsoft Visual Studio 2010 Express,並且正在嘗試使用VB.NET製作註冊表單。這是我的代碼到目前爲止:INSERT INTO語句獲取語法錯誤

Private Sub insrt() 
    If picStr = vbNullString Then 
     'save the current image default (noimage) 
     picImage.Image.Save(".png", Imaging.ImageFormat.Png) 
     'reload current " Noimage" to a binary 
     picStr = Application.StartupPath + (".\noimage.png") 
    End If 

    'set image object to value 
    IMG = (Nothing) 
    IMG = Image.FromFile(picStr) 
    'fill mystream into image binary 
    MyStream = New MemoryStream 
    IMG.Save(MyStream, Imaging.ImageFormat.Png) 

    Dim gen As String = String.Empty 
    If rbtnMale.Checked = True Then 
     gen = "Male" 
    ElseIf rbntFemale.Checked = True Then 
     gen = "Female" 
    End If 

    Dim dob As String = cboDay.Text & "/" & cboMonth.Text & "/" & txtYr.Text 

    Dim con As New OleDbConnection(cnSettings) 
    con.Open() 
    Try 
     If txtID.Text = "" Then 
      MsgBox("Please Enter year enrolled and Generate ID for this Student") 
      Exit Sub 
     Else 
      Dim cmd As New OleDbCommand 
      cmd.Connection = con 
      cmd.CommandText = "INSERT into tbEnroll(StudentID,Firstname,Lastname,Gender,DOB,Hometown,Region,Sess,Batch,Class,GFirstname,GLastname,GTel,GEmail,GResidence,DateEnroll,Picture) values ('" & txtID.Text & "','" & txtFN.Text & "','" & txtLN.Text & "','" & gen & "','" & dob & "','" & txtHomeTWN.Text & "','" & cboRegion.Text & "','" & cboSession.Text & "','" & txtBatch.Text & "','" & cboClass.Text & "','" & txtGfirst.Text & "','" & txtLastnm.Text & "','" & txtTel.Text & "','" & txtEmail.Text & "','" & txtRes.Text & "','" & DateTimePicker1.Text & "', @img ,)" 
      cmd.Parameters.AddWithValue("@img", OleDbType.VarBinary).Value = MyStream.GetBuffer 
      cmd.ExecuteNonQuery() 
      cmd.Dispose() 

      MyStream.Dispose() 
      MyStream = Nothing 
      MsgBox("Enrollment Done successfully", , "Enrollment") 
      cleartxt() 
     End If 
    Catch ex As Exception 
     MsgBox(ex.Message) 
    End Try 

End Sub 

有什麼可以解決這個問題?

+1

*一個*問題是'Class'是一個保留字,另一個沒有使用SQL param因爲在那個糟糕的SQL湯中可能會有太多或太少的滴答。另一種是通過將所有內容作爲字符串傳遞而導致無意的數據類型轉換。 – Plutonix

回答

0

不應將所有SQL參數連接到SQL語句,而應使用參數,以使代碼稍後更具可讀性。

而且我看到一個問題,@img)」在這裏,你不應該有一個逗號@img因爲你沒有其他的參數插入後

另外,作爲Plutonix上面說的,你不應該將所有內容都轉換爲字符串以便爲SQL使用參數,它們應該是它們在SQL表中的值類型

0

嘗試複製&查詢視圖中的Access SQL設計過去查詢(CommandText)。如果數據庫有問題

相關問題