Updating a database table with a dataset

Source: Internet
Author: User

/*Updating a database table in a dataset * Note: When updating a database table with a dataset, the table must specify a primary key or a unique column*/ stringConnString ="Data source= (local); Initial catalog=linq;integrated Security=sspi";//log in with a Windows userusing(SqlConnection conn =NewSqlConnection (connstring)) {Conn.    Open (); using(SqlCommand cmd =Conn. CreateCommand ()) {Cmd.commandtext="select * FROM Orders"; DataSet DataSet=NewDataSet (); SqlDataAdapter Adapter=NewSqlDataAdapter (CMD); /*populate the DataSet with the results of the query and specify a surface: Orders * NOTE: * If you do not specify a table name, use adapter directly. Fill (DataSet) is populated by the way the DataTable is received according to the index number * DataTable table = DataSet.         Tables[0]; */adapter. Fill (DataSet,"orders"); DataTable Table= DataSet. tables["orders"]; //update the city of the first row of data to "Panzhihua"DataRow row = table. rows[0]; row[" City"] ="Panzhihua"; /*Builder: Automatically create SqlCommand when users update the database * to view: * Builder.         GetUpdateCommand (); * Builder.         GetInsertCommand (); * Builder.         GetDeleteCommand (); */SqlCommandBuilder Builder=NewSqlCommandBuilder (adapter); /*There are several ways to update here: * Adapter. Update (DataSet, "Orders"); Specifies the table name of the dataset updated * adapter. Update (table "), Updating table * adapter. Update (dataset), updating the entire dataset directly*/adapter. Update (DataSet,"orders"); Console.WriteLine ("Update Successful");    Console.readkey (); }}

Updating a database table with a dataset

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.