【问题标题】:Visual Basic 2015 (Visual Studio)Visual Basic 2015 (Visual Studio)
【发布时间】:2016-08-03 05:44:16
【问题描述】:

我正在尝试在 Visual Studio 中使用 Visual Basic 2015 创建登录表单。我已经按照我看过的视频中的说明进行操作,但是,当我尝试运行代码时发生了错误。

这是我到目前为止所做的代码:


Private Sub picgo1_Click(sender As Object, e As EventArgs) Handles picgo1.Click

 openConn()

        Dim dr As SqlDataReader
        Dim cmd As SqlCommand
        Dim sqlsyntax As String
        cmd = New SqlCommand
        cmd.CommandType = CommandType.Text
        cmd.Connection = conn
        sqlsyntax = "select * from tblusers where user = '" & txtuser.Text & "' and pass = '" & txtpass.Text & "'"
        cmd.CommandText = sqlsyntax
        dr = cmd.ExecuteReader()

If dr.HasRows Then

 MsgBox("Access Granted! Welcome '" & txtuser.Text & "'")

Else

 MsgBox("Access Denied! Incorrect Username or Password!")

End If

 conn.Close()

 cmd.Dispose()

    End Sub

另一个模块

Imports System.Data.SqlClient

Module ModuleConnections
    Public conn As SqlConnection

    Sub openConn()
        Try
            conn = New SqlConnection("Data Source=E:\HRIMS\HRIMS V1.0\WINDOWSAPPLICATION2\HRIMSDB.MDF;Integrated security=True")
            If conn.State = ConnectionState.Closed Then
                conn.Open()
            End If
        Catch ex As Exception
            MsgBox("Connecting to Database Failed" & ex.ToString)
        End Try
    End Sub

End Module

当我尝试运行表单时,这是我得到的error。然后当我按下确定时,它会将我指向这个line

我还在努力学习,所以请不要对我太苛刻:D

提前谢谢你。

【问题讨论】:

  • 您是否还从视频中复制了连接字符串(conn=New SqlConnection...),还是指向您自己的数据库?
  • @ehh,那是我的。这部分具体是“E:\HRIMS\HRIMS V1.0\WINDOWSAPPLICATION2\HRIMSDB.MDF”。其余部分是从视频中复制的。
  • @Luke,我明白了,但是,我不知道出了什么问题。我能够创建我的数据库而没有任何错误。桌子也准备好了,所以我不知道下一步该做什么。这一行“conn = New SqlConnection("Data Source=E:\HRIMS\HRIMS V1.0\WINDOWSAPPLICATION2\HRIMSDB.MDF;Integrated security=True")”是我的主要问题吗?我应该为数据源添加什么链接?谢谢。
  • 尚未使用 .mdf 数据库。我总是建议使用 SQL 数据库(例如 SQL EXPRESS),但这是另一个话题。您是否尝试过使用用户名/密码并检查文件权限?此外,您在文件路径中使用了一个空格 - 尝试删除它?这可能会导致错误。
  • 您的连接字符串完全错误。 MDF 文件本身没有用。它们必须附加到某种 SQL Server 实例,无论是完整的、Express 还是 LocalDB。我不知道,但我猜 LocalDB 是随 VS 安装的。访问 www.connectionstrings.com 了解如何编写 SQL Server 连接字符串。

标签: vb.net


【解决方案1】:

试试:

Public conn As SqlConnection

Public Sub openConn()
        conn = New SqlConnection(CONNECTIONSTRING)
        If conn.State = ConnectionState.Closed Then
            conn.Open()
        End If
        Return conn
End Function

登录代码:

 Using cmd As New SqlCommand("SELECT * 
                             FROM tblusers 
                             WHERE user = @userName 
                             AND pass = @userPass", openConn)
   'Parameterize your query to be safe from SQL Injection
   cmd.Parameters.AddWithValue("@userName", txtuser.Text)
   cmd.Parameters.AddWithValue("@userPass", txtpass.Text)
   Using rdr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult)
       Dim Login As Object = 0
       If rdr.HasRows Then
           rdr.Read
           Login = rdr(Login)
       End If
       If Login = Nothing Then
           MsgBox("Access Denied! Incorrect Username or Password!")
       Else
           'MsgBox("Access Granted! Welcome '" & txtuser.Text & "'")
           MsgBox(String.Format("Access Granted! Welcome {0}!", txtuser.text)
       End If
    End Using
End Using

【讨论】:

  • OP 的连接字符串有问题。您刚刚删除了有问题的连接字符串,这有什么帮助?
  • @Jeroen,感谢兄弟提供的信息。我的笔记本电脑昨天被拉出来保修。突然无缘无故死了,所以我没有机会按照您提供的链接进行操作。拿回笔记本电脑后,我会尽快与您联系。再次感谢您。
猜你喜欢
  • 1970-01-01
  • 2016-02-02
  • 1970-01-01
  • 1970-01-01
  • 2016-07-19
  • 2015-10-05
  • 1970-01-01
  • 2020-01-21
  • 1970-01-01
相关资源
最近更新 更多