VBNET怎么将datagridview1的数据插入数据库
请问,我有一个DATAGRIDVIEW1里的数据是火鸟数据库里读出来的,我现在要将他存入到SQL数据库,可以指点下吗?...
请问,我有一个DATAGRIDVIEW1里的数据是火鸟数据库里读出来的,我现在要将他存入到SQL数据库,可以指点下吗?
展开
展开全部
Dim strConn As String = "driver={microsoft access driver (*.mdb)};uid=admin;pwd=;dbq=" &
Application.StartupPath & "\ACCESS.MDB"
Dim cn As New OdbcConnection(strConn)
cn.Open()
Dim da As New OdbcDataAdapter("Select * From 表名", cn)
Dim ds As New DataSet
da.Fill(ds, "表名")
Dim cmdBuilder As New OdbcCommandBuilder(da)
da.InsertCommand = cmdBuilder.GetInsertCommand
With ds.Tables("表名")
For i = 0 to DataGridView1.Rows.Count - 1
Dim dr As DataRow = .NewRow
dr("列名1") = DataGridView1中的值
.
.
.
dr("列名n") = DataGridView1中的值
.Rows.Add(dr)
next
End With
da.Update(ds.Tables("表名"))
cn.Close()
Application.StartupPath & "\ACCESS.MDB"
Dim cn As New OdbcConnection(strConn)
cn.Open()
Dim da As New OdbcDataAdapter("Select * From 表名", cn)
Dim ds As New DataSet
da.Fill(ds, "表名")
Dim cmdBuilder As New OdbcCommandBuilder(da)
da.InsertCommand = cmdBuilder.GetInsertCommand
With ds.Tables("表名")
For i = 0 to DataGridView1.Rows.Count - 1
Dim dr As DataRow = .NewRow
dr("列名1") = DataGridView1中的值
.
.
.
dr("列名n") = DataGridView1中的值
.Rows.Add(dr)
next
End With
da.Update(ds.Tables("表名"))
cn.Close()
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询