VB.net&我的SQL Next-Previous-First-Last Record [英] VB.net & My SQL Next-Previous-First-Last Record
问题描述
亲爱的朋友
Sarfaraz在这里。我现在正在转向VB.net并逐渐尝试学习该语言,感谢大家对我在VB6中的早期问题的支持。
朋友我连接了我的sql服务器表与vb.net,可以显示数据网格中的所有记录。但是我在移动记录并将它们绑定到文本框时遇到问题。问题是我只能绑定最后一条记录。
我的代码正在加载文本框中的最后一条记录,但是如何转到Next-Previous-通过单击Next-Previous-First-Last Record的命令按钮并在相应的文本框中显示它们的First-Last Record。
sql = SELECT * FROM users
con.Open()
cmd.CommandText = sql
cmd.Connection = con
dataadapter.SelectCommand = cmd
datareader = cmd.ExecuteReader
while datareader.Read
datareader.Read()
TextBox1.Text = datareader( UserId)
TextBox2.Text = datareader( 用户名)
TextBox3.Text = datareader( 年龄)
结束 当
con.Close( )
请帮助
谢谢
您还没有任何约束。
你却放的记录,一个内容在时间到文本框中。对于您迭代过的每条记录,您将文本框的内容替换为最新记录的内容。
尝试阅读BindingNavigator [ ^ ]并且可能会看 this [ ^ ]。
亲爱的所有
我尝试并最终想出解决方案。
代码如下: -
进口 MySql .Data.MySqlClient
公共 类 Form1
Dim con As MySqlConnection = 新 MySqlConnection( data source = localhost; database = students; user id = root; password = root)
< span class =code-keyword> Dim ds As DataSet = 新 DataSet
Dim dataadapter 作为 新 MySqlDataAdapter( 从用户中选择*,con)
Dim cmd 作为 MySqlCommand = 新 MySqlCommand()
Dim dv 正如 DataView
Dim cm As CurrencyManager
Dim datareader As MySqlDataReader
Public Sub filldatasetandview()
ds = 新 DataSet
dataadapter.Fill(ds , users)
dv = 新 DataView(ds.Tables( users))
cm = < span class =code-keyword> CType ( Me .BindingContext(dv),CurrencyManager)
结束 Sub
公共 Sub bindfields()
TextBox1.DataBindings.Clear()
TextBox2.DataBindings.Clear()
TextBox3.DataBindings.Clear()
TextBox1.DataBindings.Add( text,dv, userid )
TextBox2.DataBindings.Add( text,dv, username)
TextBox3.DataBindings.Add( text,dv, 年龄)
结束 Sub
公共 Sub showposition()
TextBox4.Text = cm.Position + 1 & & of& & cm.Count()
结束 Sub
私有 Sub Form1_Load( ByVal sender < span class =code-keyword> As System。 Object , ByVal e As System.EventArgs)句柄 MyBase .Load
filldatasetandview()
bindfields()
showposition()
结束 Sub
私有 Sub previousrecord_Click( ByVal sender As System。 Object , ByVal e As System.EventArgs) Handles previousrecord.Click
cm.Position = cm.Position - 1
showposition()
结束 Sub
私有 Sub nextrecord_Click( ByVal sender As System。 Object , ByVal e As System.EventArgs) Handles nextrecord.Click
cm .Position = cm.Position + 1
showposition()
结束 Sub
私有 Sub lastrecord_Click(ByVal sender As System。 Object , ByVal e As System.EventArgs) Handles lastrecord。点击
cm.Position = cm.Count - 1
showposition()
结束 Sub
私有 Sub firstrecord_Click( ByVal sender As System。对象, ByVal e 作为 System.EventArgs)句柄 firstrecord.Click
cm.Position = 0
showposition()
电子nd Sub
结束 类跨度>
Dear friends
Sarfaraz here. I am now moving to VB.net and gradually trying to learn the language thanks to you all for support on my earlier questions in VB6.
Friends I have connected My sql server table with vb.net and could display all the records in a datagrid. But I have a problem in moving the records and bind them to text boxes. The problem is that i could only bind the last record.
My code is is working to load last record in the text boxes but how to move to Next-Previous-First-Last Record by clicking on command buttons for Next-Previous-First-Last Record and display them in text boxes accordingly.
sql = "SELECT * FROM users"
con.Open()
cmd.CommandText = sql
cmd.Connection = con
dataadapter.SelectCommand = cmd
datareader = cmd.ExecuteReader
While datareader.Read
datareader.Read()
TextBox1.Text = datareader("UserId")
TextBox2.Text = datareader("Username")
TextBox3.Text = datareader("Age")
End While
con.Close()
Please help
Thank you
You haven't bound anything.
All you did was put the contents of the records, one at time into the textbox. With each record you iteracted over, you replaced the contents of the textboxes with the contents of the latest record.
Try reading up on BindingNavigator[^] and maybe watch this[^].
Dear all
I try and finally come up with the solutions.
The code is as under:-
Imports MySql.Data.MySqlClient Public Class Form1 Dim con As MySqlConnection = New MySqlConnection("data source=localhost;database=students;user id=root;password=root") Dim ds As DataSet = New DataSet Dim dataadapter As New MySqlDataAdapter("select * from users", con) Dim cmd As MySqlCommand = New MySqlCommand() Dim dv As DataView Dim cm As CurrencyManager Dim datareader As MySqlDataReader Public Sub filldatasetandview() ds = New DataSet dataadapter.Fill(ds, "users") dv = New DataView(ds.Tables("users")) cm = CType(Me.BindingContext(dv), CurrencyManager) End Sub Public Sub bindfields() TextBox1.DataBindings.Clear() TextBox2.DataBindings.Clear() TextBox3.DataBindings.Clear() TextBox1.DataBindings.Add("text", dv, "userid") TextBox2.DataBindings.Add("text", dv, "username") TextBox3.DataBindings.Add("text", dv, "age") End Sub Public Sub showposition() TextBox4.Text = cm.Position + 1 & " " & "of" & " " & cm.Count() End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load filldatasetandview() bindfields() showposition() End Sub Private Sub previousrecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles previousrecord.Click cm.Position = cm.Position - 1 showposition() End Sub Private Sub nextrecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nextrecord.Click cm.Position = cm.Position + 1 showposition() End Sub Private Sub lastrecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lastrecord.Click cm.Position = cm.Count - 1 showposition() End Sub Private Sub firstrecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles firstrecord.Click cm.Position = 0 showposition() End Sub End Class
这篇关于VB.net&我的SQL Next-Previous-First-Last Record的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!