<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Oledb" %>
Dim objConn as New OleDBConnection
("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=e:\sff\site\db\users.mdb")
objConn.Open()
objConn.Close()
objConn=Nothing
Dim objCmd as New OleDbCommand("SELECT * From users", objConn)
Dim objCmd as New OleDbCommand()
objCmd.Connection = objConn
objCmd.CommandText = "SELECT * FROM users"
Dim objCmd as New OleDbCommand(SQL String, connection string)
ExecuteNonQuery
objCmd.ExecuteNoQuery
Dim objRd as OleDbDataReader
objRd = objCmd.ExeuteReader
Dim objReader as OleDbDataReader
objReader = objCmd.ExecuteReader
While objReader.Read
Response.Write(objReader.GetString(0) & "<br>")
End While
Dim objAdapter as New OleDbDataAdapter("SELECT * FROM users", objConn)
Dim ds as Dataset = New DataSet()
objAdapter.Fill(ds, "users")
Mappings
objAdapter.TableMappings.Add("adbtable", "users")
With objAdapter.TableMappings(0).ColumnMappings
.Add("PID", "ID")
.Add("LastName", "LName")
.Add("StreetAddress", "Addy")
End With
objAdapter.Fill(ds)
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Oledb" %>
<script language="VB" runat="server">
Sub Page_Load(sender as object, e as eventargs)
objConn.Open()
Dim objAdapter as New OleDbDataAdapter("SELECT * FROM users", objConn)
Dim objCmdBld As New OleDbCommandBuilder(objAdapter)
</script>
二、 Data Set
Dim ds1 As New DataSet()
Dim dtable As new DataTable("people")
With dtable.Columns
.Add("FName", System.Type.GetType("System.String"))
.Add("LName", System.Type.GetType("System.String"))
.Add("UID", System.Type.GetType("System.Int32"))
End With
dtable.Columns("UID").AutoIncrement = True
ds1.Tables.Add(dtable)
ds1.Tables("people").PrimaryKey = pkey
DataTables
Dim dtable As DataTable = ds1.Tables("people")
Dim row as DataRow = dtable.NewRow()
row(0) = "Philip"
row(1) = "Quinn"
dtable.Rows.Add(row)
dtable.AcceptChanges
<%@ Import Namespace="System.Data" %>
<script language="VB" runat="server">
Sub Page_Load(sender as object, e as eventargs)
Dim ds1 As New DataSet()
Dim dtable As new DataTable("people")
With dtable.Columns
.Add("FName", System.Type.GetType("System.String"))
.Add("LName", System.Type.GetType("System.String"))
.Add("UID", System.Type.GetType("System.Int32"))
End With
ds1.Tables.Add(dtable)
row(0) = "Philip"
row(1) = "Quinn"
dtable2.Rows.Add(row)
dtable2.AcceptChanges
End Sub
</script>
ds1.Tables("people").Rows(0)("FName") = "Phil"
Dim drow as DataRowCollection = ds1.Tables("people").Rows
Dim drow2 as DataRow = drow.Item(0)
drow2(0) = "Philip"
drow2.AcceptChanges
通过这个图示我们很容易就理清了DataSet、DataTable、DataRowCollection、DataRows、DataColumn和DataColumnCollection之间的相互关系。
三、 DataSet的使用
DataSet <-> Data Source
Field Name Type FName LName ID
FName Text Philip Quinn 1
LName Text Joesph Payne 2
ID AutoNumber Douglas Adams 3
Michael Okuda 4
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data.Oledb" %>
<script language="VB" runat="server">
Sub Page_Load(sender as object, e as eventargs)
Dim objConn as New OleDBConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=e:\sff\site\db\test.mdb")
objConn.Open()
Dim objAdapter as New OleDbDataAdapter("SELECT * FROM users", objConn)
Dim objCmdBld as New OleDbCommandBuilder(objAdapter)
Dim drow as DataRow
drow = ds.Tables("users").NewRow()
drow(1) = "Rodenberry"
ds.Tables("users").Rows.Add(drow)
drow(1) = "Stewart"
ds.Tables("users").Rows.Add(drow)
objAdapter.Update(ds, "users")
End Sub
</script>
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data.Oledb" %>
<script language="VB" runat="server">
Sub Page_Load(sender as object, e as eventargs)
objConn.Open()
Dim objAdapter as New OleDbDataAdapter("SELECT * FROM users", objConn)
Dim objCmdBld as New OleDbCommandBuilder(objAdapter)
</script>
四、数据显示和修改
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data.Oledb" %>
<script language="VB" runat="server">
Sub Page_Load(sender as object, e as eventargs)
objConn.Open()
Dim objAdapter as New OleDbDataAdapter("SELECT * FROM users", objConn)
Dim objCmdBld as New OleDbCommandBuilder(objAdapter)
Repeater1.DataSource = ds.Tables("users").DefaultView
DataBind()
Rpt.DataSource = ds.Tables("users").DefaultView
DataBind()
End Sub
</script>
<html><body><font face="Arial" size="2">
<asp:repeater id="Rpt" runat="server">
<table border="0" cellspacing="1" cellpadding="3">
<tr>
<td bgcolor="#6699FF" width="25%">Last Name</td>
<td bgcolor="#6699FF" width="25%">First Name</td>
</tr>
</HeaderTemplate>
<tr>
<td>
<%# Container.DataItem("LName") %>
</td><td>
<%# Container.DataItem("FName") %>
</td>
</tr>
</ItemTemplate>
</table>
</FooterTemplate>
</font></body></html>
DataList
dl.DataSource = ds.Tables("users").DefaultView
Databind()
End Sub
Sub dl_ItemCommand(sender as object, e as DataListCommandEventArgs)
dl.SelectedIndex = e.Item.ItemIndex
dl.DataBind()
End Sub
</script>
<asp:datalist id="dl" runat="server"
HeaderStyle-BackColor="#6699FF"
SelectedItemStyle-BackColor="#6666FF"
SelectedItemStyle-ForeColor="#FFFFFF"
RepeatLayout = "table"
RepeatDirection = "vertical"
DataKeyField = "ID"
OnItemCommand="dl_ItemCommand">
Last Name, click for full name.
</HeaderTemplate>
<asp:linkbutton id="b1" runat="server" Text='<%# Container.DataItem("LName") %>' CommandName = "select" />
<br>
</ItemTemplate>
<%# Container.DataItem("LName") & ", " & Container.DataItem("FName") %>
<br>
</SelectedItemTemplate>
</asp:datalist>
</form></font>
</body>
</html>
dg.DataSource = ds.tables("users").DefaultView
DataBind()
End Sub
</script>
以上代码很简单,就是绑定数据。
dg.DataSource = ds.tables("users").DefaultView
DataBind()
End Sub
</script>
BorderColor="black"
GridLines="vertical"
cellpadding="3"
cellspacing="1"
width="50%"
Font-Names="Arial"
Font-Size="10pt"
HeaderStyle-BackColor="#6699FF"
AlternatingItemStyle-BackColor="#6666FF"
AutoGenerateColumns="False">
<Columns>
<ItemTemplate>
<asp:label id="Name" runat="server" Text='<%# Container.DataItem("FName") & " " & Container.DataItem("LName") %>' />
</ItemTemplate>
</asp:templatecolumn>
可能有一点复杂,我们看说明:
五、理解DataGrid
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Oledb" %>
<script language="VB" runat="server">
Dim objConn as New OleDBConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=e:\sff\site\db\test.mdb")
Dim ds as Dataset = New DataSet()
Dim objAdapter as New OleDbDataAdapter("SELECT * FROM users", objConn)
objConn.Open()
objAdapter.Fill(ds, "users")
dg.DataSource = ds
dg.DataMember = "users"
If Not Page.IsPostBack Then
dg.Databind()
End If
End Sub
dg.edititemindex = -1
dg.DataBind()
end sub
dg.edititemindex = -1
dg.databind()
end sub
</script>
自定义DataGrid
<form runat="server">
<asp:DataGrid id="dg" runat="server"
Bordercolor="black"
gridlines="vertical"
font-names="Arial"
font-size="10pt"
HeaderStyle-BackColor="#FFCC00" '表格头信息
ItemStyle-BackColor="#FFCC66" '每行背景色
AlternatingItemStyle-BackColor="#FFFFFF" '交替背景色
AutoGenerateColumns="False" '自定义每行
OnEditCommand="dg_edit" '以下三个事件处理
OnCancelCommand="dg_cancel"
OnUpdateCommand="dg_update">
<Columns>
<asp:boundcolumn readonly="true" headertext="ID" DataField="Auto" />
<asp:boundcolumn headertext="First Name" DataField="Number1" />
<asp:boundcolumn headertext="Last Name" Datafield="Number2" />
<asp:editcommandcolumn edittext="Edit" CancelText="Cancel" UpdateText="Save" HeaderText="" />
</Columns>
</form>
sub dg_edit(sender as object, e as DataGridCommandEventArgs)
dg.databind()
end sub
sub dg_update(sender as object, e as DataGridCommandEventArgs)
Dim FNAME As TextBox = e.item.cells(1).controls(0)
Dim LNAME As TextBox = e.item.cells(2).controls(0)
Dim sqlstr As String
"Number1 = '" & FNAME.Text & "', " & _
"Number2 = '" & LNAME.Text & "' " & _
"WHERE Auto = " & e.item.cells(0).text
objCommand.ExecuteNonQuery()
objAdapter.Fill(ds, "users")
dg.DataBind()
end sub
六、DataGrid数据排序
<asp:DataGrid id="dg" runat="server"
Bordercolor="black"
gridlines="vertical"
font-names="Arial"
font-size="10pt"
HeaderStyle-BackColor="#FFCC00"
ItemStyle-BackColor="#FFCC66"
AlternatingItemStyle-BackColor="#FFFFFF"
AutoGenerateColumns="True"
OnEditCommand="dg_edit"
OnCancelCommand="dg_cancel"
OnUpdateCommand="dg_update"
OnSortCommand="dg_sort"
AllowSorting="True"
>
</asp:dataGrid>
排序事件处理
sub dg_sort(sender as object, e as DataGridSortCommandEventArgs)
Dim dtable as DataTable = ds.Tables("users")
Dim dview as new DataView(dtable)
dview.sort = e.SortExpression.ToString & " ASC"
dg.DataSource = dview
dg.databind()
end sub
<form runat="server">
<asp:DataGrid id="dg" runat="server"
Bordercolor="black"
gridlines="vertical"
font-names="Arial"
font-size="10pt"
HeaderStyle-BackColor="#FFCC00"
ItemStyle-BackColor="#FFCC66"
AlternatingItemStyle-BackColor="#FFFFFF"
AutoGenerateColumns="True"
OnEditCommand="dg_edit"
OnCancelCommand="dg_cancel"
OnUpdateCommand="dg_update"
OnSortCommand="dg_sort"
AllowSorting="True"
>
</asp:dataGrid>
<p>
<asp:radiobuttonlist runat="server" id="sortby" repeatdirection="horizontal" textalign="right" >
<asp:listitem selected="true">Ascending</asp:listitem>
<asp:listitem>Descending</asp:listitem>
</asp:radiobuttonlist>
</form>
以上大部分代码我们都很熟悉,只是在最后加了两个选项来选择升序还是降序。我们来看事件处理:
sub dg_sort(sender as object, e as DataGridSortCommandEventArgs)
Dim sortby = Request.Form("sortby")
Dim dtable as DataTable = ds.Tables("users")
Dim dview as new DataView(dtable)
If sortby = "Ascending" Then
dview.sort = e.SortExpression.ToString & " ASC"
Else If sortby = "Descending" Then
dview.sort = e.SortExpression.ToString & " DESC"
End If
dg.DataSource = dview
dg.databind()
end sub
总结:
本书从最基础的知识讲起,结合丰富的示例,由浅入深,内容翔实,是掌握Visual Basic 2005编程的权威图书,非常适合初学者迅速入门并获得提高,也适用于有经验的编程人员快速掌握 Visual Studio编程的核心技术。本书是各级程序员学习Visual Basic 2005编程技术必备的参考书。