如何编写一个从数据库中获取信息的sql查询,然后将其放入标签中的文本中?我不确定该怎么做。
答案 0 :(得分:2)
MSDN有很多通过ADO.NET获取数据的例子。例如。 http://msdn.microsoft.com/library/dw70f090
您需要调整连接和命令类型(以及连接字符串)以使其适用于My SQL。如果您有My SQL的ODBC驱动程序,那么只需更改连接字符串即可遵循ODBC示例。
答案 1 :(得分:1)
对于在.NET中使用MySQL,我建议你this tutorial,特别是你的问题第6部分,关于用MySQLDataReader读取数据。
一个(几乎正常工作)的样本,通过复制和粘贴从那里进行一些更改:
Private Sub getData()
Dim conn As New MySqlConnection
Dim myCommand As New MySqlCommand
Dim myReader As MySqlDataReader
Dim SQL As String
SQL = "SELECT LabelContent FROM myTable"
conn.ConnectionString = myConnString ' your connection string here'
Try
conn.Open()
Try
myCommand.Connection = conn
myCommand.CommandText = SQL
myReader = myCommand.ExecuteReader
' loop through all records'
While myReader.Read
Dim myLabelValue as String
myLabelValue = myReader.GetString(myReader.GetOrdinal("LabelContent"))
' ... do something with the value, e.g. assign to label '
End While
Catch myerror As MySqlException
MsgBox("There was an error reading from the database: " & myerror.Message)
End Try
Catch myerror As MySqlException
MessageBox.Show("Error connecting to the database: " & myerror.Message)
Finally
If conn.State <> ConnectionState.Closed Then conn.Close()
End Try
End Sub
答案 2 :(得分:1)
要从ms access 2007数据库中选择一个要标记的列,请执行以下步骤
Sub lihat()
Dim str As String = "select * from Table1"
Dim cn As New OleDb.OleDbConnection
Dim com As New OleDb.OleDbCommand
Dim adp As OleDb.OleDbDataReader
With cn
.ConnectionString = "Provider=Microsoft.ace.oledb.12.0;data source=test.accdb;persist security info=false"
.Open()
End With
With com
.Connection = cn
.CommandText = str
End With
adp = com.ExecuteReader
adp.Read()
Label1.Text = adp(1)
cn.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lihat()
End Sub
adp(1)上的数字1是Table1上的列数。