Solved

DataTable Insert Query

Posted on 2003-10-22
4
663 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 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

821 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