As per my requirement i want to read and export data from excel (reside on network drive) to sqlserver 2012. I will be creating console application ( exe) using c# (.net framework 4.5).. this console application will be schedule to run daily using window scheduler on web server.
can you let me know what is the best approach to do it...keeping performance also in mind. if any one has readymade code/component then please share.
I pieced this code together from an application II wrote some time ago. It uses .NET OLEDB to import data from an Excel file, in this case into an MS-Access database, but the concept is the same for SQL-Server. You will need to adapt it to the table, Excel file and data you are using:
Substitutions Required:
YourConnectionString
YourSLQTable
Your SQL Columns: col1, col2, coln
Your Column Valuess: @cdata1, cdata2, cdatan
UniqueValue: the columns(s) that make your SQL table unique
Note that parameter values must be supplied in the order they are used in the SQL statement, not in the named list, ex. New String() {"UV", "cdata1", "cdata2"}, _
Public Shared connString As String = _
Configuration.ConfigurationManager.ConnectionStrings("xls").ConnectionString
Public Shared connStringX As String = _
Configuration.ConfigurationManager.ConnectionStrings("xlsx").ConnectionString
Public Shared Sub ImportExcelFile(ByRef Filepath As String)
Dim ConnectionString = IIf(Filepath.EndsWith("xlsx"), connStringX, connString)
Try
Using axsCon = New OleDbConnection("YourConectionString")
axsCon.Open()
Using m_connexcel As OleDbConnection = _
New OleDbConnection(String.Format(ConnectionString, Filepath))
m_connexcel.Open()
Dim Sheetname As String = GetFirstExcelWorksheetName(m_connexcel, Filepath)
Using cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [" & Sheetname & "]", m_connexcel)
Using oRDR As OleDbDataReader = cmd.ExecuteReader
While (oRDR.Read)
If Common.GetScalar(axsCon, "SELECT Count(*) FROM YourSQLTable WHERE [UniqueValue]=@UV ", _
New String() {"UV"}, New Object() {oRDR.GetValue(0)}) = 0 Then
Common.ExecuteSQL(axsCon, _
"INSERT INTO YouSQLTable(col1, col2, coln) " & _
"VALUES(@C1,@C2,@CN)", New String() {"Cdata1", "Cdata2", "CdataN"}, _
New Object() {oRDR.GetValue(0), oRDR.GetValue(1), oRDR.GetValue(2)})
Else
Common.ExecuteSQL(axsCon, _
"UPDATE YourSQLTable SET col1=@cdata1,col2=@cdata2,UNiqueValue=@UV " & _
"WHERE UniqueValue=@UV", New String() {"UV", "cdata1", "cdata2"}, _
New Object() {oRDR.GetValue(1), oRDR.GetValue(2), oRDR.GetValue(0)})
End If
End While
End Using
End Using
m_connexcel.Close()
End Using
End Using
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Sub
' This function is needed to find the first worksheet and may not apply to your application.
Private Shared Function GetFirstExcelWorksheetName(ByVal m_connexcel As OleDbConnection, ByVal Filepath As String) As String
Try
Dim ExcelSheets As DataTable = m_connexcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
Return ExcelSheets.Rows(0).Item("TABLE_NAME")
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.