[英]Adding data to Access Database through Visual Basic Form
我有一個論壇,我希望用戶鍵入配置文件名稱,然后單擊“添加”,然后它將在表中的“ ProfileName”字段的文本框中添加他們鍵入的內容。 當我單擊“添加”按鈕時,它出現一個錯誤,指出System.data.dll中發生了System.Data.OleDb.OleDbException
類型的未處理的異常。 這是我的代碼:
Private Sub RefreshData()
Dim cnn As New OleDb.OleDbConnection
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
Dim da As New OleDb.OleDbDataAdapter("SELECT id AS [ID], " & _
"ProfileName AS [Name] " & _
" FROM Profile ORDER BY id", cnn)
Dim dt As New DataTable
da.Fill(dt)
cnn.Close()
Me.dgvData.DataSource = dt
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If (TextBox1.Text = "") Then
MsgBox("Please enter a profile name.")
Else
Dim cnn As New OleDb.OleDbConnection("Provider=Microsof… Source=c:\Data\Database.mdb ;Extended Properties=Paradox 5.x;")
Dim cmd As New OleDb.OleDbCommand
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
cmd.Connection = cnn
cmd.CommandText = "INSERT INTO Profile(ProfileName) " & _
"VALUES (" & Me.TextBox1.Text & "')"
cmd.ExecuteNonQuery()
cnn.Close()
Dim oForm As addsnake
oForm = New addsnake
oForm.Show()
oForm = Nothing
Me.Close()
End If
End Sub
如果沒有更多信息,我建議:
您無需為id賦予ID的別名:
SELECT id AS [ID]
您仍然可以將它括在方括號中,如果您願意:
SELECT [id]
您在以下行中缺少撇號:
"VALUES (" & Me.TextBox1.Text & "')"
還要在connectionstrings.com上檢查您的連接字符串
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.