How can we insert a DataTable into database?

  • Thread starter Thread starter ad
  • Start date Start date
A

ad

I have a typed DataSet with a DataTable in it.
The structure of the DataTable is from a table in a database.

Now I have make the DataTble with about 10 new row.
How can I insert the DataTable into database at once?
 
Define an insert stored procedure as you normally would if you were
doing a single insert.

Create a sql connection

Then in your code define a SqlDataAdapter (or data adapter for the type
of database you use).

You create a sqlcommand with the insert stored procedure specified.

Then set the sqldataadapters insertcommand property to the sql command
you just created.

Then if you call the Sql data adapters Update method and pass in the
datatable.

check out the following link

http://msdn2.microsoft.com/en-us/library/59x02y99.aspx

Please note, the insertcommand will be fired when you call the Update
method. The dataadapter knows what to do for each row by checking the
rows state.

So if the rows rowstate is new then it would be inserted using the
insert command, if it is modified it would be updated using the update
command, if it is deleted it would use the delete command.

Hope this helps

Pete
 
Duh, I misread dataset instead of database.
Assuming you are using vs.net 2003 just drag & drop proper dataadapter from
toolbox on the form - wizard will show up and guide you...

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Miha Markic said:
Ehm, by using DataSet.Tables.Add method?

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

ad said:
I have a typed DataSet with a DataTable in it.
The structure of the DataTable is from a table in a database.

Now I have make the DataTble with about 10 new row.
How can I insert the DataTable into database at once?
 
Back
Top