请问,如何用SqlDataSource1控件,直接提取SQL库里的2个字段的值呢??

tgc99 2009-08-25 01:05:20
str = "select * from mytb where username='" & txt1& "' and password='" & txt2 & "'"

SqlDataSource1.SelectCommand = str

SqlDataSource1.Select

我不想绑定控件,只是想取到select 到的username,password 这2个值

或者,判断这个select 查询结果是否有正确的记录,,然后我想登录到另一个窗口
...全文
163 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
tgc99 2009-08-25
  • 打赏
  • 举报
回复
我试了一下,返回值为-1呢

然后又查了一下::ExecuteNonQuery()是update,delete ,insert影响行数,对于select永远返回-1


[Quote=引用 5 楼 wtnu200 的回复:]
其实你可以这样做,不用那么麻烦,就是自己代码连接到数据库.用ExecuteNonQuery执行你的sql语句,它会返回影响的行数,你再判断就行了.下面是我写的.你可以自己看下.
str = "select * from mytb where username='" & txt1& "' and password='" & txt2 & "'"
dim con as New System.Data.SqlClient.SqlConnection(加连接语句)
dim com as new System.Data.SqlClient.sqlcommon
com.connection=con
com.commomtext=str
con.open
if  com.ExecuteNonQuery>0 then
msgbox("登录成功")
else
msgbox("登录失败")
end if 
con.close

不懂可以加我Q451603829
       
[/Quote]
xunis 2009-08-25
  • 打赏
  • 举报
回复
datatable dataset 其实是一样的
dataset 包含datatable
你想用dataset == 我给你写个


 Public Function GetDataSet(ByVal strSQL As String) As DataSet
Dim ds As DataSet = New DataSet '聲明數據表 Dim ds As New DataSet
Try
'初始化適配器
Dim da As SqlDataAdapter
'打開數據庫鏈接
If g_CN Is Nothing Then
GetConnection()
End If
ds.Clear()
'將數據庫和適配器鏈接在一起
da = New SqlDataAdapter(strSQL, g_CN)
'將數據放到數據集中
da.Fill(ds)
Return ds
Catch ex As Exception
MessageBox.Show("GetDataSet出错!")
End Try
Return ds
End Function


strSQL = "SELECT * FROM VWEmployees WHERE DepID like '%" & trvData.SelectedNode.Name & "%' order by EmpID"
Dim ds As DataSet = GetDataSet(strSQL)
If ds.Tables.Item(0).Rows.Count = 0 Then
MessageBox.Show("资料不存在")
Else
'显示数据
Me.dgvShowData.DataSource = ds.Tables.Item(0).DefaultView
'中文标题
chineseTitle()
End If
xunis 2009-08-25
  • 打赏
  • 举报
回复
 Try
parentPage.TreeView1.Enabled = True
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try

End If


这段你可以换成 查询数据存在 (我这里是登录成功了) 存在了做什么的代码
xunis 2009-08-25
  • 打赏
  • 举报
回复
  Public Function quaryUser() As Boolean
'判断窗体是否为空
Dim strSql As String = ""
If cmbUid.Text = "" Or txtPwd.Text = "" Then
MessageBox.Show("帐号或密碼不能為空!")
Return False
'獲取數據庫鏈接
Else
'校驗密碼和用戶名的SQL語句
strSql = "SELECT * FROM Users WHERE UserID = '" + cmbUid.Text + "' AND PASSWORD = '" + txtPwd.Text + "'"

