[英]Get specific row col values from mysql using vb 2012
I have one button retrieve I just want to get the database table "account" value by its row and col and display it in a textbox.我有一个按钮检索我只想通过其行和列获取数据库表“帐户”值并将其显示在文本框中。 I keep on getting errors on the datafill line我一直在数据填充线上出错
Imports MySql.Data.MySqlClient
Public Class Form1
Dim dataset As DataSet
Dim datatable As DataTable
Dim sqlcon As MySqlConnection
Dim dataadapter As MySqlDataAdapter
Dim sqlcommand As MySqlCommand
Dim sql As String
Private Sub retrieve_Click(sender As Object, e As EventArgs) Handles retrieve.Click
sqlcon = New MySqlConnection("Data Source=localhost;Database=database;User ID=root;Password=;")
sqlcon.Open()
sql = "select * from account"
dataadapter = New MySqlDataAdapter(sql, sqlcon)
dataadapter.Fill(dataset)
TextBox2.Text = dataset.Tables(0).Rows(0).Item(0).ToString()
End Sub
End Class
You need to instantiate the dataset that you pass to the Fill method.您需要实例化传递给 Fill 方法的数据集。
....
dataset = new DataSet()
dataadapter.Fill(dataset)
...
Do not forget to close the connection when finished.完成后不要忘记关闭连接。 It is a resource very costly to keep open when you not need it在不需要时保持开放是一种成本非常高的资源
Using sqlcon = New MySqlConnection("Data Source=localhost;Database=database;User ID=root;Password=;")
sqlcon.Open()
sql = "select * from account"
dataadapter = New MySqlDataAdapter(sql, sqlcon)
dataset = new DataSet()
dataadapter.Fill(dataset)
TextBox2.Text = dataset.Tables(0).Rows(0).Item(0).ToString()
End Using
See the Using Statement请参阅使用声明
However if you need only one row it is better to refine the query applying a WHERE clause to limit the results returned by the database.但是,如果您只需要一行,最好应用 WHERE 子句来优化查询以限制数据库返回的结果。
sql = "select * from account WHERE AccountName = @name"
dataadapter = New MySqlDataAdapter(sql, sqlcon)
dataadapter.SelectCommand.Parameters.AddWWithValue("@name", inputNameBox.Text)
dataset = new DataSet()
dataadapter.Fill(dataset, "Account")
if dataset.Tables("Account").Rows.Count > 0 then
TextBox2.Text = dataset.Tables("Account").Rows(0).Item(0).ToString()
End If
this hopefully will return just the row needed这有望只返回所需的行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.