我試圖插入數據到數據庫中使用登錄窗體中,但代碼似乎不工作。沒有錯誤發生,它看起來像我運行代碼時沒有發生任何事情。無法插入到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
要說沒有任何反應是絕對錯誤的。一些事情總是發生。事實上,你所尋找的東西並不會發生並不意味着什麼都不會發生。首先要測試的是'ExecuteNonQuery'返回的值。如果這不是零,那麼代碼的工作方式應該和它應該的一樣,問題在於你。 – jmcilhinney