vb.net数据库查询

来源:百度知道 编辑:UC知道 时间:2024/09/28 10:26:07
小弟要做一个登陆查询,两个textbox。1个叫txtusername,1个叫txtpassword,我只会C#,所以照猫画虎的模拟了一个vb的登陆,可是登陆失败,好像是数据库查询的语句有错,请教哪位大哥给在下指点迷津。
我的代码如下:
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub txtlogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtlogin.Click

Dim strSource As String = "Data Source =(local);Database=DB;uid=sa;pwd=password"

Dim strSql As String = "select * from Users where username =" & txtusername.Text.Trim() & "and password=" & txtpassword.Text.Trim()

Dim conn As SqlConnection = New SqlConnection(strSource)

Dim cmd As SqlCommand = New SqlCommand(strSql, conn)

Try
conn.Open()

Dim dr As SqlDataReader = cmd.ExecuteReader()

If dr.HasRows Then

MsgBox("登陆成功了阿")

End If

’引用命名空间
Imports System
Imports System.Data
Imports System.Data.SqlClient

‘下面是在txtlogin下的密码验证代码
Dim cn As New SqlConnection("data source=(local);initial catalog= 你的数据库名称;uid=sa;pwd=你的数据库密码;integrated security=sspi")’链接数据库

‘验证
cn.Open()
dim cm as New SqlCommand("select count(*) from Users where username ='" & txtusername.Text.Trim() & "' and password='" & txtpassword.Text.Trim() & "'", cn)
Dim count As Integer = cm.ExecuteScalar
If count >0 Then
msgbox("登陆成功!")
Else
msgbox("帐号或密码不正确")
End If
cn.close