【问题标题】:Find a row according from textbox input根据文本框输入查找一行
【发布时间】: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


【解决方案1】:

如果您按原样在代码中的该行设置断点,并查看SQLControl 变量的内容,您会看到它设置为Nothing,因为您从未创建过它的新实例.

将您的线路 Dim SQLControl As SQLControl 更改为 Dim SQLControl As New SQLControl

【讨论】:

  • 我将 SQLControl 设置为新 SQLControl,现在从异常消息中我得到“解析查询时出错。[令牌行号 = 1,令牌行偏移量 = 32,令牌错误 = = ],我认为它与我的查询有关,有什么想法吗?
  • 如果您的列名为UsernamePasswordIsAdmin,那么您的查询应该类似于SELECT * FROM TabelAkun WHERE Username = '" &amp; Username &amp; "' AND Password = '" &amp; Password &amp; "' AND IsAdmin = " &amp; IsAdmin。总而言之,你真的应该考虑参数化你的查询,在这个网站上和通过谷歌搜索都有很多很棒的文章可以帮助你解决这个问题。它可能会纠正您在查询中遇到的任何问题。
猜你喜欢
  • 2015-12-14
  • 1970-01-01
  • 1970-01-01
  • 2020-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-22
  • 2022-01-12
相关资源
最近更新 更多