Results 1 to 6 of 6

Thread: CSV to Database Help!!

  1. #1

    Thread Starter
    Lively Member heeroyu16's Avatar
    Join Date
    Nov 2005
    Posts
    123

    CSV to Database Help!!

    Hi Guys, I'm having troubling with my application to get data from an excel file, save it to a CSV, and export it over to a DB.

    First Problem, Excel doesn't seems to close after i convert xls to csv.

    Second problem i need to concat the last two columns of the CSV file to obtain a timestamp of the data as of the xls.

    This is my coding so far

    VB Code:
    1. Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
    2.  
    3.         Dim xlApp As Excel.Application
    4.         Dim xlBook As Excel.Workbook
    5.         Dim xlSheet As Excel.Worksheet
    6.         Dim xlformat As Excel.XlFileFormat = Excel.XlFileFormat.xlCSV
    7.         Dim c As New CSV_TO_DATA_TABLE.CSV_CONVERT.CSV_TO_DATA_TABLE
    8.         Dim dt As DataTable
    9.  
    10.         xlApp = CreateObject("Excel.Application")
    11.         xlBook = xlApp.Workbooks.Open("C:\Program Files\STOCK TAKE SYSTEM\STS.XLS")
    12.         xlBook.SaveAs("C:\Program Files\STOCK TAKE SYSTEM\STS.CSV", FileFormat:=xlformat)
    13.         xlBook.Close()
    14.         xlBook.Saved = True
    15.         xlApp.Quit()
    16.  
    17.         Dim myDriver As New OleDb.OleDbConnection("Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=C:\Program Files\STOCK TAKE SYSTEM;Extensions=asc,csv,tab,txt;")
    18.         Dim myConnection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\STOCK TAKE SYSTEM;Extended Properties=""text;HDR=Yes;FMT=Delimited""")
    19.         Dim insertData As New DataSet
    20.         Dim mydatatable As DataTable
    21.         Dim ds As DataSet
    22.         Dim sql As String
    23.  
    24.         connectDB = New connection("SAMPLE", "", "")
    25.         Dim myDataAdapter As New OleDb.OleDbDataAdapter("SELECT * FROM [STS.CSV]", myConnection)
    26.         ' ds = connectdb.updateRecord("insert into Sample.SRST_TEST ("
    27.  
    28.         Try
    29.             myDataAdapter.Fill(insertData)
    30.             DataGrid1.DataSource = insertData
    31.  
    32.         Catch Ex As Exception
    33.             MessageBox.Show(Ex.Message)
    34.         End Try
    35.         'closes connection upon complete
    36.         myConnection.Close()    
    37.  
    38.     End Sub

    I'm suppose to make an application, non web based

    Thanks in advance

  2. #2

    Thread Starter
    Lively Member heeroyu16's Avatar
    Join Date
    Nov 2005
    Posts
    123

    Re: CSV to Database Help!!

    so nobody can help me... ?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: CSV to Database Help!!

    You don't need all those steps. You can use ADO.NET to retrieve the data from the XLS directly into a DataTable, which you can then insert into any database. See www.connectionstrings.com for details of connecting to Excel. It's virtually the same as connecting to a CSV file. I'd be inclined to use OLEDB over ODBC.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member heeroyu16's Avatar
    Join Date
    Nov 2005
    Posts
    123

    Re: CSV to Database Help!!

    do you mean by using this??

    VB Code:
    1. Private Function InsertSql()
    2.         Dim myDriver As New OleDb.OleDbConnection("Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=C:\Program Files\STOCK TAKE SYSTEM;Extensions=asc,csv,tab,txt;")
    3.         Dim myConnection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\STOCK TAKE SYSTEM;Extended Properties=""text;HDR=Yes;FMT=Delimited""")
    4.         Dim insertData As New DataSet
    5.         Dim mydatatable As DataTable
    6.         Dim ds As DataSet
    7.         Dim sql As String
    8.  
    9.       Dim myDataAdapter As New OleDb.OleDbDataAdapter("SELECT * FROM [STS.CSV]", myConnection)
    10.         ' ds = connectdb.updateRecord("insert into Sample.SRST_TEST ("
    11.  
    12.         Try
    13.             myDataAdapter.Fill(insertData)
    14.             dg.DataSource = insertData
    15.         Catch Ex As Exception
    16.             MessageBox.Show(Ex.Message)
    17.         End Try
    18.         'closes connection upon complete
    19.         myConnection.Close()
    20.     End Function

    Or directly read the xls file
    but inserting into my database is abit more troublesome as some columns are fixed by my predessor. so i have to extract out the individual columns, check if there are any duplicate data entry, by timestamp and a column with serial numbers.
    Last edited by heeroyu16; Mar 12th, 2006 at 08:29 PM.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: CSV to Database Help!!

    None of that matters. You don't need a CSV at all. You simply read the data directly from the XLS into a DataTable using ADO.NET. You can then either manipulate that DataTable as needed or create a new DataTable with the appropriate schema for the target database and transfer the data from the existing DataTable to the new DataTable as needed. You can then simply save the new DataTable to the database.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Lively Member heeroyu16's Avatar
    Join Date
    Nov 2005
    Posts
    123

    Re: CSV to Database Help!!

    hmm, okie okie i'll try tat thanks alot
    JM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width