《位於记忆体的资料库》由会员分享,可在线阅读,更多相关《位於记忆体的资料库(27页珍藏版)》请在金锄头文库上搜索。
1、4-5. Data Set,Ch. 18,Data Set,位於記憶體的資料庫 可包含多個的資料表 搭配DataAdapter使用 使用Fill()方法讀入DataSet物件 可將更新後的資料送回資料庫 離線模式 使用Update()方法,DataSet物件一覽表,DataTableCollection,DataColumn,DataRow,使用DataSet物件的步驟,建立資料連結 對資料來源下SQL命令 使用DataAdapter物件 存取DataSet物件,使用DataAdapter物件獲取DataSet物件,宣告方式 Dim objCmd As New OleDbDataAdapter
2、() Dim objCmd As New OleDbDataAdapter(selectCommand) Dim objCmd As New OleDbDataAdapter(selectCommandText, selectConnection) Dim objCmd As New OleDbDataAdapter(selectCommandText, selectConnectionString),Command 物件,SQL 字串,Example (範例 方法二),Dim objConn As New OleDbConnection() objConn.ConnectionString
3、= “Provider=Microsoft.Jet.OLEDB.4.0;“ & “Data Source=“ & Server.MapPath(“/App_Data/Friend.mdb“) objConn.Open()Dim SelectCmd As New OleDbCommand(“Select *From 擇友俱樂部“, objConn) Dim objCmd As New OleDbDataAdapter(SelectCmd),Example (範例 方法四),Dim objCmd As New OleDbDataAdapter(“Select * From 擇友俱樂部“, “Pro
4、vider=Microsoft.Jet.OLEDB.4.0; DataSource=“ & Server.MapPath(“/App_Data/Friend.mdb“),建立DataSet物件,Dim DS As New DataSet() Dim DS As New DataSet(dataSetName),連結DataAdapter and DataSet,Dim objConn As New OleDbConnection() objConn.ConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;“ & “Data Source=“ &
5、 Server.MapPath(“/App_Data/Friend.mdb“) Dim objCmd As New OleDbDataAdapter(“Select * From 擇友俱樂部“, objConn) Dim DS As New DataSet() objCmd.Fill(DS, “擇友俱樂部“),Example (範例),Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)取得 web.config 組態檔的資料連接設定Dim setting As ConnectionStringSettings = Config
6、urationManager.ConnectionStrings(“OLEDB_Friend“) 建立 DataAdpter 物件Dim objCmd As New OleDbDataAdapter(“Select * From 擇友俱樂部“, setting.ConnectionString)建立 DataSet 物件Dim DS As New DataSet()objCmd.Fill(DS, “擇友俱樂部“)宣告 myTable 變數為DataTable物件,並將擇友俱樂部資料表指派給它Dim myTable As DataTable = DS.Tables(“擇友俱樂部“)顯示 Data
7、Set 物件內擇友俱樂部資料表的所有欄位名稱Dim I, J As IntegerResponse.Write(“)For I = 0 To myTable.Columns.Count - 1Response.Write(“ & myTable.Columns(I).Caption & “)NextResponse.Write(“),顯示 DataSet 物件內擇友俱樂部資料表的所有資料 For I = 0 To myTable.Rows.Count - 1Response.Write(“)For J = 0 To myTable.Columns.Count - 1Response.Write
8、(“ & myTable.Rows(I).Item(J) &“)NextResponse.Write(“) Next Response.Write(“) End Sub ,Practice (練習),利用網頁,顯示price.mdb資料表的所有資料 利用網頁,查詢所有資料的姓名欄位,並且使用BulletedList的方法呈現,運用 DataSet,DataTableCollection (資料表集合) DataColumnCollectoin (資料欄集合) DataRowCollection (資料列集合),DataTableCollection,屬性 Count Item(index|na
9、me) 方法 Add() Add(table) Add(tableName) Clear() Contains(tableName) Remove(table|tableName) RemoveAt(index),DataTable,屬性 CaseSensitive=True|False Columns DataSet HasErrors MinimumCapacity=n Rows TableName=“ 方法 AcceptChanges() Clear() Clone() Compute(expression, filter) Copy() GetChanges(Added|Deleted
10、|Detached|Modified|Unchanged) GetErrors() ImportRow(row) NewRow() RejectChanges() Select(),DataColumnCollection,屬性 Count Item(index|name) 方法 Add() Add(column) Add(columnName) Add(columnName, type) Clear() Contains(columnName) Remove(column|columnName) RemoveAt(index),DataColumn,屬性 AutoIncrement=True
11、|False AutoIncrementSeed=n AutoIncrementStep=n AllowDBNull=True|False Caption=“ ColumnName=“ DataType DefaultValue=“ Expression=“ MaxLength=n Ordinal ReadOnly=True|False Table Unique=True|False,DataRowCollection,屬性 Count Item(index) 方法 Add(row) Clear() Contains(key) Find(key) IndexOf(row) Remove(row
12、) RemoveAt(index),DataRow,屬性 HasErrors Item(columnName|index) ItemArray= RowError=“ RowState Table 方法 AcceptChanges() ClearErrors() Delete() GetColumnError(columnName|index|column) GetColumnsInError() IsNull(columnName|index|column) RejectChanges() SetAdded() SetColumnError(columnName|index| column,
13、 string) SetModified(),Practice (練習),Public myTable As DataTableSub Page_Load(ByVal sender As Object, ByVal e As Eventargs)Dim setting As ConnectionStringSettings = _ConfigurationManager.ConnectionStrings(“OLEDB_Price“)建立 DataAdpter 物件Dim objCmd As New OleDbDataAdapter(“Select * From 零組件報價表“, settin
14、g.ConnectionString)建立 DataSet 物件Dim DS As New DataSet()objCmd.Fill(DS, “零組件報價表“),將零組件報價表資料表指派給 myTable 變數myTable = DS.Tables(“零組件報價表“)If Not IsPostBack ThenDim I As IntegerFor I = 0 To myTable.Rows.Count - 1 row.Items.Add(I)row.Items(I).Text = “第 “ & I + 1 & “ 列“row.Items(I).Value = INextFor I = 0 T
15、o myTable.Columns.Count - 1 column.Items.Add(I)column.Items(I).Text = myTable.Columns(I).ColumnNamecolumn.Items(I).Value = myTable.Columns(I).ColumnNameNextEnd IfEnd Sub,Sub show_Click(sender As Object, e As EventArgs)Message.Text = row.SelectedItem.Text & “, 欄位的值為【“ & myTable.Rows(row.SelectedValue).Item(column.SelectedValue) & “】“End Sub請選擇要顯示的資料列及欄位:資料列:欄位:結果:,