views:

108

answers:

2

Hi, I am generating a DataTable from a webservice and i would like to save the whole DataTable into one database table.

DataTable ds = //get info from webservice

The DataTable is getting generated but What to do next .I am getting stuck .Show me some syntax.I dont really need the select statement there either, i just want to insert all the info from the DataTable into a blank db table.

A: 

This is exactly what SqlBulkCopy is for. Check it out: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx

John Gibb
+1  A: 

Use bulkcopy, this is the code. And make sure that the table has no foriegn key or primary key constraints.

  SqlBulkCopy bulkcopy = new SqlBulkCopy(myConnection);
 bulkcopy.DestinationTableName = table.TableName;
                            try
                            {
                                bulkcopy.WriteToServer(table);
                           }
    catch(Exception e){messagebox.show(e.message);}
Yash
thanks .....many many thaks....Will you please tell me what's the benefit of using the SqlBulkCopy.If my table have foriegn or primary key than what to do.Thanks again.It's really helpful for me.
shamim