I have a more data in excel file .so i have to import it into sql database using vb.net.can anyone send the source code?
2 Answers
Dim ExcelConnection As New
System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\MyExcelSpreadsheet.xlsx; Extended Properties=""Excel 12.0 Xml; HDR=Yes""")
ExcelConnection.Open()
Dim expr As String = "SELECT * FROM [Sheet1$]"
Dim objCmdSelect As OleDbCommand = New OleDbCommand(expr, ExcelConnection)
Dim objDR As OleDbDataReader
Dim SQLconn As New SqlConnection()
Dim ConnString As String = "Data Source=MMSQL1; Initial Catalog=DbName; User Id=UserName; Password=password;"
SQLconn.ConnectionString = ConnString
SQLconn.Open()
Using bulkCopy Asd SqlBulkCopy = New SqlBulkCopy(SQLConn)
bulkCopy.DestinationTableName = "TableToWriteToInSQLSERVER"
Try
objDR = objCmdSelect.ExecuteReader
bulCopy.WriteToServer(objDR)
objDR.Close()
SQLConn.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Using
Comments
If it's a one-off job, use DTS or SSIS. No code required.
Otherwise, you can open Excel as a data source, suck up its contents and insert into your database.
11 Comments
user321182
ok but i want in vb.net code because we have 1000's of files to import tats y
Marcelo Cantos
That's a fair bit of work and relies on intimate knowledge of your particular circumstances. No one on SO is going write it for you.
user321182
but i"m using file browser to get the excel file from the path and then i have to convert this is my process actually can u help
Marcelo Cantos
Sorry, but that doesn't make things any clearer. Actually, I'm more confused about you want than before.
user321182
ok i'll explain it clearly ..i have more than 1000 files in the excel format.from tat i have to import into sql database by using windows form using vb.net. In tat i have File upload to choose the file to covert... this is my main objective.can u help me now?
|