Selecting data from a DataTable into a new table in a DB

S

Stropher

If I have a table (t1) in a database of SQL-Server and I want to select
some rows into a new table (t2), I do the following:
select co1, col2, col3
into t2 from t1 //t2 wird be created automatically with the three
columns

But supposing t1 is not a table of a database but a DataTable (in c#),
how do I go about it to achieve the same result as I did with a normal
database-table (see the above)?


Thanks in anticipation
Stropher
 
M

Michael Nemtsev

Hello Stropher,

You can use DataTable.Select/DataTables.Rows.Add for this

S> If I have a table (t1) in a database of SQL-Server and I want to
S> select
S> some rows into a new table (t2), I do the following:
S> select co1, col2, col3
S> into t2 from t1 //t2 wird be created automatically with the three
S> columns
S> But supposing t1 is not a table of a database but a DataTable (in
S> c#), how do I go about it to achieve the same result as I did with a
S> normal database-table (see the above)?
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
S

Stropher

Hi Michael,
Thanks for your quick answer.

I have tried something like this:
....
string command = datatable.Select() + "into newTable";
SqlCommand com = new SqlCommand(command, connection)
int rows = com.ExecuteNonQuery();

Unfortunately, I get an exception saying that either the object is
empty (not the case anyway) or the column names could not be used.

What am I missing?
Thanks in advance
Stropher
 
M

Michael Nemtsev

Hello Stropher,

Look at msdn

you need specify in Select() by with condition data should be selected and
you got the array of selected DataRow
This DataRows need be inserted into second DataTable

S> Hi Michael,
S> Thanks for your quick answer.
S> I have tried something like this:
S> ...
S> string command = datatable.Select() + "into newTable";
S> SqlCommand com = new SqlCommand(command, connection)
S> int rows = com.ExecuteNonQuery();
S> Unfortunately, I get an exception saying that either the object is
S> empty (not the case anyway) or the column names could not be used.
S>
S> What am I missing?
S> Thanks in advance
S> Stropher
S> Michael Nemtsev wrote:
S>---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top