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
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."
Re: Insert DATATABLE into SQL
Quote:
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.
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.
Re: Insert DATATABLE into SQL
Ok. Not sure that I really understand why you're getting that particular error then but moving on.
Quote:
"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
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
Re: Insert DATATABLE into SQL
Did you even bother to read the page I pointed you to? It's wrong because it's wrong!
Re: Insert DATATABLE into SQL
Sorry, but the SQL was not wrong.
Re: Insert DATATABLE into SQL
select * into newTable from OldDatabase.dbo.OldTable
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