asp07.htm

来自「VB.NET网络编程指南 书籍语言: 简体中文 书籍类型」· HTM 代码 · 共 313 行 · 第 1/2 页

HTM
313
字号
  Dim 对象名 As New DataSet(数据集名) 格式①未指出数据集名,可在创建DataSet对象后用DataSetName属性进行设置。<br>
  DataSet对象的属性和方法列于表中。</p>
<p align="center"><img src="image/slide7image012.PNG" width="608" height="253"></p>
<p>DataSet对象的常用方法有Clear()和Copy() 。<br>
  &nbsp;&nbsp;&nbsp; 2. 数据表集合和DataTable对象 DataSet的所有数据表包含于数据表集合DataTableCollection中,通过DataSet的 
  Tables属性访问DataTableCollection。DataTableCollection有以下两个属性:<br>
  Count:DataSet对象所包含的DataTable个数。<br>
  Item({index,name}):获取DataTableCollection中下标为index或名称为name的数据表。 DataTableCollection有以下常用方法:<br>
  Add({table,name}):向DataTableCollection中添加数据表。<br>
  Clear():清除DataTableCollection中的所有数据表。<br>
  CanRemove(table):判断参数table指定的数据表能否从DataTableCollection中删除。<br>
  Contains(name):判断名为name的数据表是否被包含在DataTableCollection中。<br>
  IndexOf({table,name}):获取数据表的序号。<br>
  Remove({table,name}):删除指定的数据表。<br>
  RemoveAt(index):删除下标为index的数据表。 <br>
  创建DataTable对象的语法格式如下: ① Dim 对象名 As New DataTable() ② Dim 对象名 As New DataTabe(&quot;数据表名&quot;) 
  下表分别列出了DataTable对象的常用属性、常用方法和事件。</p>
<p align="center"><img src="image/slide7image013.PNG" width="645" height="172"><br>
  <img src="image/slide7image014.PNG" width="648" height="109"><br>
  <img src="image/slide7image015.PNG" width="649" height="178"></p>
<p>3. 数据列集合和DataColumn对象<br>
  DataColumnColection有以下2个属性:<br>
  Count:数据表所包含的字段个数。<br>
  Item({index,name}):获取下标为index或名称为name的字段。创建DataColumn对象的语法格式如下: <br>
  &nbsp; ① Dim 对象名 As New DataColumn() <br>
  &nbsp; ② Dim 对象名 As New DataColumn(&quot;字段名&quot;) <br>
  &nbsp; ③ Dim 对象名 As New DataColumn(&quot;字段名&quot;,数据类型)<br>
  下表列出了DataColumn对象的常用属性。</p>
<p align="center"><img src="image/slide7image016.PNG" width="605" height="313"></p>
<p>4. 数据行集合和DataRow对象<br>
  创建DataRow对象的语法格式如下: <br>
  &nbsp; ① Dim 对象名 As DataRow <br>
  &nbsp; ② Dim 对象名 As DataRow = DataTable对象名.NewRow() <br>
  DataRow对象的属性主要有:<br>
  Item({index,columnName}):获取或设置指定字段的值。<br>
  Table:获取包含该数据行的DataTable对象。 <br>
  DataRow对象的方法主要有:<br>
  AcceptChanges():将所有变动过的数据行更新到DataRowCollection。<br>
  Delete():删除数据行。<br>
  IsNull({colName,index,Column对象名}):判断指定列或Column对象是否为空值。 <br>
  7.4.3 数据库查询操作 <br>
  1. DataView对象及使用 定义DataView对象的语法格式为: <br>
  &nbsp; ① Dim 对象名 As DataView = New DataView() <br>
  &nbsp; ② Dim 对象名 As DataView = New DataView(table) <br>
  &nbsp; ③ Dim 对象名 As DataView = New DataView(table,rowFilter,sort,rowState)<br>
  DataView对象的主要属性有:<br>
  AllowDelete:获取或设置是否允许删除DataView中的数据行。类似的属性还有AllowEdit、AllowNew。<br>
  Count:获取DataView对象中包含的数据行数。<br>
  Item(recordIndex):获取DataView对象中下标为recordIndex的DataRowView对象。<br>
  RowFilter:获取或设置选取数据行的表达式。<br>
  Sort:获取或设置排序字段和方式。<br>
  Table:获取或设置DataView对象对应的数据表名。 <br>
  DataView对象的主要方法有:<br>
  AddNew():添加一条记录到DataView对象中。<br>
  Delete(index):删除下标为index的数据行。<br>
  2. 执行带参数查询<br>
  使用DataAdapter对象执行带参数的SQL查询,其方式与在Command对象中执行带参数查询是一样的,因为DataAdapter是建立在Command对象之上的。<br>
  7.4.4 通过DataAdapter对象更新数据库<br>
  使用Command对象可对数据库进行更新操作,使用DataAdapter可以执行多个SQL命令。但注意,只有执行了Update()方法后,才会对物理数据库进行更新。 
  <br>
  7.4.5 执行存储过程 ADO.NET中执行存储过程,主要是通过Command对象来实现的。<br>
  7.5 数据显示 <br>
  7.5.1 Repeater控件 Repeater控件语法格式如下: <br>
  &lt;ASP: Repeater Runat=“Server” Id=“控件标识” DataSource=“&lt;%#数据来源%&gt;” <br>
  OnItemCommand=&quot;…&quot; OnItemCreate=&quot;…&quot; OnItemDataBound=&quot;…&quot;&gt; 
  <br>
  &lt;模板名称&gt;HTML格式的模板&lt;/模板名称&gt; <br>
  &lt;/ASP:Repeater&gt; Repeater控件使用模板来控制数据的输出格式。模板的格式说明列于表中。 <br>
  Repeater控件所增加的属性和事件分别列于表中。</p>
