zoukankan      html  css  js  c++  java
  • Update Dataset data back to Database

    This post explains how to insert, update, and delete data in a Database using Dataset.

    The DataSet can be considered an in-memory cache of data retrieved from a database. The DataSet consists of a collection of tables, relationships, and constraints.

    Firstly, we have to fill data into a DataSet from Database.
    Secondly, when the DataSet is loaded, you can modify the data, and the DataSet will keep track of the changes made bu user.

    The Add method of DataTable accepts either an array of the expected data columns, or a DataRow.

    (Please note that all code is in c# language)

    Step 1: Create a new Connection and SqlDataAdapter
    SqlConnection myConnection = new SqlConnection(“connectionStringGoesHere”);
    SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(“Select * from tblNews”, myConnection);
    DataSet myDataSet = new DataSet();
    DataRow myDataRow;

    Step 2: Create SqlCommandBuilder(The SqlDataAdapter does not automatically generate the Transact-SQL statements required to match changes made to a DataSet with SQL Server. But, you can create a SqlCommandBuilder object to automatically generate Transact-SQL statements for single-table updates.)
    SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(mySqlDataAdapter);

    // Set the MissingSchemaAction property to AddWithKey because Fill will not cause primary
    // key & unique key information to be retrieved unless AddWithKey is specified.
    mySqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

    mySqlDataAdapter.Fill(myDataSet, “tblNews”);

    Step 3: Add rows to DataTable

    myDataRow = myDataSet.Tables["tblNews"].NewRow();
    myDataRow["cNewsId"] = “NewID”;
    myDataRow["cHeading"] = “New Heading”;
    myDataRow["cContent"] = “Content here”;

    myDataSet.Tables["tblNews"].Rows.Add(myDataRow);

    Step 4: Editing Row Data
    We can change the data in a DataRow by accessing the DataRow. Use the index of the row in the RowsCollection accessed through the Rows property:

    myDataSet.Tables["tblNews"].Rows[0]["cHeading"]=”Abc”;

    Access a specific row by specifying the Primary Key value:

    DataRow myDataRow1 = myDataSet.Tables["tblNews"].Rows.Find(“124″);
    myDataRow1["cHeading"]=”Abc”;

    Here “124″ is the value of the Primary Key “cHeading” in the “tblNews” table.

    Step 5: Deleting a record
    Use Delete method to delete a Row. All changes are done in dataset until you don’t explicitly update DataSet data to the Database. Use RejectChanges method of DataSet to reverse all changes made to DataSet.

    myDataSet.Tables["tblNews"].Rows[0].Delete();

    Note: The original and new values are maintained in the row. The RowChanging event allows you to access both original and new values to decide whether you want the edit to proceed.

    SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(mySqlDataAdapter);

    Step 6: Finally Update DataBase
    To submit the data from the DataSet into the database, use dataAdapter’s Update method.

    mySqlDataAdapter.Update(myDataSet, “tblNews”);

    A Visual Studio Solution code sample will be coming soon……

    **End of article**

  • 相关阅读:
    “花田喜事” 婚庆网站策划
    discuz 模块模板标签说明 DIY模块模板语法详解
    discuz x2.5 广告位开发学习(第二步:制作)
    DiscuzX2.5完整目录结构【模板目录template】
    Webservice 安全性访问
    X2.5 怎么关闭个人空间
    心中有佛,看人即佛;心中有屎,看人即屎
    discuz x2.5 广告位开发学习(第一步:摸索)
    UVA 128 Software CRC
    UVA 10791 Minimum Sum LCM
  • 原文地址:https://www.cnblogs.com/zhangchenliang/p/2749931.html
Copyright © 2011-2022 走看看