vb.net 数据未插入 ms access 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20258876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vb.net data not inserting into ms access database
提问by Ahmad Asjad
This is my form snapshot
这是我的表格快照


This is my code to insert data into data base
这是我将数据插入数据库的代码
Imports System.Data.OleDb
Public Class Test
Dim cnn As New OleDb.OleDbConnection
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cmd As New OleDb.OleDbCommand
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
cmd.Connection = cnn
cmd.CommandText = "INSERT INTO Test(ID, Test) " & _
" VALUES(" & Me.TextBox1.Text & ",'" & Me.TextBox2.Text & "')"
cmd.ExecuteNonQuery()
cnn.Close()
End Sub
Private Sub Test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cnn = New OleDb.OleDbConnection
cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\OfficeAutomationSystem.accdb; Persist Security Info=False"
End Sub
End Class
My database name is : OfficeAutomationSystem.accdb , Table Name is: Test , Table Structure is as follows:
我的数据库名是:OfficeAutomationSystem.accdb,表名是:Test,表结构如下:
FieldName DataType
ID Number
Test Text
Code is running successfully, and giving no error. When I see in database, there was no record found in that
代码运行成功,没有报错。当我在数据库中看到时,没有在其中找到记录
What's the error? I'm unable to find it. So Please, Help me. Thanks in advance
有什么错误?我无法找到它。所以请帮助我。提前致谢
回答by ?s??? ????
Sometimes Data Source=|DataDirectory|\...is problematic when debugging. Please bear on mind that you'll have another database in \bin\debugat your project folder when you are debugging your code. Probably you're updating the records in this database instead the original one.
有时Data Source=|DataDirectory|\...在调试时会出现问题。请记住,\bin\debug当您调试代码时,您的项目文件夹中将有另一个数据库。可能您正在更新此数据库中的记录而不是原始记录。
Try to set an absolute path and check if the records are being updated.
尝试设置绝对路径并检查记录是否正在更新。
回答by Malek
Dim cnn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\OfficeAutomationSystem.accdb"

