嗨,有人可以帮我解决我的连接字符串,我可以浏览我的数据库,但我无法向其中添加数据。在此先感谢我的代码在下面,如果有人对我有所了解,那就太好了。所以我可以导航,但我不能添加数据或更新数据。我确实曾经有过这样的工作,但从那以后就开始了。要是我做了备份就好了:(
Imports System.Data.OleDb
公共类 adminPanel
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim ds As New DataSet
Dim sql As String
Dim da As New OleDb.OleDbDataAdapter
Dim inc As Integer
Dim MaxRows As Integer
Private Sub adminPanel(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Builder As New OleDb.OleDbConnectionStringBuilder With
{
.ConnectionString = My.Settings.Database1
}
' Change it
Builder.DataSource = IO.Path.Combine(Application.StartupPath, "Database1.mdb")
lblName.Text = LoginForm.txtUserName.Text
If lblName.Text = LoginForm.txtUserName.Text Then
Builder.Add("Jet OLEDB:Database Password", "password")
Using con As New OleDb.OleDbConnection With {.ConnectionString = Builder.ConnectionString}
sql = "SELECT * FROM tblContacts"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "Database1")
'MsgBox("Database is now open")
MaxRows = ds.Tables("Database1").Rows.Count
inc = -1
End Using
End If
End Sub
Private Sub NavigateRecords()
UserName.Text = CStr(ds.Tables("Database1").Rows(inc).Item(1))
UserPassword.Text = CStr(ds.Tables("Database1").Rows(inc).Item(2))
UserTimer.Text = ds.Tables("Database1").Rows(inc).Item(3).ToString
End Sub
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
UserName.Text = CStr(ds.Tables("Database1").Rows(inc).Item(1))
UserPassword.Text = CStr(ds.Tables("Database1").Rows(inc).Item(2))
UserTimer.Text = CStr(ds.Tables("Database1").Rows(inc).Item(3))
da.Update(ds, "Database1")
MsgBox("Data updated")
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
If inc <> MaxRows - 1 Then
inc = inc + 1
NavigateRecords()
Else
MsgBox("No More Rows")
End If
End Sub
Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
If inc > 0 Then
inc = inc - 1
NavigateRecords()
Else
MsgBox("First Record")
End If
End Sub
Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click
If inc <> MaxRows - 1 Then
inc = MaxRows - 1
NavigateRecords()
End If
End Sub
Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click
If inc <> 0 Then
inc = 0
NavigateRecords()
End If
End Sub
Private Sub adminPanel_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
MsgBox("Data cleared")
btnCommit.Enabled = False
btnAddNew.Enabled = True
btnUpdate.Enabled = True
btnDelete.Enabled = True
inc = 0
NavigateRecords()
End Sub
Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles btnAddNew.Click
btnCommit.Enabled = True
btnUpdate.Enabled = False
btnDelete.Enabled = False
btnAddNew.Enabled = False
UserName.Clear()
UserPassword.Clear()
UserTimer.Clear()
End Sub
Private Sub btnCommit_Click(sender As Object, e As EventArgs) Handles btnCommit.Click
If inc <> -1 Then
Dim con As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("Database1").NewRow()
dsNewRow.Item("UserName") = UserName.Text
dsNewRow.Item("UserPassword") = UserPassword.Text
dsNewRow.Item("UserTimer") = UserTimer.Text
ds.Tables("Database1").Rows.Add(dsNewRow)
da.Update(ds, "Database1")
MsgBox("New Record added to the Database")
btnCommit.Enabled = False
btnAddNew.Enabled = True
btnUpdate.Enabled = True >
btnDelete.Enabled = True
End If
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
If MessageBox.Show("Do you really want to Delete this Record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
MsgBox("Operation Cancelled")
Exit Sub
End If
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("Database1").Rows(inc).Delete()
MaxRows = MaxRows - 1
inc = 0
da.Update(ds, "Database1")
NavigateRecords()
End Sub
结束类