標籤:table ogr error 資料集 元素 提高 tps sqlcmd sys
DataSet是ADO.NET的中心概念。可以把DataSet當成記憶體中的資料庫,DataSet是不依賴於資料庫的獨立資料集合。所謂獨立,就是說,即使斷開資料鏈路,或者關閉資料庫,DataSet依然是可用的,DataSet在內部是用XML來描述資料的,由於XML是一種與平台無關、與語言無關的資料描述語言 (Data Description Language),而且可以描述複雜關係的資料,比如父子關係的資料,所以DataSet實際上可以容納具有複雜關係的資料,而且不再依賴於資料庫鏈路。
在典型的多層實現中,用於建立和重新整理 DataSet 並依次更新未經處理資料的步驟包括:通過 DataAdapter 使用資料來源中的資料產生和填充 DataSet 中的每個 DataTable。通過添加、更新或刪除 DataRow 對象更改單個 DataTable 對象中的資料。調用 GetChanges 方法以建立只反映對資料進行的更改的第二個 DataSet。調用 DataAdapter 的 Update 方法,並將第二個 DataSet 作為參數傳遞。調用 Merge 方法將第二個 DataSet 中的更改合并到第一個中。針對 DataSet 調用 AcceptChanges。或者,調用 RejectChanges 以取消更改。需要注意的是:dataset所有資料都載入在記憶體上執行的,可以提高資料訪問速度,提高硬碟資料的安全性。極大的改善了程式啟動並執行速度和穩定性。
使用方法
1、建立DataSet對象
DataSet ds = new DataSet();
DataSet ds = new DataSet("DataSetName");
2、用資料集填充DataSet
最常用的是DataAdapter對象的Fill()方法給他填充資料
(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、執行個體
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)
{
//建立串連
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());
}
//建立DataSet
DataSet ds = new DataSet();
//將資料添加到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中的所有資料
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);
}
}
}
--------------------- 本文來自 Andrewniu 的CSDN 部落格 ,全文地址請點擊:53065522?utm_source=copy
DATASET()用法