Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DataTable Insert Query

Posted on 2003-10-22
4
Medium Priority
?
669 Views
Last Modified: 2010-08-05
Can this be done...

This works fine in SQL Analyser:

INSERT INTO table1 (intMyNumber, strJobName, dtJobDate)

        SELECT '22', strJobName, dtJobDate
'22 is a variable not a column name
       FROM table2

The sql above also works in vb.net code, but... How could i(if possible),  select a Datatable instread of table2 (example below).  A datatable is not an object in SQL Server so it may not be possible.  

e.g. This doesnt seem to be working

INSERT INTO table1 (intMyNumber, strJobName, dtJobDate)

        SELECT '22', myDataTable.Column(1),  myDataTable.Column(2)

       FROM myDataTable

Is there a way?
0
Comment
Question by:sublimation
  • 2
4 Comments
 
LVL 1

Expert Comment

by:johan_asplund
ID: 9598026
Hello

Cant you use a SqlCommand?

something like this

sqlBuf = "INSERT INTO table1 (intMyNumber, strJobName, dtJobDate) SELECT '22', strJobName,  dtJobDate  FROM myDataTable"

SqlCommand dbCommand;
dbCommand = new SqlCommand(sqlBuf, dbConnection);      
dbCommand.ExecuteNonQuery();
dbCommand.Dispose();

Br
Johan
0
 
LVL 4

Author Comment

by:sublimation
ID: 9598061
Hi, Johan

That wont work as the database (SQL Server) doesn't have an object in it named myDataTable.  myDataTable is an object created in memory.
0
 
LVL 1

Expert Comment

by:johan_asplund
ID: 9598237
hmm ok

can you do the following or is it multipple rows in the in memory table?

sqlBuf = "INSERT INTO table1 (intMyNumber, strJobName, dtJobDate) SELECT '22','" + myDataTable.Column(1) +"','"  myDataTable.Column(2) + "'"


Br
Johan
0
 
LVL 10

Accepted Solution

by:
GoodJun earned 1000 total points
ID: 9598307
You can not run sql statement against datatable. YOur option is the the datatable.select method or the dataview.rowfilter method. both have some way to limit the rows returned but very limited compare with sql statement. Look at these two method in MSDN to find some examples.
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Screencast - Getting to Know the Pipeline
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question