database 如何从数据库中获取数据到 VB.net 上的文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19642521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:06:34  来源:igfitidea点击:

How to get data from database to textbox on VB.net

databasevb.net

提问by phenomenon09

hi im new in Visual basic. I have a button that when its clicked, its gonna find the student via their ID inputted by the user and its gonna output the data to the textfields. I'm not pretty sure if im doing this right. because i'm getting this error [image] >> http://img812.imageshack.us/img812/7650/gq0z.png

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

btw here's my code so far. can someone help me please? thanks!

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

        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

回答by Justin E

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

change to:

改成:

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

You can do it this way, or

你可以这样做,或者

 dr = cmd.ExecuteReader

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

or

或者

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

easier to read....

更容易阅读....

回答by user5746814

First add a reference, if you are using MySQL database put it bet. the class

先加个引用,如果你用的是MySQL数据库就赌一把。班上

Dim Connection As MySqlConnection
Dim command As MySqlCommand

Put this to your textbox

把它放到你的文本框中

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

the root is default

根是默认的

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

回答by NARENDER REDDY

If you are using Mysql Database, first add reference. To add reference of MySql Database

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

  1. Go to your solution explorer and rightclick on your project name
  2. Find add->references

    a window will be opened

  3. In that window,under Assemblies select framework.
  4. On right side there will be a list find and select Microsoft.VisualBasic.Compatability.Data
  5. In extensions, find and add MySql.Data and MSDATASRC
  6. Click OK
  1. 转到您的解决方案资源管理器并右键单击您的项目名称
  2. 查找添加-> 引用

    将打开一个窗口

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