DATASET用法
DataSet是ADO.NET的中心概念。可以把DataSet當(dāng)成內(nèi)存中的數(shù)據(jù)庫,DataSet是不依賴于數(shù)據(jù)庫的獨(dú)立數(shù)據(jù)集合。所謂獨(dú)立,就是說,即使斷開數(shù)據(jù)鏈路,或者關(guān)閉數(shù)據(jù)庫,DataSet依然是可用的,DataSet在內(nèi)部是用XML來描述數(shù)據(jù)的,由于XML是一種與平臺無關(guān)、與語言無關(guān)的數(shù)據(jù)描述語言,而且可以描述復(fù)雜關(guān)系的數(shù)據(jù),比如父子關(guān)系的數(shù)據(jù),所以DataSet實(shí)際上可以容納具有復(fù)雜關(guān)系的數(shù)據(jù),而且不再依賴于數(shù)據(jù)庫鏈路。
在典型的多層實(shí)現(xiàn)中,用于創(chuàng)建和刷新 DataSet 并依次更新原始數(shù)據(jù)的步驟包括: 通過 DataAdapter 使用數(shù)據(jù)源中的數(shù)據(jù)生成和填充 DataSet 中的每個 DataTable。 通過添加、更新或刪除 DataRow 對象更改單個 DataTable 對象中的數(shù)據(jù)。 調(diào)用 GetChanges 方法以創(chuàng)建只反映對數(shù)據(jù)進(jìn)行的更改的第二個 DataSet。 調(diào)用 DataAdapter 的 Update 方法,并將第二個 DataSet 作為參數(shù)傳遞。 調(diào)用 Merge 方法將第二個 DataSet 中的更改合并到第一個中。 針對 DataSet 調(diào)用 AcceptChanges。或者,調(diào)用 RejectChanges 以取消更改。 需要注意的是:dataset所有數(shù)據(jù)都加載在內(nèi)存上執(zhí)行的,可以提高數(shù)據(jù)訪問速度,提高硬盤數(shù)據(jù)的安全性。極大的改善了程序運(yùn)行的速度和穩(wěn)定性。使用方法
1、創(chuàng)建DataSet對象
DataSet ds = new DataSet();
DataSet ds = new DataSet("DataSetName");
2、用數(shù)據(jù)集填充DataSet
最常用的是DataAdapter對象的Fill()方法給他填充數(shù)據(jù)
(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、訪問DataSet中的表、行和列 值
(1): 訪問每個 DataTable
按表名訪問:ds.Tables["mytest"] //指定DataTable對象mytest(即訪問DataSet中名為mytest的DataTable)
按索引(索引基于0的)訪問:ds.Tables[0] //指定DataSet中的第一個DataTable
(2): 訪問DataTable中的行
ds.Tables["mytest"].Rows[n] //訪問mytest表 的第n+1行(行的索引是從0開始的)
ds.Tables[i].Rows[n] //訪問DataSet中的第i+1個DataTable 的第n+1列(列的索引是從0開始的)
(3): 訪問DataTable中的某個元素
ds.Tables["mytest"].Rows[n][m] //訪問mytest表的第n+1行第m+1列的元素
ds.Tables[i].Rows[n][m] //訪問DataSet中的第i+1個DataTable 表的第n+1行第m+1列的元素
ds.Tables["mytest"].Rows[n][name] //訪問mytest表的第n+1行name列的元素
ds.Tables[i].Rows[n][name] //訪問DataSet中的第i+1個DataTable 表的第n+1行name列的元素
(4): 取DataTable中的列名
ds.Tables["mytest"].Columns[n] //取出mytest表的n+1列列名
ds.Tables[i].Columns[n]
4、實(shí)例
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)
{
//創(chuàng)建連接
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());
}
//創(chuàng)建DataSet
DataSet ds = new DataSet();
//將數(shù)據(jù)添加到DataSet中
adapt.Fill(ds,"mytest");
//取出mytest表各列名
Console.WriteLine("{0,-15} {1,-10} {2,-10}",ds.Tables["mytest"].Columns[0],
ds.Tables["mytest"].Columns[1],ds.Tables["mytest"].Columns[2]);
//輸出mytest表中第六行
DataRow row1 = ds.Tables["mytest"].Rows[5];
Console.WriteLine("{0,-15} {1,-10} {2,-10}",row1[0],row1[1],row1[2]);
//輸出mytest表中第五行的第二列的值
DataRow row2 = ds.Tables["mytest"].Rows[4];
Console.WriteLine(" {0,-25} ", row2[1]);
//下列兩種方法等效都等同于row2[1](即第五行的第二列的值)
Console.WriteLine(" {0,-25} ", ds.Tables["mytest"].Rows[4][1]);
Console.WriteLine(" {0,-25} ", ds.Tables["mytest"].Rows[4]["number"]);
//輸出DataSet中的所有數(shù)據(jù)
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"]);
//取第三列的值
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);
}
}
}
總結(jié)
- 上一篇: mac安装mysql记录,使用zsh
- 下一篇: 微信小程序----全局变量