Dim dt As DataTable = cls.GetDataTable(strSql) '模块的方法在此的调用 给你发下面
If dt.Rows.Count = 0 Then
MessageBox.Show("密碼或用戶名錯誤!", "錯誤提示!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
Else
Try
parentPage.TreeView1.Enabled = True
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try

End If
End If
End Function


'执行查询,并将查询结果封装成DataTable
Public Function GetDataTable(ByVal strSQL As String) As DataTable
Dim dtTable As New DataTable
Dim daAper As SqlDataAdapter
Dim gCN As SqlConnection = GetConnection()
daAper = New SqlDataAdapter(strSQL, gCN)
daAper.Fill(dtTable)
GetDataTable = dtTable
gCN.Close()
gCN = Nothing
End Function



tgc99 2009-08-25
  • 打赏
  • 举报
回复
请问,如何搜索SELECT 语句,,,才可以进行到这一句?dataset又如何操作呢??-----if ds.Tables.Item(0).Rows.Count
mbh0210 2009-08-25
  • 打赏
  • 举报
回复

str = "select Count(*) as count from mytb where username='" & txt1& "' and password='" & txt2 & "'"


这样也可以,判断count 是否等于1即可
水哥阿乐 2009-08-25
  • 打赏
  • 举报
回复
方法真多,都顶
wtnu200 2009-08-25
  • 打赏
  • 举报
回复
其实你可以这样做,不用那么麻烦,就是自己代码连接到数据库.用ExecuteNonQuery执行你的sql语句,它会返回影响的行数,你再判断就行了.下面是我写的.你可以自己看下.
str = "select * from mytb where username='" & txt1& "' and password='" & txt2 & "'"
dim con as New System.Data.SqlClient.SqlConnection(加连接语句)
dim com as new System.Data.SqlClient.sqlcommon
com.connection=con
com.commomtext=str
con.open
if com.ExecuteNonQuery>0 then
msgbox("登录成功")
else
msgbox("登录失败")
end if
con.close

不懂可以加我Q451603829
dylike 2009-08-25
  • 打赏
  • 举报
回复
虽然不懂数据库,但仍需要友情支持
☆☆☆☆☆■☆☆☆☆☆☆☆☆☆■☆☆☆☆■☆☆☆☆
■■■■■■■■■■■☆☆☆☆■☆☆■■■■■☆☆
☆☆☆☆☆■☆☆☆☆☆☆☆■■■■☆☆☆■☆☆☆☆
☆☆☆☆☆■☆☆☆☆☆☆☆☆☆■☆■■■■■■■☆
☆■■■■■■■■■☆☆☆☆☆■☆☆☆☆☆■☆☆☆
☆☆■☆☆☆☆☆☆■☆☆☆☆☆■■☆■■■■■■☆
☆☆☆■☆☆☆☆■☆☆☆☆■■■☆☆☆☆☆■☆☆☆
☆☆☆☆■☆☆■☆☆☆☆☆☆☆■☆☆■☆☆■☆☆☆
☆☆☆☆☆■■☆☆☆☆☆☆☆☆■☆☆☆■☆■☆☆☆
☆☆☆■■☆☆■■☆☆☆☆☆☆■☆☆☆☆☆■☆☆☆
■■■☆☆☆☆☆☆■■☆☆☆■■☆☆☆■■■☆☆☆
☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
[DYLIKE TEXT TO IMAGE-TEXT]
泡面之夏 2009-08-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 forrest23 的回复:]
将这个查询语句str = "select * from mytb where username='" & txt1& "' and password='" & txt2 & "'" 的查询记录赋给一个dataset比如ds,如果ds.table(0).rows.count>0 则代表是正确的记录
[/Quote]

str = "select username,password from mytb where username='" & txt1& "' and password='" & txt2 & "'"


把结果集用DataTable或者是dataset来接受,
然后判断这个结果集是否为空;
在做相应的操作;
Forrest23 2009-08-25
  • 打赏
  • 举报
回复
将这个查询语句str = "select * from mytb where username='" & txt1& "' and password='" & txt2 & "'" 的查询记录赋给一个dataset比如ds,如果ds.table(0).rows.count>0 则代表是正确的记录
xunis 2009-08-25
  • 打赏
  • 举报
回复
 If ds.Tables.Item(0).Rows.Count > 0 Then
lblExpand.Text = lblExpand.Text & " 考号:" & (ds.Tables(0).Rows(0)(0).ToString)
lblExpand.Text = lblExpand.Text & " 姓名:" & (ds.Tables(0).Rows(0)(1).ToString)
'密码栏位
'lblExpand.Text = lblExpand.Text & " 密码:" & (ds.Tables(0).Rows(0)(2).ToString)
'性别处理
Dim strSex As String = ""
If ds.Tables(0).Rows(0)(3).ToString = "1" Then
strSex = "男"
Else
strSex = "女"
End If
lblExpand.Text = lblExpand.Text & " 性别:" & strSex
End If

ds.Tables(0).Rows(0)(0).ToString


16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