【问题标题】:VB.net in visual studio-how to retrieve specific data from database?Visual Studio中的VB.net-如何从数据库中检索特定数据?
【发布时间】:2014-11-06 17:45:54
【问题描述】:

我正在使用 Visual Studio 通过 VB.net 创建一个窗口基础系统,并且数据库是 Visual Studio 中的向导。如何检索特定数据集并在文本框中显示?例如,我有 我的数据库中的用户名、用户名和地址。我想在输入用户 ID 时显示用户名和地址。

【问题讨论】:

  • 最好尝试一些教程

标签: vb.net visual-studio sqlcommand


【解决方案1】:

根据您的描述,我知道 UserId 将是一个输入(比如说:Text1.text)。

1.您需要连接到您的数据库。

Dim Conn As New SqlConnection
Conn.ConnectionString = "your connection string"
Conn.Open()

2.传递您的查询。如果需要,请考虑对您的 UserId 进行验证

Dim cmd As New SqlCommand
cmd.Connection = Conn
cmd.CommandText = "Select username,address from YourTable where Userid = '" & Text1.Text & "'"

3.在db上执行查询

Dim dr As SqlDataReader
dr = cm.ExecuteReader

4.在屏幕上显示您的数据并关闭使用的对象

If dr.HasRows Then

dr.Read()
username.text = dr.Item("username")
address.text = dr.Item("address")

dr.Close()

EndIf
Conn.Close()

希望它对你有用。

【讨论】:

  • 对本教程表示赞赏,但请不要教如何留下 Sql Injection 漏洞
  • 附加问题。如果我想导航到下一条记录怎么办?
  • 查看LINK
猜你喜欢
  • 2021-12-13
  • 2013-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多