INSERT from a DataTable

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a DataTable that is filled with records that I want to INSERT into a
SQL Table. I get it to work fine, however I'm wondering if there is a better
way.

Currently I do something along the lines of the following
daNorthwind.Update(dsNorthwind2, "Employees")
http://support.microsoft.com/kb/310351/EN-US/

The problem is, this does one INSERT at a time instead of doing something
like the following in a Stored Procedure

INSERT INTO MySqlTable
SELECT * FROM DataTableWithNewRecords

Meaning - is there a way to pass a dataTable or even a DataSet to a stored
procedure as a cursor or something? This would seem to be faster...maybe
it's even the same speed...i don't really know :-)

Anyway, your help would be appreciated if you know of a better way.

Regards,
Mekim
 
Mekim,

Assuming you are working with Sql Server, for this the closest approximation
to this is to either use SqlXml or use OPENXML.

In other words, you'll have to use database specific xml features to get
this working. This is covered in my book Chapter #12.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
 
Hi Sahil,

I will certainly look for your book - thx

That's interesting - but basically you're saying to pass to a Stored
Procedure an XML representation of your DataTable?

Regards,
Mekim
 
Yes .. that is what I am saying. Either a datatable or dataset - you can
send an Xml representation to it. It's a whole another topic in itself, but
there is also a related object called SqlXml and other adjunct objects
called SqlXmlAdapter/command etc. You might wanna google on that - you could
even get a few code samples.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
 
Hi Sahil,

thank you...it's a very interesting idea that keeps to reinforcing the power
of xml

and again thx for the book recommendation

Regards,
Mike
 
Back
Top