Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DataTable Insert Query

Posted on 2003-10-22
4
Medium Priority
?
667 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

719 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