如何将Access数据库的密码附加到Vb.Net存储库和表单代码 [英] How to append Access Database's password to Vb.Net repository and form code
问题描述
您好,我无法再次进行此工作.在它工作之前,但是当我用密码保护数据库时,它停止工作.
以我的登录格式,如果用户名和密码匹配,它将通过以下代码传递用户名:
Hi im having trouble making this work again. Before it is working but then when i protected my database with a password it stopped working.
in my login form, if a username and password match, it will pass the usernamme by this code:
'passed details starts here
Dim Obj As New UserAccount
Obj.PassedText = UNameTB.Text
'end
这是 UserAccount 表格:
This is the UserAccount Form:
Public Class UserAccount
Private mstrConn As String = _
"Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & Application.StartupPath & "\InfoDB.accdb;" & _
"Persist Security Info=False;" & _
"Jet OLEDB:Database Password=adminGMRT;"
Private _CurrentUser As UserData
Private _MyDB As String = Application.StartupPath & "\InfoDB.accdb"";Jet OLEDB:Database Password=adminGMRT;"
Private _Repository As MyRepository = New MyRepository(_MyDB)
Private PositionDB As String
Public Property [PassedText]() As String
Get
Return IIf(_CurrentUser Is Nothing, "", _CurrentUser.UserName)
End Get
Set(ByVal Value As String)
_CurrentUser = _Repository.GetUser(Value)
If _CurrentUser IsNot Nothing Then RegisterLbl.Text = _CurrentUser.FirstName + " " + _CurrentUser.LastName + "!"
PositionDB = _CurrentUser.Position
End Set
End Property
Private Sub UserAccount_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
存储库类
The Repository class
Imports System.Data.OleDb
Public Class MyRepository
Private _connection As OleDbConnection
Private _dbPath As String
Private Function AccessConnectionString(ByVal DatabasePath As String)
Return New OleDbConnectionStringBuilder() With {
.Provider = "Microsoft.ACE.OLEDB.12.0",
.DataSource = DatabasePath,
.PersistSecurityInfo = False
}.ConnectionString
End Function
Public Sub New(ByVal DatabasePath As String)
_dbPath = DatabasePath
_connection = New OleDbConnection(AccessConnectionString(DatabasePath))
End Sub
Public Function GetUser(ByVal UserName As String) As UserData
Dim dt As DataTable = FillTable(
"SELECT * FROM DBtable WHERE UserName = @UserName",
{New OleDbParameter("@UserName", UserName)}
)
If dt.Rows.Count = 0 Then
Return Nothing
Else
Return UserFromRow(dt.Rows(0))
End If
End Function
Public Function GetUsers() As IEnumerable(Of UserData)
Return From row In FillTable("SELECT * FROM DBtable")
Select UserFromRow(row)
End Function
Private Function FillTable(ByVal CommandText As String, Optional ByVal Parms() As OleDbParameter = Nothing) As DataTable
Dim dt As New DataTable
Using adp = New OleDbDataAdapter(CommandText, _connection)
If Parms IsNot Nothing Then
adp.SelectCommand.Parameters.AddRange(Parms)
End If
adp.Fill(dt)
End Using
Return dt
End Function
Private Function UserFromRow(ByVal row As DataRow) As UserData
Return UserData.CreateUser(
DBValue(Of String)(row("UserName")),
DBValue(Of String)(row("PassCode")),
DBValue(Of String)(row("FirstName")),
DBValue(Of String)(row("LastName")),
DBValue(Of String)(row("Position"))
)
End Function
Private Function DBValue(Of T)(ByVal value As Object) As T
If value Is DBNull.Value Then
Return CType(Nothing, T)
Else
Return CType(value, T)
End If
End Function
End Class
UserData类
the UserData class
Public Class UserData
Public Property UserName As String
Public Property Password As Security.SecureString
Public Property FirstName As String
Public Property LastName As String
Public Property Position As String
Private Sub New()
End Sub
Public Shared Function CreateUser(ByVal Username As String, ByVal Password As String, ByVal FirstName As String, ByVal LastName As String, ByVal Position As String) As UserData
Return New UserData() With {
.UserName = Username,
.Password = ConvertToSecure(Password),
.FirstName = FirstName,
.LastName = LastName,
.Position = Position
}
End Function
Private Shared Function ConvertToSecure(ByVal value As String) As Security.SecureString
Dim s As New Security.SecureString
If value IsNot Nothing Then
For Each c As Char In value
s.AppendChar(c)
Next
End If
Return s
End Function
End Class
登录sn工作.当我运行此命令时,登录名显示为成功".正在显示用户帐户表单.
但是,UserAccount中有一个标签,必须将其更改为已登录用户的名称.正如我上面所说的,它是在我将密码输入数据库之前显示的.所以我想错误是在UserData和Repository中数据库连接字符串的编码中.我已经调试了好几个小时,但是我找不到它.代码没有显示任何错误,但是它也没有输出用户名…….请帮助我,我迷失了所有这些代码.
The login sn working. When i run this, the login shows "Successful". The User Account Form is Showing.
But then there is a label in UserAccount that must be change to the Name of the logged-in user. As i have said above, it is showing before i put a password in my database. So i guess the mistake will be in the coding of database connection string in UserData and Repository. I have been debugging this for hours but i cant find it.. The code is not showing any error, but it does not output the name of the user either.... Help me please, im getting lost with all these codes..
推荐答案
Private _MyDB As String = Application.StartupPath & "\InfoDB.accdb"";Jet OLEDB:Database Password=adminGMRT;"
此行在您的连接字符串中添加了不必要的"...
This line here throws an unnecessary " into your connection string...
这篇关于如何将Access数据库的密码附加到Vb.Net存储库和表单代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!