我是新來vb.net
我需要使用插入表中的數據vb.net請任何一個可以幫助我怎樣才能插入使用VBNet
我已經試過這
在這裏,我試過示例代碼
我得到這個例外Column name or number of supplied values does not match table definition.
感謝推進
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim strName As String = txtName.Text
Dim strId As String = txtID.Text
Dim strPhone As String = txtPhone.Text
Dim strBranch As String = cmboxBranch.SelectedItem.ToString()
Dim strCourse As String = cmbboxCourse.SelectedItem.ToString()
Dim dblFee As Double = Double.Parse(txtFee.Text)
Dim strCommand As String = "insert into student values('" & strName & "','" & strId & "','" & strPhone & "','" & strBranch & "','" & strCourse & "'," & dblFee & ")"
Dim command As SqlCommand = New SqlCommand(strCommand, connection)
command.CommandType = CommandType.Text
'' MsgBox(strCommand)
connection.Open()
If (command.ExecuteNonQuery().Equals(1)) Then
MsgBox("Information stored in database")
Else
MsgBox("Not stored in database")
End If
End Sub
您應該向我們展示_table definition_。也許你忘記了自動增加pk列'IDENTITY'。順便說一句,你打開SQL注入。您應該使用['SqlParameters'](http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlparameter.aspx)。 –
我的桌子'defn'是'學生(SName,SID,SPhone,SBranch,SCourse,SFee)' 所有屬於'varchar'類型,除了'fee'('費用是實際類型') – rangasathish
@rangasathish - 那不是一個表格定義。最簡單的方法是使用SQL Server Management Studio(SSMS)訪問表格。右鍵單擊它,選擇「腳本表 - >作爲創建 - >到剪貼板」。然後*編輯*你的問題並粘貼結果。它會向我們顯示錶格的*數據類型*,任何*約束*等。 –