Solved

DataTable Insert Query

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now