如何使用 vb.net 形式删除 MS access 数据库的记录以及如何使用 vb.net 形式进入受密码保护的数据库 [英] How to delete records of MS access database using vb.net form and how to enter a password protected database using vb.net form

查看:68
本文介绍了如何使用 vb.net 形式删除 MS access 数据库的记录以及如何使用 vb.net 形式进入受密码保护的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 vb.net 的新手,我还是一名学生.我创建了一个表格,用于在 vb.net 中输入学生注册详细信息,仅用于教育需要.我使用 MS Access 2010 创建了一个数据库并将其链接到我的 vb 表单.它工作正常,我可以通过 vb.net 应用程序将详细信息输入到我的访问数据库,但我无法处理以下任务.

I'm a new for a vb.net and i'm still a student. I created a form to enter students enrollment details in vb.net just for educational needs. I created a database using MS Access 2010 and linked it to my vb form. It works fine and I can enter details via vb.net application to my access database, but I'm unable to handle following tasks.

1)使用主键删除数据库的记录(我的数据库的主键学号"这是我的代码;请纠正我以完成此任务:

1) Delete record of the database using a Primary key( primary key of the my database " student number" here's my code; please correct me to do this task:

Imports System.Data.OleDb
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim con As OleDbConnection
        Dim com As OleDbCommand
        con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\Users\Pasindu\Documents\database9.accdb")
        com = New OleDbCommand("delete from Table1 where Student number =@sno", con)
        con.Open()
        com.Parameters.AddWithValue("@sno", TextBox1.Text)
        com.ExecuteNonQuery()
        MsgBox("Record Deleted")
        con.Close()
    End Sub
End Class

2)我想使用密码保护我的数据库,并使用密码加密我的 MS Access 数据库.所以我只需要在保护我的数据库时知道对我的代码的修改,因为我无法使用我以前的代码更新我的数据库..这是我以前将数据输入到访问数据库的代码..如果有的话,代码工作正常不是受密码保护的数据库.

2) I want to protect my database using a password and I encrypt my MS Access database using a password. So I just need to know the modification to my code when I protect my database, because I'm unable to update my database using my previous code.. here's my previous code to enter data to the access database.. code works fine if there wasn't a password protected database.

Imports System.Data.OleDb
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim con As OleDbConnection
        Dim com As OleDbCommand
        con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\Users\Pasindu\Documents\database9.accdb;")
        com = New OleDbCommand("insert into table1 values(@sno,@sname,@nic)", con)
        con.Open()
        com.Parameters.AddWithValue("@sno", TextBox1.Text)
        com.Parameters.AddWithValue("@sname", TextBox2.Text)
        com.Parameters.AddWithValue("@nic", TextBox3.Text)
        com.ExecuteNonQuery()
        MsgBox("record added")
        con.Close()
    End Sub

推荐答案

要打开 MS Access 数据库保护密码,您需要更改连接字符串

To open an MS Access database protect with password you need to change your connection string

"Provider=Microsoft.ACE.OLEDB.12.0;" + 
"Data Source=C:\Users\Pasindu\Documents\database9.accdb;" + 
"Jet OLEDB:Database Password=MyDbPassword;"

其中 MyDbPassword 应替换为正确的字符串

where MyDbPassword should be replaced with the correct string

对于您问题的删除部分,我将尝试更改

For the delete part of your question, I will try to change the command in

delete * from Table1 where [Student number]=@sno

如果您的字段名称包含空格,则将名称用方括号括起来

If your field name contains spaces then encapsulate the name with square brackets

这篇关于如何使用 vb.net 形式删除 MS access 数据库的记录以及如何使用 vb.net 形式进入受密码保护的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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