c# - How can I use single query to insert multiple records from Dataset into SQL Server 2005? -
i have dataset in ado.net containing multiple records user side. need insert rows in single query database, in order avoid multiple queries
maybe bulk copy answer. example in code project below show how using datatable, should able change example around use dataset.
below small part of code covers conenction , exection in sql server (taken codeproject).
the key part notice bulkcopy.writetoserver(sourcetable); sourcetable part of dataset pass it
//first create connection string destination database string connectionstring; connectionstring = <em>yourconnectionstring</em>and initial catalog=testsmodatabase"; //open connection destination database; using (sqlconnection connection = new sqlconnection(connectionstring)) { connection.open(); //open bulkcopy connection. using (sqlbulkcopy bulkcopy = new sqlbulkcopy(connection)) { //set destination table name //to table created. bulkcopy.destinationtablename = "dbo.testtable"; try { bulkcopy.writetoserver(sourcetable); // sourcetable come dataset } catch (exception ex) { console.writeline(ex.message); } connection.close(); } }
Comments
Post a Comment