在vb.net中获取单值表单数据库 [英] fetching single value form database in vb.net
本文介绍了在vb.net中获取单值表单数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的桌子
id 姓名
1 abc
2 xyz
我输入 1 然后各自的值 abc显示在不同的文本框中???
my table
id name
1 abc
2 xyz
it i enter 1 then respective value "abc" display in different text box???
Private Sub butsea_Click(sender As Object, e As EventArgs) Handles butsea.Click
Dim dset As New DataSet
Dim da As SqlDataAdapter
Dim myCmd As New SqlCommand
Try
myConn.ConnectionString = "Data Source=THEONE\PARTH;Initial Catalog=testdatabase;Integrated Security=True;"
myConn.Open()
Dim avalue As String = (InputBox("Input Student Id", "Search Student")).ToString
txt_id.Text = avalue
da = New SqlDataAdapter("SELECT * FROM studentdetails where student_id= '" & txt_id.Text & "", myConn)
If dset.Tables(0).Rows.Count > 0 Then
'what should i write here
Else
MsgBox("No Record Found")
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
myConn.Close()
End Try
End Sub
推荐答案
If dset.Tables(0).Rows.Count > 0 Then
txt_name.Text = dset.Tables(0).Rows(0)("name").ToString()
Else
MsgBox("No Record Found")
End If
0)不要将数据访问代码放在UI代码中;写一个数据访问层和一个业务逻辑层
1)使用参数化查询,而不是连接
2)不要使用*
如果你只需要一个值(或几个)
3)不要使用DataSet来获得一个值
4)不要使用DataAdapter获取一个值
5)我建议使用ExecuteScalar
0) Don't put data access code in your UI code; write a Data Access Layer and a business logic layer
1) Use parameterized queries, not concatenation
2) Don't use*
if you only want one value (or a few)
3) Don't use a DataSet to get one value
4) Don't use a DataAdapter to get one value
5) I recommend using ExecuteScalar
这篇关于在vb.net中获取单值表单数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文