<p align="center"><img src="image/slide7image017.PNG" width="618" height="190"><br>
  <img src="image/slide7image018.PNG" width="616" height="320"> <br>
  <img src="image/slide7image019.PNG" width="615" height="188"> </p>
<p>OnItemCommand事件的参数为RepeaterCommandEventArgs,该参数可取值为:<br>
  (1)CommandArgument<br>
  (2)CommandName<br>
  (3)CommandSource<br>
  (4)Item <br>
  7.5.2 DataList控件 <br>
  DataList控件与Repeater控件相似,也是使用模板控制数据显示的格式。 DataList控件定义语法格式为:<br>
  &lt;ASP: DataList Runat=&quot;Server&quot; Id=&quot;控件标识&quot; CellPadding=&quot;n&quot; 
  CellSpacing=&quot;n&quot; DataSource=&quot;&lt;%#数据来源%&gt;&quot; <br>
  DataKeyField=&quot;…&quot; ExtractTemplateRow=&quot;{True,False}&quot; GridLines=&quot;{None,Horizontal,Vertical,Both}&quot; 
  <br>
  RepeatColumns=&quot;n&quot; RepeateDirection=&quot;{Horizontal,Vertical}&quot; 
  RepeatLayout=&quot;{Flow,Table}&quot; <br>
  ShowFooter=&quot;{True,False}&quot; ShowHeader=&quot;{True,False}&quot; OnItemCommand=&quot;…&quot; 
  OnCancelCommand=&quot;…&quot; OnDeleteCommand=&quot;…&quot; <br>
  OnEditCommand=&quot;…&quot; OnUpdateCommand=&quot;…&quot; OnItemCreated=&quot;…&quot; 
  &gt; <br>
  &lt;模板名称&gt;HTML格式的模板&lt;/模板名称&gt; <br>
  &lt;/ASP:DataList&gt; 表列出了DataList控件的常用属性。 按用途,可把DataList控件的属性可分为以下几类:<br>
  用于模板设置的属性、用于控制Table显示特征的属性、用于模板显示特性设置的属性、用于数据表项处理的属性。</p>
<p align="center"><img src="image/slide7image020.PNG" width="640" height="296"><br>
  <img src="image/slide7image021.PNG" width="642" height="117"><br>
  <img src="image/slide7image022.PNG" width="643" height="232"></p>
