Solved

DataTable Insert Query

Posted on 2003-10-22
4
665 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

636 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