sql-server - VB.NET 数据表行
问题描述
我正在尝试制作登录表单。
我在我的服务器上创建了一个数据库并创建了行用户名和密码。然后我创建了一个root用户,密码为root。
但我在检查用户名和密码是否正确时遇到问题,我不知道如何给他 2 行。
Dim conn = New SqlConnection("Data Source=SRV-SQL;Initial Catalog=prova;User ID=user;Password=user")
Dim sda = New SqlDataAdapter("select count(*) from tblLogin where username ='" + txtUsername.Text + "' and password='" + txtUserPwd.Text + "'", conn)
Dim dt = New DataTable()
sda.Fill(dt)
If (dt.Rows().ToString() = "1") Then
MsgBox("Logged-in successfully")
Else
MessageBox.Show("The username or the password is wrong!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
桌子:
解决方案
在线评论和解释。
Private Sub VerifyLogin()
'For the Return Value of the command
Dim RetVal As Integer
' A Using...End Using will ensure that you connectionis closed and disposed event
'it there is an error.
Using conn = New SqlConnection("Data Source=SRV-SQL;Initial Catalog=prova;User ID=user;Password=user")
'You don't need a DataAdapter, just a command
'USE PARAMETERS. Yes, I am yelling :-) Even if you are the only user
'it will save you headaches with syntax.
Using cmd = New SqlCommand("select count(*) from tblLogin where username = @UserName and password= @Password;", conn)
cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = txtUsername.Text
cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value = txtUserPwd.Text
'You are only returning one row
'ExecuteScalar returns the value in the first column of the
'first row of the the data
conn.Open()
RetVal = CInt(cmd.ExecuteScalar)
End Using
End Using
'No need to convert to a string just compare the Integer
If RetVal = 1 Then
MsgBox("Logged-in successfully")
Else
MessageBox.Show("The username or the password is wrong!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
推荐阅读
- c++ - static_cast
与 static_cast - python - Django set_language 请求工作但不改变语言
- javascript - 时间为上午 10 点时重新启动倒数计时器,倒数计时器基于数据库中的数据
- spring - 为什么我在创建 keycloak 用户(spring boot)时会收到 http 409?
- woocommerce - woocommerce 产品数据元框中的 Cutsom Checkbox 字段
- flutter - 将整数四舍五入到最接近的千位
- java - 使用springboot在mongodb中进行多个搜索查询
- javascript - TypeError:映射到两个数组时无法读取未定义的属性“id”
- java - 调用一个类“如果”
- extjs - 为什么框架版本 7.3 无法在 Sencha 支持站点上下载?