Problem: Dataset into SQL Table

Last post 03-26-2008 7:35 AM by BlogPortalist. 3 replies.

Sort Posts:

  • Problem: Dataset into SQL Table

    03-12-2008, 2:07 PM
    • Loading...
    • acoleman
    • Joined on 01-22-2008, 3:10 PM
    • Nashville
    • Posts 9

    I am reading a CSV file into a dataset and trying to insert this dataset into an existing SQL table.  The problem I am having lies in the fact that the dataset I have contains 2 columns, and the SQL table has 20+ columns. 

    I can't get any data to be inserted into the SQL table from the dataset, I really don't know what I am doing wrong here.  Thanks for any help or suggestions.

        Public Function DatasetToSQL() As DataSet
    
            Dim ds As DataSet
            Dim dr As DataRow
            Dim da As SqlDataAdapter
            Dim punchtime As String
            Dim eventtype As String
            Dim sqlconn As New SqlConnection(GetConnectionString())
    
            Try
                ds = ConnectCSV()
                sqlconn.Open()
                For Each dr In ds.Tables(0).Rows
                    punchtime = ds.Tables(0).Rows.Item(0).ToString                 
    	eventtype = ds.Tables(0).Rows.Item(1).ToString
                    da = New SqlDataAdapter("INSERT INTO ClockData (DateTime, EventTypeID) VALUES (" + punchtime + "," + eventtype + ")", sqlconn)
                    da.Update(ds, "CSVData")
                Next
    
    
            Catch ex As Exception
    
            End Try
    
        End Function
     
  • Re: Problem: Dataset into SQL Table

    03-25-2008, 8:52 PM
    • Loading...
    • trdudei
    • Joined on 05-10-2007, 3:58 PM
    • WI
    • Posts 33

    What are the constraints on the table you're inserting into?  What's the error you're getting?

  • Re: Problem: Dataset into SQL Table

    03-26-2008, 6:53 AM
    • Loading...
    • sudipta
    • Joined on 02-25-2008, 3:33 PM
    • India
    • Posts 399

    acoleman:

    I am reading a CSV file into a dataset and trying to insert this dataset into an existing SQL table.  The problem I am having lies in the fact that the dataset I have contains 2 columns, and the SQL table has 20+ columns. 

    I can't get any data to be inserted into the SQL table from the dataset, I really don't know what I am doing wrong here.  Thanks for any help or suggestions.

        Public Function DatasetToSQL() As DataSet
    
            Dim ds As DataSet
            Dim dr As DataRow
            Dim da As SqlDataAdapter
            Dim punchtime As String
            Dim eventtype As String
            Dim sqlconn As New SqlConnection(GetConnectionString())
    
            Try
                ds = ConnectCSV()
                sqlconn.Open()
                For Each dr In ds.Tables(0).Rows
                    punchtime = ds.Tables(0).Rows.Item(0).ToString                 
    	eventtype = ds.Tables(0).Rows.Item(1).ToString
                    da = New SqlDataAdapter("INSERT INTO ClockData (DateTime, EventTypeID) VALUES (" + punchtime + "," + eventtype + ")", sqlconn)
                    da.Update(ds, "CSVData")
                Next
    
    
            Catch ex As Exception
    
            End Try
    
        End Function

     

    What is the error? Did you try to see if you are able to retrieve the data from the DataSet table by Respose.Write

    -------------------------------------------------------------------------------------------------------Please click Mark As Answer if this helped in solving your problem.

     

    Please click "Mark As Answer" if this hepled in solving your problem.
  • Re: Problem: Dataset into SQL Table

    03-26-2008, 7:35 AM

    acoleman:
     
            Catch ex As Exception

            End Try
     

    Look at the Exception object to see the message and the stacktrace. Without knowing error messages and/or DDL for the table you are trying to insert into, it's not that easy to help out....

    If this answered your question, be sure to mark it as the answer. Then you give credit to people who help you, and others will know that the question is already answered.
    My SQL Server blog (swedish): http://www.underlandet.com/SqlServer
Page 1 of 1 (4 items)
Microsoft Communities
Page view counter