DATASET () Usage

Source: Internet
Author: User

The dataset is the central concept of ADO. Datasets can be treated as an in-memory database, which is a separate collection of data that is not dependent on the database. The so-called independence, that is, even if the data link is disconnected, or shut down the database, the dataset is still available, the dataset is internally XML to describe the data, because XML is a platform-independent, language-independent data Description language, and can describe the complex relationship of data, For example, the data for a parent-child relationship, so the dataset can actually accommodate data with complex relationships and is no longer dependent on the database link.

In a typical multi-tier implementation, the steps for creating and refreshing a DataSet and updating the original data in turn include: Generate and populate each DataTable in the dataset with data from the data source by using DataAdapter. Change the data in a single DataTable object by adding, updating, or deleting a DataRow object. Call the GetChanges method to create a second dataset that reflects only the changes made to the data. Call the Update method of DataAdapter and pass the second DataSet as a parameter. Call the Merge method to merge the changes from the second DataSet into the first one. Call AcceptChanges for the DataSet. Alternatively, call RejectChanges to cancel the change. It is important to note that all data in the dataset is loaded in memory and can improve data access speed and drive data security. Greatly improves the speed and stability of the program operation.

How to use

1. Create a DataSet object

DataSet ds = new DataSet ();

DataSet ds = new DataSet ("DatasetName");

2. Populating a DataSet with a dataset

The most common is the fill () method of the DataAdapter object to fill him with data

(1)

DataSet ds = new DataSet ();

SqlDataAdapter adapt = new SqlDataAdapter (Sqlcmd,con)

Adapt. Fill (ds, "mytest");

(2)

DataSet ds=new DataSet ();

DataTable dt=new DataTable ("NewTable");

Ds. Tables.add (DT);

(3)

DataSet ds=new DataSet ();

DataTable Dt=ds. Tables.add ("newtable");

3. Accessing table, row, and column values in a dataset

(1): Access to each DataTable

Access by Table name: DS. tables["mytest"]//Specify DataTable Object MyTest (that is, access to a DataTable named MyTest in the dataset)

Access by index (index 0-based): DS. TABLES[0]//Specify the first DataTable in a dataset

(2): Accessing Rows in a DataTable

Ds. tables["MyTest"]. ROWS[N]//access to row n+1 of the MyTest table (the row index is starting from 0)

Ds. Tables[i]. ROWS[N]//access to column n+1 of the I+1 DataTable in the dataset (the index of the column starts from 0)

(3): Accessing an element in a DataTable

Ds. tables["MyTest"]. ROWS[N][M]//access to the element of row n+1 m+1 column of the MyTest table

Ds. Tables[i]. ROWS[N][M]//access the element of row n+1 m+1 column of the I+1 DataTable table in the dataset

Ds. tables["MyTest"]. Rows[n][name]//access the element of the N+1 row Name column of the MyTest table

Ds. Tables[i]. Rows[n][name]//access the element of the N+1 row Name column in the I+1 DataTable table in the dataset

(4): Fetch a column name from a DataTable

Ds. tables["MyTest"]. COLUMNS[N]//Remove the n+1 column name of the MyTest table

Ds. Tables[i]. Columns[n]

4. Example

Using System;

Using System.Collections.Generic;

Using System.Text;

Using System.Data.SqlClient;

Using System.Data;

Namespace SqlConnection1

{

Class Program

{

private void sqlconnectionf (string source, string Select)

{

Create a connection

SqlConnection con = new SqlConnection (source);

SqlDataAdapter adapt = new SqlDataAdapter (Select,con);

Try

{

Con. Open ();

Console.WriteLine ("Connection is successful!");

}

catch (Exception e)

{

Console.WriteLine ("Connection error is: {0}", e.tostring ());

}

Create a DataSet

DataSet ds = new DataSet ();

Adding data to a DataSet

Adapt. Fill (ds, "mytest");

Remove the mytest table column names

Console.WriteLine ("{0,-15} {1,-10} {2,-10}", ds. tables["MyTest"]. Columns[0],

Ds. tables["MyTest"]. Columns[1],ds. tables["MyTest"]. COLUMNS[2]);

Output mytest The sixth row in the table

DataRow Row1 = ds. tables["MyTest"]. ROWS[5];

Console.WriteLine ("{0,-15} {1,-10} {2,-10}", row1[0],row1[1],row1[2]);

Output the value of the second column of row fifth in the MyTest table

DataRow Row2 = ds. tables["MyTest"]. ROWS[4];

Console.WriteLine ("{0,-25}", row2[1]);

The following two methods are equivalent to row2[1] (that is, the value of the second column of line fifth)

Console.WriteLine ("{0,-25}", ds. tables["MyTest"]. ROWS[4][1]);

Console.WriteLine ("{0,-25}", ds. tables["MyTest"]. rows[4]["number"]);

Output all data in a dataset

foreach (DataRow row in DS. tables["MyTest"]. Rows)

{

Console.WriteLine ("{0,-15} {1,-10} {2,-10} {3}", row["name"],

row["Number"], row["Low"], row["High"]);

Take the value of the third column

Console.WriteLine ("{0,-15}", row[3]);

}

Console.ReadLine ();

Con. Close ();

}

static void Main (string[] args)

{

String sou = "server=duanyf\\sqlexpress;" + "Initial catalog=master;" + "UID = sa;" + "Password = dyf123";

String sel = "Select Name,number,low,high from Dbo.spt_values";

Program Sqlcon = new program ();

Sqlcon. Sqlconnectionf (SOU, SEL);

}

}

}

---------------------This article from Andrewniu csdn blog, full-text address please click: 53065522?utm_source=copy

DATASET () Usage

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.