我試圖從窗體視圖中獲取用戶輸入的數據並將其插入到數據庫中的表 問題是隻要編譯器到達Rtype變量存儲它的價值,它給了我這個錯誤: 我知道錯誤的意思,但我根本無法得到它的工作。 以下是我在類代碼Form1中插入數據到數據庫(sqlexception)不起作用
Imports System.Data.SqlClient
Public Class Form1`
Private Sub newBtn_Click(sender As Object, e As EventArgs) Handles BtnNwRoom.Click
Dim obj As New Hotl()
Dim selectedItem As Object
selectedItem = hotelCombobox.SelectedItem()
If (obj.addnew(CInt(Me.roomNum.Text), CInt(selectedItem), Me.roomType.Text, Me.price.Text) = False) Then
MsgBox(" no record is added, Try again later")
End If
End Sub
End class
這是增加新的功能:
Public Function addnew(ByVal roomNo As Integer, ByVal hotelNo As String, ByVal RoomType As String, ByVal price As Integer) As Boolean
Dim sqlstmnt = "insert into Room (roomNo,hotelNo,RoomType,price) values(" & roomNo & " , " & hotelNo & " , " & RoomType & " , " & price & ")"
MsgBox(sqlstmnt)
conn = ConNew()
'''''''''''''''''''''''''''''' Execute Reader
''''''''''''''''''''''''''''''''''''''''''''''
Dim command As New SqlCommand(sqlstmnt, conn)
If command.ExecuteNonQuery() = 1 Then
MessageBox.Show("insertion Succeded")
Return True
Else
Return False
End If
End Function
使用sql參數,可能會解決這個問題,更重要的是,你固有的sql注入漏洞。 –
「Room」表中的列是什麼? –
您的其中一個值是文本「大」,但您沒有用單引號包裝文本值,所以它被解釋爲列名,因此您被告知該列名無效。你可以把單引號放在你的SQL的適當位置,但這是一個bandaid措施。正如@TimSchmelter所建議的那樣,使用參數正確執行此操作。看到這裏:http://jmcilhinney.blogspot.com.au/2009/08/using-parameters-in-adonet.html – jmcilhinney