无法插入MS Access数据库

时间:2017-04-25 07:20:49

标签: vb.net 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

1 个答案:

答案 0 :(得分:2)

我不确定您尝试使用连接字符串但是您不需要此部分:

;_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 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,可能会就如何做到这一点给你一些想法。