无法将数据插入数据库 ms-access vb [英] Can't Insert data to database ms-access vb

查看:26
本文介绍了无法将数据插入数据库 ms-access vb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

案例 1:我无法向我的数据库中插入数据

Case 1: i can't insert data to my database

  Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\ProjectVB.accdb")

案例 2:我可以向我的数据库中插入数据

Case 2: i can insert data to my database

Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Admin\Documents\ProjectVB.accdb")

数据库已经保存在我的项目中

database was saved in my project already

conn.Open()

            cmd.Connection = conn
            cmd.Parameters.AddWithValue("@ac_ID", txtID.Text)
            cmd.Parameters.AddWithValue("@ac_pass", txtPassword.Text)
            cmd.Parameters.AddWithValue("@nation_ID", txtNoID.Text)
            cmd.Parameters.AddWithValue("@fName", txtFirstName.Text)
            cmd.Parameters.AddWithValue("@lName", txtLastName.Text)
            cmd.Parameters.AddWithValue("@tel", txtTel.Text)
            cmd.Parameters.AddWithValue("@province", cbNation.SelectedItem)
            cmd.Parameters.AddWithValue("@regOn", dtRegOn.Text)
            cmd.Parameters.AddWithValue("@status", txtStatus.Text)
            cmd.Parameters.AddWithValue("@gender", cbGender.SelectedItem)
            cmd.Parameters.AddWithValue("@location", txtLocation.Text)
            cmd.Parameters.AddWithValue("@img", imgBuffer)
            cmd.Parameters.AddWithValue("@rank", cbRank.SelectedItem)
            cmd.CommandText = "INSERT INTO db_KJ_Profile VALUES(@ac_ID,@ac_pass,@nation_ID,@fName,@lName,@tel,@province,@regOn,@status,@gender,@location,@img,@rank)"
            cmd.ExecuteNonQuery()

推荐答案

如果是 DataDirectory 替换字符串,您应该检查文件夹 PROJECTFOLDER\BIN\DEBUG(或相同路径的 x86 版本).

In the case of the DataDirectory substitution string you should check the content of the database in the folder PROJECTFOLDER\BIN\DEBUG (or x86 version of the same path).

这是因为在 WinForms 应用程序中,当在 VS 的调试会话中运行时,DataDirectory 指向执行应用程序的文件夹(BIN\DEBUG 或 BIN\项目文件夹的 x86\DEBUG 子文件夹).

This is due to the fact that in a WinForms application, when running in a debug session of VS, the DataDirectory points to the folder where your application is executed (The BIN\DEBUG or BIN\x86\DEBUG subfolder of the project folder).

这很容易被忽视,而且总是令人困惑.
如果您的项目项中列出了 ACCDB 文件,情况会更糟.在这种情况下,数据库项有一个名为Copy To The Output Directory 的属性.您应该检查是否设置为 Copy If Newer 或 Copy Never 而不是 Copy Always 因为在这种情况下,在每次重新启动调试会话时都会有一个新的数据库副本从您的项目文件夹复制(当然没有刚刚插入的数据)到 BIN\DEBUG 文件夹

This is easy to overlook and it is always a source of confusion.
And it is worse if you have the ACCDB file listed in your project items. In this case the database item has a property called Copy To The Output Directory. You should check that is set to Copy If Newer or Copy Never and not to Copy Always because in this case, at every restart of your debug session a new copy of the database is copied (of course without the just inserted data) from your project folder to the BIN\DEBUG folder

这篇关于无法将数据插入数据库 ms-access vb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