VB2010和Mysql不会显示表字段中的所有数据 [英] VB2010 and Mysql does not display all data from table fields
问题描述
我有一个MySQL数据库(ixdatabase),其中有几个表,其中之一是"masterlist".我有几个文本框可以显示表中的数据,但不是全部.它只能显示SURNAME,GIVEN,MID,MIDDLE,但对于其他字段则不能.我已经仔细检查了我的数据库,并且很肯定我正在将代码链接到正确的代码,也没有空格或特殊字符.我是一个新手,我想知道我在做什么错了?
I have a MySQL Database (ixdatabase) where I have several tables one of them is "masterlist". I have several textboxes which can display the data from my table BUT not all. It can only display SURNAME,GIVEN,MID, MIDDLE but for the other fields, it can''t. I have double checked my database and I''m positive that I am linking my code to the correct one, also there are no whitespaces or special characters. I am quite a newbie on this, I wonder what is wrong with what I am doing?
Imports MySql.Data.MySqlClient
Public Class frm_masterlist
Dim ServerString As String = "Server=localhost;User Id=root;Password=aaaaaa;Database=ixdatabase"
Dim SQLConnection As MySqlConnection = New MySqlConnection
Dim cmd As MySqlCommand = New MySqlCommand
Dim dadapter As New MySqlDataAdapter
Dim datardr As MySqlDataReader
Dim strsql As String
Private Sub frm_masterlist_Load(sender As Object, e As System.EventArgs) Handles Me.Load
SQLConnection.ConnectionString = ServerString
If SQLConnection.State = ConnectionState.Closed Then
SQLConnection.Open()
MsgBox("Connection to Database is Established")
Else
MsgBox("Cannot establish connection to Database")
End If
strsql = "SELECT * FROM ixdatabase.masterlist ORDER BY SURNAME"
cmd.CommandText = strSql
cmd.Connection = SQLConnection
dadapter.SelectCommand = cmd
datardr = cmd.ExecuteReader
If datardr.HasRows Then
datardr.Read()
tb_lname.Text = datardr("SURNAME")
tb_fname.Text = datardr("GIVEN")
tb_mname.Text = datardr("MID")
tb_mi.Text = datardr("MIDDLE")
tb_app.Text = datardr("APPELLATION")
tb_prefix.Text = datardr("PREFIX")
tb_sex.Text = datardr("SEX")
tb_status.Text = datardr("STATUS")
End If
End Sub
Private Sub cmd_close_Click(sender As System.Object, e As System.EventArgs) Handles cmd_close.Click
Me.Close()
SQLConnection.Close()
End Sub
End Class
还是perharps,您可以帮助我改善代码..请帮助.谢谢!
Or perharps, you can help me improve my code.. Please help. Thanks!
推荐答案
2件事情:
1.如果要获取多行数据,您打算如何在文本框中显示它?当前逻辑不适合它.
2.如果要获取多行数据,请将数据读取部分更改为:
2 things:
1. If you are fetching more than one row of data, how are you planning to display it in textboxes? Current logic does not fit in for it.
2. If you are fetching more than one row of data, change your data reading part to:
If reader.HasRows Then
Do While reader.Read()
//Console.WriteLine(reader.GetInt32(0) & vbTab & reader.GetString(1))
Loop
Else
Console.WriteLine("No rows found.")
End If
参考:
使用DataReader(ADO.NET)检索数据 [ ^ ]
DataReader类 [
Refer:
Retrieving Data Using a DataReader (ADO.NET)[^]
DataReader Class[^]
Lastly, since you are doing a ''Select * from Table'', you need to display the data in a list or datagrid kind of control.
If datardr.HasRows Then
Do While datardr.Read()
tb_lname.Text = datardr("SURNAME")
tb_fname.Text = datardr("GIVEN")
tb_mname.Text = datardr("MID")
tb_mi.Text = datardr("MIDDLE")
tb_app.Text = datardr("APPELLATION")
tb_prefix.Text = datardr("PREFIX")
tb_sex.Text = datardr("SEX")
tb_status.Text = datardr("STATUS")
Loop
Else
Console.WriteLine("No rows found.")
End If
这就是我所做的,如果我有FIRST,PREV,NEXT和LAST按钮,如何导航记录?顺便说一句,还有一些未显示的数据.
This is what I did, how would I be able to navigate records if I have FIRST,PREV,NEXT and LAST buttons? BTW, there there are still some data that are not displayed.
这篇关于VB2010和Mysql不会显示表字段中的所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!