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**

  • 相关阅读:
    ubuntu 16.04下源码安装opencv3.4
    机器学习库--dlib
    ubuntu查看内存占用和查看cpu使用情况的简单方法(ubuntu内存管理)
    语音开放平台简介
    语音开源代码简介
    语音开源代码与平台对比
    source insight 添加 python 支持
    Taglist: Exuberant ctags (http://ctags.sf.net) not found in PATH. Plugin is not loaded
    人脸检测----Adaboost学习方法
    人脸检测---特征的提取
  • 原文地址:https://www.cnblogs.com/zhangchenliang/p/2749931.html
Copyright © 2011-2022 走看看