473,378 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

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

May 25 '06 #1
3 3372
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
May 25 '06 #2
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

Michael Nemtsev wrote:
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


May 26 '06 #3
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>
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

---
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
May 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: VMI | last post by:
We have this huge application that's based on storing tons of data on a dataTable. The only problem we're having is that storing LOTS of data (1 million records) into a datatable will slow down the...
6
by: aaj | last post by:
Hi all I use a data adapter to read numerous tables in to a dataset. The dataset holds tables which in turn holds full details of the records i.e. keys, extra colums etc.. In some cases I...
3
by: Eych | last post by:
Here's an example: Dim dsMyData As DataSet dsMyData = CallWebService(Param1,Param2) Dim dtUsersTable As DataTable = dsMyData.Tables("Users") I have a Web service that fills dsMyData with...
11
by: Ron L | last post by:
I have a data table that lists a series of items in my database. In my user form, I want the user to be able to filter by a number of criteria (e.g. location, contract, date modified, etc). Other...
2
by: Rayne | last post by:
I have a combobox that is bound to a datasource. It lists the users in the system and I want it to automatically select the current logged on user, so that the screen loads that user's information...
1
by: Maxwell2006 | last post by:
Hi, I am working with strongly typed datatables. What is the most efficient way to build a new DataTAble based on the result of DataTable.Select? At this point I use a foreach loop to do the...
1
by: bbdobuddy | last post by:
I have two datatables in which I am looping through one table and selecting rows from the other tables that match and then deleting those matches. Once I delete a row and I try to select another...
9
by: Anil Gupte | last post by:
After reading a tutorial and fiddling, I finally got this to work. I can now put two tables created with a DataTable class into a DataRelation. Phew! And it works! Dim tblSliceInfo As New...
6
by: rcoco | last post by:
Hi, I have a datagrid that is ment to insert data. But when I run the form only the header appears. I would like some advise from you all and solve this problem I'm using visual studio 2003. My...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.