vb2008中,怎么把listbox中的内容逐条写到数据库(access)中 请附上代码,谢谢
展开全部
假设我现在有一个名为“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.0;DataSource='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.0;Data 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
这张表中只有一个字段“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.0;DataSource='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.0;Data 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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询