【发布时间】:2015-11-18 14:47:33
【问题描述】:
现在我有一个问题,无法解决我自己。 我有本地数据库 .SDF 并尝试创建一个登录帐户数据库,其中包含许多用户和密码,也限制了管理员和用户。
我有一个名为 SQLControl 的类,这是代码
Imports System.Data.SqlServerCe
Public Class SQLControl
#Region "Main Declaration"
Dim SQLConn As SqlCeConnection
Dim SQLConnString As String = "Data Source=AdmApotikDatabase.sdf"
Dim SQLCmd As SqlCeCommand
Dim SQLAdapter As SqlCeDataAdapter
Dim SQLTable As DataTable
#End Region
Public Sub LoadData(NewCmdSelect As String)
Try
SQLConn = New SqlCeConnection
SQLCmd = New SqlCeCommand
SQLConn.ConnectionString = SQLConnString
SQLCmd.Connection = SQLConn
SQLCmd.CommandText = NewCmdSelect
SQLAdapter = New SqlCeDataAdapter(SQLCmd)
SQLTable = New DataTable
SQLConn.Open()
SQLAdapter.Fill(SQLTable)
SQLConn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Sub AddData(NewCmdAdd As String)
Try
SQLConn = New SqlCeConnection
SQLCmd = New SqlCeCommand
SQLConn.ConnectionString = SQLConnString
SQLCmd.Connection = SQLConn
SQLCmd.CommandText = NewCmdAdd
SQLConn.Open()
SQLCmd.ExecuteNonQuery()
SQLConn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Sub EditData(NewCmdEdit As String)
Try
SQLConn = New SqlCeConnection
SQLCmd = New SqlCeCommand
SQLConn.ConnectionString = SQLConnString
SQLCmd.Connection = SQLConn
SQLCmd.CommandText = NewCmdEdit
SQLConn.Open()
SQLCmd.ExecuteNonQuery()
SQLConn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Sub FreeCmd(NewCmdFree As String)
Try
SQLConn = New SqlCeConnection
SQLCmd = New SqlCeCommand
SQLConn.ConnectionString = SQLConnString
SQLCmd.Connection = SQLConn
SQLCmd.CommandText = NewCmdFree
SQLConn.Open()
SQLCmd.ExecuteNonQuery()
SQLConn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
现在我想尝试根据我在 'txtUsername' 和 'txtPassword' 文本框中写的内容找到一行。这是我的“pbLogin”点击事件:
Public Class LoginForm
Dim SQLControl As SQLControl
Private Sub pbLogin_Click(sender As Object, e As EventArgs) Handles pbLogin.Click
Dim Username As New String
Dim Password As New String
Dim IsAdmin As New Integer
Dim IsUser As New Integer
If txtUserName.Text <> "" And txtPassword.Text <> "" Then
Dim AdminCmd As String = "SELECT * FROM TabelAkun WHERE " & Username & "='admin' AND " & Password & "='admin' AND " & IsAdmin & "= 1"
SQLControl.FreeCmd(AdminCmd)
If txtUserName.Text = Username And txtPassword.Text = Password Then
SQLControl.LoadData("")
MainWindows.pbAbout.Visible = True
MainWindows.pbAccount.Visible = True
MainWindows.pbDataObat.Visible = True
MainWindows.pbDataSuplier.Visible = True
MainWindows.pbDataTransaksi.Visible = True
End If
Else
MsgBox("Tidak boleh kosong !")
End If
End Sub
End Class
它只是在 'SQLControl.FreeCmd(AdminCmd) 处导致 'NullReferenceException' 这是我的表,名为“TabelAkun”,这是它的列:
Dim Username As New String
Dim Password As New String
Dim IsAdmin As New Integer
Dim IsUser As New Integer
我需要有人根据我的班级 Sub 来纠正我上面称为“pbLogin”的点击按钮事件,谢谢 :)
【问题讨论】:
标签: vb.net