vb.net和Access数据库连接:读取数据库中表的一列值的问题。

自己想了好久,还是没能整出来,来发个帖问问大神。
我想实现:用vb.net代码读取表Student中的某一列(名称为Name)的所有值,并将这些值赋给一个未定长度的数组。该数组用于其他地方。用的SQL语言。谢谢大家了!!
另外我从网上搜到了一个用vb编的例子,能将此例转换为vb.net语言也可。谢谢!
conn 为ADODB.Connection,Rs 为 New ADODB.Recordset
Private Sub Form_Load()
Dim i As Integer
db = Trim("D:\Users\lafox\Desktop\inventor编程示例\")
db = "Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;Data Source=" & db & "FasernElement.accdb"
conn.CursorLocation = adUseClient
conn.Open db
Rs.Open "select Name from Elements", conn, adOpenKeyset, adLockPessimistic
If Rs.EOF = False Then
Do Until Rs.EOF
Combo1.AddItem Rs!Name, i
Rs.MoveNext
i = i + 1
Loop
End If End Sub
最新回答
可爱的害羞鬼

2024-10-02 00:28:02

'vs2010,VB10 牧固图
imports system.data
imports system.data.oledb

dim conn as OleDbConnection=new OleDbConnection
conn.ConnectionString= "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Db & ";Persist Security Info=True"
dim cmd as new sqlcommand
Dim cmd As OleDbCommand = New OleDbCommand
cmd.CommandText = "Select Name from Elements"
dim da as new OleDbDataAdapter
da.SelectCommand =cmd
dim ds as new Dataset
da.fill(ds)
conn.close
dim dt as datatable=ds.tables(0) '从数据库里面读出一列,放入了一个dataTable里面去。
'dt可以绑定到控件。如combobox
combo1.datasource=dt
combo1.displaymember="Name"
丸子头

2024-10-02 00:16:02

Imports System.Data.OleDb
Imports System.Data
Imports System.IO
Public Class Form1
Public con As New OleDb.OleDbConnection
Public cmd As New OleDbCommand
Public dr As OleDbDataReader
Public oName() As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label1.Text = ""
Dim mcount As Long
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Users\lafox\Desktop\inventor编程示例\FasernElement.accdb;Persist Security Info=False"
cmd.Connection = con
cmd.CommandText = "Select * from Elements"
con.Open()
mcount = cmd.ExecuteScalar
dr = cmd.ExecuteReader
Try
While dr.Read
Label1.Text = Label1.Text & String.Format("{0}", dr(0)) & Chr(13)
ComboBox1.Items.Add(String.Format("{0}", dr(0)))
mcount = mcount + 1
End While
Catch ex As Exception
End Try
End Sub
End Class