无法插入到MS Access数据库

问题描述:

我试图插入数据到数据库中使用登录窗体中,但代码似乎不工作。没有错误发生,它看起来像我运行代码时没有发生任何事情。无法插入到MS Access数据库

代码:

Imports System.Data.OleDb 

Public Class Form2 
Dim provider As String 
Dim datafile As String 
Dim connstring As String 
Dim myconnection As OleDbConnection = New OleDbConnection 
Dim Mode As String 

Private Sub btnSignin_Click(sender As Object, e As EventArgs) Handles btnSignin.Click 


    ' Enable the Insert/Update/Delete buttons. 
    btnSignin.Enabled = True 

    ' Test for the Mode of the form and perform the necessary actions accordingly. 
    Select Case Mode 
     Case "INSERT" ' If the mode is INSERT, perform a new record insertion. 
      Try 
       ' Declare the connection and command objects. 
       Dim connection As New OleDb.OleDbConnection 
       Dim command As New OleDb.OleDbCommand 

       ' Set the ConnectionString property and open the database connection. 
       connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Dentist Pat 2\Dentist Pat 2\Users.accdb;_Name=;_Surname=;_Username=;_Password=;" 
       connection.Open() 

       ' Set the Connection and CommandText properties of the command. 
       command.Connection = connection 
       command.CommandText = "INSERT INTO Users (_Name,_Surname,_Password,_Username) VALUES (@Name,@Surname,@Password,@Username)" 

       ' Declare and initialize the parameters required by the command object. 
       Dim parName = New OleDb.OleDbParameter("@Name", txtName.Text) 
       Dim parSurname = New OleDb.OleDbParameter("@Surname", txtSurname.Text) 
       Dim parUsername = New OleDb.OleDbParameter("@Username", txtUsername.Text) 
       Dim parPassword = New OleDb.OleDbParameter("@Password", txtPassword.Text) 

       ' Add the parameters to the command object. 
       command.Parameters.Add(parName) 
       command.Parameters.Add(parSurname) 
       command.Parameters.Add(parUsername) 
       command.Parameters.Add(parPassword) 

       ' Execute the command. 
       command.ExecuteNonQuery() 

       ' Close the database connection. 
       connection.Close() 

      Catch ex As Exception 
       ' Prompt the user with the exception. 
       MessageBox.Show("Sign In Complete") 
      End Try 
    End Select 
    Me.Hide() 
    Form3.ShowDialog() 
End Sub 
End Class 
+2

要说没有任何反应是绝对错误的。一些事情总是发生。事实上,你所寻找的东西并不会发生并不意味着什么都不会发生。首先要测试的是'ExecuteNonQuery'返回的值。如果这不是零,那么代码的工作方式应该和它应该的一样,问题在于你。 – jmcilhinney

我不知道你与你的连接字符串尝试什么,但你并不需要这一部分:

;_Name=;_Surname=;_Username=;_Password=; 

而是去掉上面:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Dentist Pat 2\Dentist Pat 2\Users.accdb; 

有关连接字符串的更多信息,请参阅www.connectionstrings.com/access

如在comments中所述,请考虑检查ExecuteNonQuery()返回的值以查看该行是否已插入。

使用MS Access时,参数的顺序非常重要,而不是名称。在使用参数时,我在我的SQL命令中使用了?占位符。我也指定了数据类型,所以请考虑使用OleDbParameter Constructor (String, OleDbType)构造函数。在我的示例中,我使用了VarChar,您可能需要相应地进行更改。

我也会考虑实施Using

管理资源由.NET Framework垃圾收集器(GC),无需您任何额外的编码处理。您不需要使用受管资源的块。但是,仍然可以使用Using块强制处理托管资源,而不是等待垃圾回收器。

您的代码看起来类似于这样:

Using con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Dentist Pat 2\Dentist Pat 2\Users.accdb;"), 
     cmd As New OleDbCommand("INSERT INTO Users (_Name, _Surname, _Password, _Username) VALUES (?, ?, ?, ?)", con) 

    con.Open() 

    cmd.Parameters.Add(New OleDbParameter("@Name", OleDbType.VarChar)).Value = txtName.Text 
    cmd.Parameters.Add(New OleDbParameter("@Surname", OleDbType.VarChar)).Value = txtSurname.Text 
    cmd.Parameters.Add(New OleDbParameter("@Username", OleDbType.VarChar)).Value = txtUsername.Text 
    cmd.Parameters.Add(New OleDbParameter("@Password", OleDbType.VarChar)).Value = txtPassword.Text 

    Dim rowsAffected As Integer = cmd.ExecuteNonQuery() 

    If rowsAffected = 1 Then 
     MessageBox.Show("Sign In Complete") 
    End If 
End Using 

正如你所看到的,整体的代码是一个很大整洁和很多更容易阅读。我希望这可以帮助你解决问题。

这是超出了这个问题的范围,但我也看看加密密码。将它们存储为纯文本是不好的做法。看看这个SO问题; Best way to store password in database,这可能会给你一些关于如何最好地做到这一点的想法。