45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:将Excel和xml文件数据保存到数据库的方法步骤

将Excel和xml文件数据保存到数据库的方法步骤

2016-08-26 16:44:54 来源:www.45fan.com 【

将Excel和xml文件数据保存到数据库的方法步骤

Inherits System.Windows.Forms.Form
Dim DS1 As New System.Data.DataSet
Dim DS2 As New System.Data.DataSet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim MyConnection As System.Data.OleDb.OleDbConnection

MyConnection = New System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=e:/BOOK1.XLS; " & _
"Extended Properties=Excel 8.0;")
' Select the data from Sheet1 of the workbook.
MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
"select * from [Sheet1$]", MyConnection)
'或者是DS1.ReadXml("c:/abcd.xml")

MyCommand.Fill(DS1, "handle")
DataGrid1.DataSource = DS1
MyConnection.Close()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Dim j As Integer
Dim person_id, information_id, handle As String
Dim handle_time As System.DateTime
'If Me.SqlConnection1.Close() Then
Me.SqlConnection1.Open()
'End If
For j = 1 To DS1.Tables("handle").Rows.Count - 1

Me.SqlDataAdapter1.InsertCommand.Parameters("@person_id").Value = DS1.Tables("handle").Rows(j).Item(0)
Me.SqlDataAdapter1.InsertCommand.Parameters("@information_id").Value = DS1.Tables("handle").Rows(j).Item(1)
'Me.SqlDataAdapter1.InsertCommand.Parameters("@handle").Value = DS1.Tables("handle").Rows(j).Item(2)
Me.SqlDataAdapter1.InsertCommand.Parameters("@handle_time").Value = CDate(DS1.Tables("handle").Rows(j).Item(3))
Me.SqlDataAdapter1.InsertCommand.ExecuteNonQuery()
Next

'MessageBox.Show(SqlDataAdapter1.InsertCommand.CommandText)
Me.SqlConnection1.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub

一个例子,原理就是先把Excel或xml文件的数据通过DataSet读到Datagrid中,再一个个取出保存到数据库中去。


本文地址:http://www.45fan.com/a/question/67998.html
Tags: 文件 excel XML
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部