Results 1 to 10 of 10

Thread: Insert DATATABLE into SQL

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    285

    Insert DATATABLE into SQL

    Good morning,
    I'm a problem with my VB.Net and SQL.
    My test code is:
    Code:
     Dim ConexaoRcsoft, ConexaoDB As SqlConnection
            Dim DsDados As DataSet
            Dim Dt As DataTable
    
            Dim sqlString As String = "SELECT taxa,descricao FROM dbo.taxa"
            ConexaoRcSoft = New SqlConnection(connStrinRcSoft)
            ConexaoDB = New SqlConnection(connString)
    
            Dim AdaptadorDados As SqlDataAdapter = New SqlDataAdapter(sqlString, ConexaoRcsoft)
            DsDados = New DataSet()
            AdaptadorDados.Fill(DsDados, "taxa")
    
            Dt = DsDados.Tables("taxa")
    
            ConexaoDB.Open()
            Dim sc As New SqlCommand(
            "INSERT INTO dbo.teste (taxa, descricao)" &
                "SELECT taxa, descricao FROM @taxa;", ConexaoDB)
            sc.Parameters.AddWithValue("@taxa", DsDados)
            sc.ExecuteNonQuery()
            MsgBox(Dt.Rows.Count)
            ConexaoDB.Close()
    I get the values from one SQL SERVER, using conexaoRcsoft
    Then, I need insert one ore more tables into another SQL SERVER, using the conexaoBD.
    Can I insert the dataset or datatable into my new SQL server database?
    How???

    The server are the same, but the databases are diferent.

    I try many solutions, but none of them works

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    285

    Re: Insert DATATABLE into SQL

    In this case, the error is:
    "No mapping exists from object type System.Data.DataSet to a known managed provider native type."

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Insert DATATABLE into SQL

    INSERT INTO dbo.teste
    I assume that dbo.teste is the database itself in which case INSERT is inapplicable. To transfer a table to a new database you will first have to CREATE a table with a matching schema in the new database and then INSERT the values into the table.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    285

    Re: Insert DATATABLE into SQL

    Hi.
    The dbo.taxa is my source table at my source database
    And the dbo.teste is my destination table at my destination database.

    the server is the same, but have two databases. And teh tables (source and destination) are equal.

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Insert DATATABLE into SQL

    Ok. Not sure that I really understand why you're getting that particular error then but moving on.

    "INSERT INTO dbo.teste (taxa, descricao)" &
    "SELECT taxa, descricao FROM @taxa;", ConexaoDB)
    The syntax here is wrong. See http://www.w3schools.com/sql/sql_select_into.asp
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    285

    Re: Insert DATATABLE into SQL

    It's wrong WHY?
    It's the only way for inject my dataset/datatable in the SQL statement.
    This, is the only think I wanna do. Insert onde dataset/datatable to a SQL Database
    It helps? So Rate it

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Insert DATATABLE into SQL

    Did you even bother to read the page I pointed you to? It's wrong because it's wrong!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    285

    Re: Insert DATATABLE into SQL

    Sorry, but the SQL was not wrong.
    It helps? So Rate it

  9. #9
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    265

    Re: Insert DATATABLE into SQL

    select * into newTable from OldDatabase.dbo.OldTable

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    285

    Re: Insert DATATABLE into SQL

    Hi.
    In this case I cant access the old database directly.
    This, cause first I need pass the data to a dataset, work them, and only after the upates, inject the data no the new database
    It helps? So Rate it

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