我在phpmyadmin上有一个mysql数据库。 数据库中的一个表有一个主键字段,我想从数据库中选择一个具有特定主键的记录。显然这只会返回1行但是我不知道如何运行查询然后将结果放入单个数据行对象。所有da.fill重载都需要一个数据集,虽然这是可能的,但定义数据集并始终将其引用为:
dataset.tables("table").rows(0).item.....
当我可以使用时:
row.item.....
TL; DR,如何在vb.net中使用dataadapter填充数据行?
答案 0 :(得分:0)
我建议将数据检索到DataGridView控件中。为此,您可以使用我在其中一个项目中输入的代码。
Imports System.Data.SqlClient
Public Class Form1
Dim dbConnection As SqlConnection
Dim dbCommand As SqlCommand
Dim dbAdapter As SqlDataAdapter
Dim DataSet As New DataSet
Dim strSQL As String
Dim dbCount As String
Public Sub SQLConnect()
dbConnection = New SqlConnection("Data Source=connectionhere\sqlexpress;Initial Catalog=line_log;Integrated Security=True")
dbConnection.Open()
End Sub
Public Sub SQLCommand()
dbCommand = New SqlCommand(strSQL, dbConnection)
End Sub
Public Sub SQLAdapter()
dbAdapter = New SqlDataAdapter(strSQL, dbConnection)
End Sub
Public Sub SQLDisconnect()
dbConnection.Close()
End Sub
Public Sub DGVLoad()
Try
SQLConnect()
strSQL = "SELECT [item] FROM [table] WHERE ([item] = 'value')"
SQLAdapter()
DataSet.Clear()
dbAdapter.Fill(DataSet)
SQLDisconnect()
DataGridView1.DataSource = DataSet.Tables(0)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DGVLoad()
End Sub