【问题标题】:How to get data from database to textbox on VB.net如何在 VB.net 上从数据库中获取数据到文本框
【发布时间】:2013-10-28 18:37:07
【问题描述】:

嗨,我是 Visual Basic 的新手。我有一个按钮,当它被点击时,它会通过用户输入的 ID 找到学生,并将数据输出到文本字段。我不太确定我这样做是否正确。因为我收到了这个错误 [图片] >> http://img812.imageshack.us/img812/7650/gq0z.png

顺便说一句,到目前为止,这是我的代码。有人能帮助我吗?谢谢!

        cmd.CommandText = "Select * from Student where Student_id = '" & id.Text & "'"
        cmd.Connection = db

        dr = cmd.ExecuteReader

        Try
            dr.Read()
            id.Text = dr.GetValue(0)
            Lname.Text = dr.GetValue(1)
            Fname.Text = dr.GetValue(2)
            Mname.Text = dr.GetValue(3)
            datet.Text = dr.GetValue(4)
            age.Text = dr.GetValue(5)
            male.Text = dr.GetValue(6)
            female.Text = dr.GetValue(7)
            status.Text = dr.GetValue(8)
            staddress.Text = dr.GetValue(9)
            cityAdd.Text = dr.GetValue(10)
            dr.Close()

        Catch ex As Exception
            MsgBox("" + ex.Message)
            dr.Close()
        End Try

【问题讨论】:

  • 如果id.Text 为空你会得到一个错误;您还需要在构建 SQL 之前将其包含的 text 转换为数字。将来发布实际的错误消息(它们是重要信息)而不是图片链接
  • 我会推荐一个:If NOT isdbnull(dr.getvalue(0))... 对于每一个显然增加每一个的 getvalue 数。
  • 不请自来的建议:继续将dr.getvalue(n) 更改为dr.getvalue("nameOfColumn")。这是一个更好的习惯。
  • 谢谢!会检查的。
  • 或将其更改为 dr.item("nameofcolumn") 短一点...请参阅下面的答案

标签: database vb.net


【解决方案1】:
cmd.CommandText = "Select * from Student where Student_id = '" & id.Text & "'"

改为:

if IsNumeric(id.text) Then
cmd.CommandText = "Select * from student where Student_id=@p1"
cmd.Prepare
cmd.Parameters.AddWithValue("@p1", id.text)
dr = cmd.ExecuteReader
....
Else
Exit Sub
End If

你可以这样做,或者

 dr = cmd.ExecuteReader

    Try
       with dr
        .Read()
        id.Text = .GetValue(0)
        end with
        dr.Close()

with dr
    .read
    id.text = .item("id")
    .close

更容易阅读......

【讨论】:

    【解决方案2】:

    首先添加一个引用,如果你使用的是 MySQL 数据库 把它打赌。班级

    Dim Connection As MySqlConnection
    Dim command As MySqlCommand
    

    把它放到你的文本框中

    Connection = New MySqlConnection
    Connection.ConnectionString = "Server=localhost;port=3306;userid=root;password=root;database=databasename"
    Dim reader As MySqlDataReader
    

    根是默认的

    Try
      Connection.Open()
      Dim query As String
      query= "Select * from Databasename.tablename where fieldname='" & textbox1.text & "'"
      Command = New MySqlCommand(query, Connection)
      reader = Command.ExecuteReader
      While reader.Read
        Dim sname As String
        sname = reader.GetString("Fieldname")
        textbox1.Items.Add(sname)
      End While
      Connection.Close()
    Catch e MySqlException
      MsgBox (ex.Message)
    Finally
      Connection.Dispose
    End Try
    

    【讨论】:

      【解决方案3】:

      如果您使用的是Mysql 数据库,请先添加引用。添加MySql数据库

      的引用
      1. 转到您的解决方案资源管理器并右键单击您的项目名称
      2. 查找添加->引用

        将打开一个窗口

      3. 在该窗口中,在“程序集”下选择框架。
      4. 在右侧会有一个列表查找并选择 Microsoft.VisualBasic.Compatability.Data
      5. 在扩展中,找到并添加 MySql.Data 和 MSDATASRC
      6. 点击确定

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-11
        • 2012-11-15
        • 2023-03-04
        • 2023-03-26
        • 2019-07-07
        • 2011-07-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多