Código:
If (txtFilePath.HasFile) Then Dim conn As OleDbConnection Dim cmd As OleDbCommand Dim da As OleDbDataAdapter Dim ds As DataSet Dim query As String Dim connString As String = "" Dim strFileName As String = DateTime.Now.ToString("ddMMyyyy_HHmmss") Dim strFileType As String = System.IO.Path.GetExtension(txtFilePath.FileName).ToString().ToLower() 'Check file type If strFileType.Trim = ".xls" Or strFileType.Trim = ".xlsx" Then txtFilePath.SaveAs(Server.MapPath("~/UploadedExcel/" & strFileName & strFileType)) Else lblMessage.Text = "Solo archivos excel puedes seleccionar." lblMessage.ForeColor = Drawing.Color.Red lblMessage.Visible = True Exit Sub End If Dim strNewPath As String = Server.MapPath("~/UploadedExcel/" & strFileName & strFileType) lblServerPath.Text = strNewPath lblServerPathTitle.Text = "Estas leyendo el archivo: " 'Connection String to Excel Workbook If strFileType.Trim = ".xls" Then connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strNewPath & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=2""" ElseIf strFileType.Trim = ".xlsx" Then connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strNewPath & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=2""" End If query = "SELECT [Razón Social],[Numero de Cliente],[Numero de Cuenta] FROM [" & txtSheet.Text & "$]" 'Create the connection object conn = New OleDbConnection(connString) 'Open connection If conn.State = ConnectionState.Closed Then conn.Open() 'Create the command object cmd = New OleDbCommand(query, conn) da = New OleDbDataAdapter(cmd) ds = New DataSet() da.Fill(ds) grvExcelData.DataSource = ds.Tables(0) grvExcelData.DataBind() grvExcelData.Visible = True lblMessage.Text = "Finalizacion ok! Total de items:" & ds.Tables(0).Rows.Count lblMessage.ForeColor = Drawing.Color.Green lblMessage.Visible = True da.Dispose() conn.Close() conn.Dispose() btnGuardar.Enabled = True Else lblMessage.Text = "Por favor seleccione un archivo primero" lblMessage.ForeColor = Drawing.Color.Red lblMessage.Visible = True End If
¿Como puedo hacer para guardarlo en una DB (es SQL 2008)?
En simples palabras tengo que trasladar los valores del XLS a una tabla en una base de datos.
Gracias!