DataSet對象是ADO.NET的中心概念,它獨立于各種資料源.是支援斷開式,分布式的核心對象.DataSet對象包含任意多個表,每個DataSet中的資料表對應于一個資料源中的資料表.
DataSet可利用DataAdapter填充.
DataAdapter對象是DataSet和資料庫之間的橋梁.主要是從資料源中檢索資料,然後填充到DataSet對象中..Net中主要有兩種DataAdapter對象,OleDataAdapter和SqlDataAdapter.
DataSet和DataAdapter配合使用的大緻過程是:
SqlDataAdapter myDa = new SqlDataAdapter(SqlStr, MyCon);//建立DataAdapter對象,SqlStr是一個查詢語句,MyCon是一個資料庫連接配接
DataSet myDs = new DataSet();
myDa.Fill(myDs);//填充DataSet對象
下面是使用DataSet和DataAdapter對象的一個具體例子.
顯示Music資料源中的條目,如果名字過長,用省略号代替.
思路為.建立DataSet對象,用DataAdapter對象查詢結果并且填充DadaSet對象,修改DataSet對象的表.然後和GridView綁定以顯示資料.
首先在SqlServer中準備好我們的資料庫,名字為Music,表名為catalog.兩列分别為id int和name nvarchar.
建立網站,添加一個web窗體
在web窗體中添加一個GredView控件,名字為GridView1.
在web.config中添加連接配接字元串
<appSettings>
<add key="ConnectString" value="server=localhost;database=Music;UId=sa;password=1"/>
</appSettings>
在Default.aspx.cs中
添加三個命名空間
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
建立一個GetConnection方法,使用ConfigurationManager擷取web.config中的連接配接字元串
public SqlConnection GetConnection()
{
string MyStr = ConfigurationManager.AppSettings["ConnectString"].ToString();
SqlConnection MyCon = new SqlConnection(MyStr);
return MyCon;
}
建立一個SubStr函數,用于把超過一定長度的字元串用省略号顯示
public string SubStr(string sString, int nLeng)
{
if (sString.Length <= nLeng)
{
return sString;
}
string sNewStr = sString.Substring(0, nLeng);
sNewStr = sNewStr + "...";
return sNewStr;
}
在PageLoad事件中,添加如下代碼
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection MyCon = GetConnection();
MyCon.Open();
string SqlStr = "select * from catalog";
SqlDataAdapter myDa = new SqlDataAdapter(SqlStr, MyCon);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
for (int i = 0; i <= myDs.Tables[0].Rows.Count - 1; i++)
{
myDs.Tables[0].Rows[i]["name"] =
SubStr(Convert.ToString(myDs.Tables[0].Rows[i]["name"]), 5);
}
GridView1.DataSource = myDs;
GridView1.DataKeyNames = new string[] { "id" };//id為資料庫中的主鍵
GridView1.DataBind();
}
}
運作,打開的網頁如下
id | name |
---|---|
1 | ji... |
2 | ha... |
3 | gg... |
6 | my... |
總結:
1.GridView對象的使用目前遇到兩種方法,一種是在控件上直接選擇資料源,可以實作和資料源的自動綁定.
另一種就是本例子中的方法,實作和DataSet的綁定,這樣的目的是從資料源擷取資料後,可以對資料進行一些處理,然後在現實在GridView中.代碼如下
GridView1.DataSource = myDs;
GridView1.DataKeyNames = new string[] { "id" };//"id"和資料源中的列名應該一緻
GridView1.DataBind();
2.本例中DataSet對象的使用為
myDs.Tables[0].Rows[i]["name"]
如果在填充時指定fill的第二個參數(給這個虛拟表的一個名字)
myDa.Fill(myDs,"catalog");
則可以通過表名調用,而不必用下标,這樣避免忘了是哪個下标
myDs.Tables["catalog"].Rows[i]["name"]
示例的建立過程編寫後又經過親測,按照步驟一步步就可以成功運作,如果差錯,請不吝指正.
補充:
使用DataSet中的資料更新資料庫
通過DataAdapter對象建立SqlCommandBuilder對象,再使用DataAdapter.Update,即可由SqlCommandBuilder對象自動生成INSERT,UPDATE或DELETE指令
要求DataSet中的資料必須至少存在一個主鍵列或唯一的列
在上述示例中,把Pageload中的代碼替換成如下代碼,即可實作DataSet修改後更新到資料庫中.
if (!IsPostBack)
{
SqlConnection MyCon = GetConnection();
MyCon.Open();
string SqlStr = "select * from catalog";
SqlDataAdapter myDa = new SqlDataAdapter(SqlStr, MyCon);
DataSet myDs = new DataSet();
SqlCommandBuilder builder = new SqlCommandBuilder(myDa);//使用SqlCommandBuilder對象
myDa.Fill(myDs, "id");
for (int i = 0; i <= myDs.Tables[0].Rows.Count - 1; i++)
{
myDs.Tables["id"].Rows[i]["name"] =
SubStr(Convert.ToString(myDs.Tables["id"].Rows[i]["name"]),2);
}
myDa.Update(myDs, "id");
GridView1.DataSource = myDs;
GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind();
myDa.Dispose();
myDs.Dispose();
MyCon.Close();
}
運作後去SqlServer中檢視,資料庫中的内容也被更新了,關鍵語句如下
SqlCommandBuilder builder = new SqlCommandBuilder(myDa);//使用SqlCommandBuilder對象
myDa.Fill(myDs, "id");
myDa.Update(myDs, "id");//更新