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.