<p>&nbsp;&nbsp;&nbsp; 表中的“取消”、“编辑”、“删除”和“更新”按钮指的是CommandName属性值分别为“Cancel”、“Edit”、“Delete”和“Update”的Button、ImageButton或LinkButton控件。 
  例如,下面的代码定义了具有事件处理的DataList控件: <br>
  &lt;ASP:DataList Runat=&quot;Server&quot; Id=&quot;StuDL&quot; OnUpdateCommand=&quot;DataList_UpdateCommand&quot; 
  OnDeleteCommand=&quot;DataList_DeleteCommand&quot; <br>
  OnCancelCommand=&quot;DataList_CancelCommand&quot; DataKeyField=&quot;studentid&quot;&gt; 
  &lt;! --模板定义 -- &gt;&lt;/ASP:DataList&gt;<br>
  &nbsp;&nbsp;&nbsp; DataList控件还有一组用于定义显示样式的属性,主要有:BackColor,BorderColor,BorderStyle,BorderWidth 
  ,Font,ForeColor,Height,HorizontalAglin,VerticalAlign,Width <br>
  7.5.3 DataGrid控件 <br>
  &nbsp; 1. DataGrid控件定义 DadaGrid控件定义语法格式如下:<br>
  &lt;ASP:DataGrid Id=&quot;名称&quot; Runat=&quot;Server&quot; DataSource='数据源' 
  AllowPaging=&quot;True/False&quot; <br>
  AllowSorting=&quot;True/False&quot; AutoGenerateColumns=&quot;True/False&quot; 
  BackImageUrl=&quot;url&quot; CellPadding=&quot;像素值&quot; CellSpacing=&quot;像素值&quot; 
  DataKeyField=&quot;主键字段&quot; GridLines=&quot;None /Horizontal/Vertical/Both&quot; 
  HorizontalAlign=&quot;Center/Justify/Left/NotSet/Right&quot; <br>
  PageSize=&quot;记录数&quot; ShowFooter=&quot;True/False&quot; ShowHeader=&quot;True/False&quot; 
  <br>
  OnCancelCommand=“事件” OnDeleteCommand=“事件” OnEditCommand=“事件” OnItemCommand=“事件” 
  OnItemCreated=“事件” OnPageIndexChanged=“事件” OnSortCommand=“事件” OnUpdateCommand=“事件”&gt; 
  <br>
  &lt;AlternatingItemStyle … /&gt;&lt;EditItemStyle … /&gt;&lt;FooterStyle … /&gt; 
  &lt;HeaderStyle … /&gt;&lt;ItemStyle … /&gt;<br>
  &lt;PageeStyle … /&gt;&lt;SelectedItemStyle … /&gt; {各类列的定义} &lt;/ASP:DataGrid&gt; 
  DadaGrid控件定义由样式、属性和事件组成。 <br>
  &nbsp; 2. DataGrid控件的属性和事件 DataGrid控件的常用属性和事件分别列于下表中。</p>
<p align="center"><img src="image/slide7image023.PNG" width="590" height="358"><br>
  <img src="image/slide7image024.PNG" width="592" height="155"><br>
  <img src="image/slide7image025.PNG" width="593" height="289"></p>
<p>&nbsp;3. 分页显示 <br>
  &nbsp;&nbsp;&nbsp; DataGrid控件内建了自动分页功能 ,以下是MyDataGrid控件定义: <br>
  &lt;ASP:DataGrid Id=&quot;MyDataGrid Runat=&quot;Server&quot; AllowPaging=&quot;True&quot; 
  AutoGenerateColumns=&quot;True&quot; PageSize=&quot;10&quot; OnPageIndexChanged=&quot;PageChanged&quot;&gt; 
  &lt;/ASP:DataGrid&gt;<br>
  &nbsp;&nbsp;&nbsp; 在使用DataGrid控件的分页属性时,要注意不可使用DataReader绑定DataGrid,因为分 页需要把所有记录加载到内存。只有在DataGrid控件绑定到DataSet时才能分页。<br>
  &nbsp; 4. 指定列 <br>
  &nbsp; 1) BoundColumn列 BoundColumn列用于指定要显示的字段,字段内容为文本数据。BoundColumn列的主要属性列于下表中。</p>
<p align="center"><img src="image/slide7image026.PNG" width="627" height="238"><br>
  <img src="image/slide7image027.PNG" width="629" height="172"></p>
<p>&nbsp;&nbsp; 例如,下面的DataGrid控件定义使用BoundColumn显示students数据表的字段值: <br>
  &lt;ASP:DataGrid Id=&quot;stuDG&quot; Runat=&quot;Server&quot; AutoGenerateColumns=&quot;False&quot; 
  &gt; &lt;Columns&gt; <br>
  &lt;ASP:BoundColumn HeaderText='学号&quot; DataField=&quot;studentid&quot; /&gt; 
  <br>
  &lt;ASP:BoundColumn HeaderText='姓名&quot; DataField=&quot;name&quot; /&gt; <br>
  &lt;ASP:BoundColumn HeaderText='所在系&quot; DataField=&quot;department&quot; /&gt; 
  <br>
  &lt;ASP:BoundColumn HeaderText='总学分&quot; DataField=&quot;totalscore&quot; /&gt; 
  &lt;/Columns&gt; <br>
  &lt;/ASP:DataGrid&gt; <br>
  &nbsp; 2) ButtonColumn列 <br>
  ButtonColumn列即按钮列,它具有一些与BoundColumn列相同属性,此外还有下列属性:<br>
  ButtonTyle:获取或设置命令按钮类型。<br>
  CommandName:获取或设置按钮的命令名。<br>
  DataTextField:获取或设置按钮文字的来源字段<br>
  DataTextFormatString:获取或设置按钮文字的显示格式。<br>
  Text:获取或设置按钮上所显示的文字</p>
<p align="center"><a href="index.htm">回首页</a></p>
</body>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?