How to delete records of MS access database using vb.net form and how to enter a password protected database using vb.net form

穿精又带淫゛_ 提交于 2020-01-05 04:37:09

问题


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) 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) 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

回答1:


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;"

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




回答2:


Double click on the form and add the following code

Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Administrator\Documents\Project.mdb")
Dim sql = "select ShopperID from Shopper"
Dim command As New OleDbCommand(sql, con)
Dim da As New OleDbDataAdapter(command)
Dim dt As New DataTable
Try
  con.Open()
  da.Fill(dt)
  Dim x As Integer
  For x = 0 To dt.Rows().Count
    ComboBox1.Items.Add(dt.Rows(x).Item("ShopperID").ToString)
  Next
Catch ex As Exception
End Try

Double click on the button and add the code below

Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Administrator\Documents\Project.mdb")
Dim sql = "delete from Shopper where ShopperID='" & ComboBox1.SelectedValue & "'"
Dim command As New OleDbCommand(sql, con)
Dim da As New OleDbDataAdapter(command)
Dim dt As New DataTable
Try
  con.Open()
  da.Fill(dt)
  command.ExecuteNonQuery()
  con.Close()
  MessageBox.Show("Reocrd Deleted")
Catch ex As Exception
End Try


来源:https://stackoverflow.com/questions/11519195/how-to-delete-records-of-ms-access-database-using-vb-net-form-and-how-to-enter-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!