C#中ListBox控件如何实时显示ACCESS数据库表中某个字段对应的所有记录值!!!求解呀!!!

C#中ListBox控件如何实时显示ACCESS数据库表中某个字段对应的所有记录值!!!求解呀!!!,第1张

//通过listbox1.DataSource

来初始化其Items中的值

DataTable

table

=

GetTable()//

这里省略了从Access中获取表数据的代码

listBox1.DataSource

=

table

listBox1.DisplayMember

=

"要显示的字段名称"

数据库连接】

Private Sub Form_Load()

Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0Persist Security Info=FalseData Source=" &App.Path &"\数据库.mdb"

Adodc1.CommandType = adCmdText

Adodc1.RecordSource = "select 资料表 from 字段"

Adodc1.Refresh

Set DataGrid1.DataSource = Adodc1

DataGrid1.Refresh

End Sub

【将Listbox内容存入数据库】

Private Sub Command1_Click()

For i = 0 To List1.ListCount - 1

Adodc1.Recordset.AddNew

Adodc1.Recordset.Fields("name") = List1.List(i)

Adodc1.Recordset.Update

DataGrid1.Refresh

Next

End Sub

假设我现在有一个名为“Database1.mdb”的access数据库,里面有一张“BasicImformation”表

这张表中只有一个字段“Name”,那么将listbox中的数据全部插入到“BasicImformation”表的代码如下

Dim i As integer=0

for i = 0 to listbox1.items.count-1

Dim OLDA As OleDbDataAdapter = New OleDbDataAdapter

Dim conn As OleDbConnection = New OleDbConnection

Dim cmd As OleDbCommand = New OleDbCommand

Dim DT As DataTable =New DataTable

Dim DataSet1 As DataSet = New DataSet

conn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0DataSource='F:\VS2008\Database1.mdb' " (DataSource 必须是你的全程路径,也就是你数据库存放位置的全程路径)

cmd.CommandText = "select * from BasicImformation"

cmd.Connection = conn

OLDA.SelectCommand = cmd

Dim builder As OleDb.OleDbCommandBuilder = New OleDbCommandBuilder(OLDA)

(这句不能掉,起一个监控的作用)

conn.Open()

OLDA.Fill(DataSet1, "BasicImformation")

DT = DataSet1.Tables("BasicImformation")

conn.Close()

Dim myrow As DataRow = DT.NewRow()

myrow("Name") = listbox1.items(i)

DT.Rows.Add(myrow)

conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0Data Source='Database1.mdb'"

cmd.CommandText = " INSERT into BasicImformation (Name) values ( '" &listbox1.items(i) &"' )"

cmd.Connection = conn

OLDA.InsertCommand = cmd

Dim builder As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder

conn.Open()

OLDA.Update(DataSet1, "BasicImformation")

next


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9675283.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存