Imports System.Configuration
Imports WindowsApplication1.commonLayer.utilityPackage
Namespace commonLayer.utilityPackage
Public Class config
Public strConn As String = ConfigurationManager.ConnectionStrings("WindowsApplication1.My.MySettings.WindowsApplication1ConnectionString").ConnectionString
End Class
End Namespace
ใน ConnectionLayer
ConnectionManage
Imports System.Data
Imports System.Data.OleDb
Imports System.Configuration
Imports WindowsApplication1.commonLayer.utilityPackage
Namespace ConnectionLayer
Public Class ConnectionManager
Private conString As New config
Public Function GetDate(ByVal sSql As String) As DataTable
Try
Dim myAdapter As New OleDbDataAdapter(sSql, conString.strConn)
Dim mydate As New DataTable
myAdapter.Fill(mydate)
Return mydate
Catch sqlex As OleDbException
Throw sqlex
Catch ex As Exception
Throw ex
End Try
End Function
Public Function ExecNonQuery(ByVal strSQL As String)
Dim cn As OleDbConnection
cn = New OleDbConnection
Try
With cn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = conString.strConn
.Open()
End With
Dim cmd As OleDbCommand = New OleDbCommand(strSQL, cn)
Return cmd.ExecuteNonQuery()
Catch sqlex As OleDbException
Throw sqlex
Catch ex As Exception
Throw ex
Finally
cn.Close()
End Try
End Function
Public Function ExecScalar(ByVal strSQL As String)
Dim cn As OleDbConnection
cn = New OleDbConnection
Try
With cn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = conString.strConn
.Open()
End With
Dim cmd As OleDbCommand = New OleDbCommand(strSQL, cn)
Return cmd.ExecuteScalar
Catch sqlex As OleDbException
Throw sqlex
Catch ex As Exception
Throw ex
Finally
cn.Close()
End Try
End Function
End Class
End Namespace
userManage
Imports System.Data
Imports System.Data.OleDb
Imports WindowsApplication1.ConnectionLayer
Namespace ConnectionLayer
Public Class UserManager
Private conMng As New ConnectionManager
Public Function getUser(ByVal userName As String, ByVal password As String) As DataTable
Dim sqlstring As String = "SELECT Username , password from Tbl_User where Username = '" & userName & "' and password = '" & password & "'"
Dim result As DataTable = conMng.GetDate(sqlstring)
Return result
End Function
End Class
End Namespace
ใน BusinessLayer
ฺBizUser
Imports Microsoft.VisualBasic
Imports WindowsApplication1.ConnectionLayer
Imports WindowsApplication1.ObjectLayer
Imports System.Data
Namespace BusinessLayer
Public Class BizUser
Private userMng As New UserManager
Public Function getUser(ByVal userName As String, ByVal password As String) As User
Dim objuser As New User
Dim dtUser As DataTable = userMng.getUser(userName, password)
If dtUser.Rows.Count > 0 Then
objuser.Username = dtUser.Rows(0).Item("Username")
objuser.Password = dtUser.Rows(0).Item("Password")
End If
Return objuser
End Function
End Class
End Namespace
ในหน้า form
หน้า form
Imports System.Data
Imports System.Data.OleDb
Imports Microsoft.VisualBasic
Imports WindowsApplication1.ObjectLayer
Imports WindowsApplication1.commonLayer.utilityPackage
Imports WindowsApplication1.BusinessLayer
Public Class Form1
Private bizUserr As New BizUser
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim username As String = TextBox1.Text
Dim password As String = TextBox2.Text
Dim objUser As User = bizUserr.getUser(username, password)
If objUser.Username = username And objUser.Password = password Then
MsgBox("เข้าสู่ระบบ")
End If
End Sub
End Class
ใน objectLayer
User
Namespace ObjectLayer
Public Class User
Private pUsername As String
Private PPassword As String
Public Property Username As String
Get
Return pUsername
End Get
Set(ByVal value As String)
pUsername = value
End Set
End Property
Public Property Password As String
Get
Return PPassword
End Get
Set(ByVal value As String)
PPassword = value
End Set
End Property
End Class
End Namespace