如何在VB.net上从数据库到文本框获取数据 [英] How to get data from database to textbox on VB.net

查看:201
本文介绍了如何在VB.net上从数据库到文本框获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi im new in Visual basic。我有一个按钮,当它点击,它会通过用户输入他们的ID找到学生,并将其输出到文本字段的数据。我不知道如果我做这个权利。因为我收到此错误[image] >> http://img812.imageshack.us /img812/7650/gq0z.png



btw这里是我的代码到目前为止。有人可以帮我吗?感谢!

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

dr = cmd.ExecuteReader

尝试
dr.Read()
id.Text = dr.GetValue(0)
Lname.Text = dr.GetValue(1)
Fname.Text = dr.GetValue(2)
Mname.Text = dr.GetValue b $ b 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()
结束Try


解决方案

  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
....
否则
退出Sub
结束如果

您可以这样做,或

  dr = cmd.ExecuteReader 

尝试
与dr
.Read()
id.Text = .GetValue(0)
end with
dr.Close()

 与dr 
.read
id.text = .item(id)
.close

更容易阅读....


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

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

解决方案

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....

这篇关于如何在VB.net上从数据库到文本框获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