How to save Excel and XML file data to a database

Source: Internet
Author: User
Inherits system. Windows. Forms. Form
Dim ds1 as new system. Data. Dataset
Dim DS2 as new system. Data. Dataset

Private sub button#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)
'or 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

For example, the principle is to read data from an Excel or XML file to the DataGrid through dataset, and then extract and save the data to the database one by one.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.