1

Using sql 2008 r2, vb.net 2010, excel 2010, file in xls mode

I want to import an excel spreadsheet programicly into sql 2010 via vb.net . I have 2 versions of the spreadsheet. The first one is the orginal one that I used for the SQL import wizard to import to the table (sheet name that showed in the import wizard is Dictionary$).

The second version is an export from sql to excel (since I had made many manual changes).

The visible differences I can see between the files is:

  1. The sheetname in import wizard does not have a $ after dictionary.
  2. Every cell in the spreadsheet has a ' (single quote at the begining of the cell').

Now to the problem of problems. Doing it programmatically and using the second version(exported) will import all the pre-existing rows without an issue but if I add rows to the spreadsheet the import will not see those rows.

If I try programmicly importing the orginal spreadsheet no row get imported becuase it can't find the table dictionary in the excel file (dictionary has the $ showing at end in wizard).

Solutions I am looking for are:

  • Is there something I need to enable in excel file to allow importing to SQL programically?
  • And what can I do to the exported file from sql so that if I add rows it can be reimported later if needed?

The code is following please note the following code works on the second version of the spreadsheet and that the SQL export wizard was used to produce it. also if I add rows to bottom of the spreadsheet those rows will not import programmtically.

Private Shared Sub ImportDictionary()
        Dim curFile As String = "dictionary.xls"
        Dim fullPath As String = Path.GetFullPath(curFile)
        Dim Excelconnection = New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; data source=" + fullPath + "; Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'")
        Dim myConnection As New SqlConnection(SQLdbString)
        Try
            myConnection.Open()
            Excelconnection.Open()
            Dim mySelectQuery As String = "SELECT * from dictionary"
            Dim myCommand2 As New OleDbCommand(mySelectQuery, Excelconnection)
            Dim myReader2 As OleDbDataReader
            myReader2 = myCommand2.ExecuteReader()
            While myReader2.Read()
                Dim myCommand As New SqlCommand
                Try
                    myCommand.Connection = myConnection
                    MyQueue.Enqueue(String.Format("{0} {1} {2} {3} {4}", myReader2.GetString(0), myReader2.GetString(1), myReader2.GetString(2), myReader2.GetString(3), myReader2.GetString(4)))
                    myCommand.Parameters.Add("@G0", SqlDbType.VarChar).Value = myReader2.GetString(0)
                    myCommand.Parameters.Add("@G1", SqlDbType.VarChar).Value = myReader2.GetString(1)
                    myCommand.Parameters.Add("@G2", SqlDbType.VarChar).Value = myReader2.GetString(2)
                    myCommand.Parameters.Add("@G3", SqlDbType.VarChar).Value = myReader2.GetString(3)
                    myCommand.Parameters.Add("@G4", SqlDbType.VarChar).Value = myReader2.GetString(4)
                    myCommand.CommandText = "INSERT INTO dictionary values(@G0,@G1,@G2,@G3,@G4)"
                    myCommand.ExecuteNonQuery()
                Catch e As Exception
                    Console.WriteLine(e.ToString())
                Finally
                    myCommand.Dispose()
                End Try
            End While
        Catch e As Exception
            Console.WriteLine(e.ToString())
        Finally
            myConnection.Dispose()
        End Try
        Excelconnection.Dispose()
    End Sub
Roan
  • 1,200
  • 2
  • 19
  • 32
Robert Allen
  • 150
  • 1
  • 16
  • One pertinent question: have you tried saving the Excel sheets as a CSV and doing a bulk insert through SQL server like this http://blog.sqlauthority.com/2008/02/06/sql-server-import-csv-file-into-sql-server-using-bulk-insert-load-comma-delimited-file-into-sql-server/ (9 short lines of code)? Importing from Excel to SQL Server can be headache, even if you're using fancy tools like SSIS, and a CSV conversion can make it pretty easy. Just another idea on the table for you if saving as a CSV is an option. – Kprof Dec 28 '12 at 19:34
  • i will give that a shot, but the main issue at least before trying would be that each time the spreadsheet is modified , one would have to remember to save as csv. right now if its modified and i compile it gets picked up and loaded with the oneclick app so the end user opens app and if file exists it deletes table rows then imports new spreadsheet then deletes the file. will see how this other way works – Robert Allen Dec 28 '12 at 19:47
  • yeah, if it's a frequent and regular process, automating it would be better. However, you can also automate the Excel sheet to save as a CSV if you find a CSV file easier to use, see this topic: http://stackoverflow.com/questions/5034717/save-an-excel-file-to-a-csv-file-in-c-sharp-code. – Kprof Dec 28 '12 at 19:54

0 Answers0