在VB.NET中将MS Access查询的结果导入文本框

时间:2012-10-12 03:16:02

标签: vb.net ms-access odbc

我在MS Access中有一个表,其中有一个名为NameC的列(使用ODBC连接到MS Access)

我希望将以下查询的结果保存在txtField

Dim query = "SELECT NameC FROM Table WHERE ClientID = " & Integer.Parse(clientID) 

如何在VB.NET中做到这一点?

我有一个txtNameC.Text字段

我目前正在审核一些示例代码,他们会这样做:

  Dim _consultationTable As DataTable
  Public Sub Load()
    Dim query = "SELECT * FROM Table WHERE ClientID = " & Integer.Parse(clientID)
    Me._consultationTable = DatabaseFunctions.GetDataTable(query)
    dvgInfo.Rows.Clear()
    For Each  dtRow In Me._consultationTable.Rows
        dvgInfo.Rows.Add()
        dvgInfo.Rows.Add(dvgInfo.RowCount-1).Cells("ColClientID").Value = dtRow("ClientId").ToString()
   Next

但我不想填写表格,我只想将查询结果放到文本框中 我怎么能这样做?

我想做这样的事情,但只返回一个值并将其保存到文本框中

Protected Sub BindData()  
   strSQL = "SELECT * FROM customer"  

   Dim dtReader As OdbcDataReader  
   objCmd = New OdbcCommand(strSQL, objConn)  
   dtReader = objCmd.ExecuteReader()  

   '*** BindData to GridView ***'  
   myGridView.DataSource = dtReader  
   myGridView.DataBind()  

   dtReader.Close()  
   dtReader = Nothing  
End Sub  


Protected Sub BindData()  
   strSQL = "SELECT SpecificValue FROM customer where x = y..."  

   Dim dtReader As OdbcDataReader  
   objCmd = New OdbcCommand(strSQL, objConn)  
   dtReader = objCmd.ExecuteReader()  

   '*** BindData to GridView ***'  
   myGridView.DataSource = dtReader  
   myGridView.DataBind()  

   dtReader.Close()  
   dtReader = Nothing  
End Sub  

1 个答案:

答案 0 :(得分:2)

使用DataReader。在datareader&中填充数据库中的数据。从datareader你可以使用特定值 我不知道你的代码,这就是为什么我举一个简单的例子 这是一个例子。

imports System.Data.OleDb
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connetionString As String
        Dim oledbCnn As OleDbConnection
        Dim oledbCmd As OleDbCommand
        Dim sql As String

        connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;"
        sql = "Your SQL Statement Here like Select * from product"

        oledbCnn = New OleDbConnection(connetionString)
        Try
            oledbCnn.Open()
            oledbCmd = New OleDbCommand(sql, oledbCnn)
            Dim oledbReader As OleDbDataReader = oledbCmd.ExecuteReader()
            While oledbReader.Read
                MsgBox(oledbReader.Item(0) & "  -  " & oledbReader.Item(1) & "  -  " & oledbReader.Item(2))
            End While
            oledbReader.Close()
            oledbCmd.Dispose()
            oledbCnn.Close()
        Catch ex As Exception
            MsgBox("Can not open connection ! ")
        End Try
    End Sub
End Class