Part III: Working with ADO.NET
CHAPTER 9 Introduction to ADO.NET
9.1 An Overview of ADO.NET
有多个NameSpace以及其中的类组成了ADO.NET的有用的类
System.Data.SqlClient,完成对SQL Server 7及以上版本的访问连接
System.Data.OleDb,完成OleDb Provider的数据库的访问
System.Data.Odbc,完成Odbc Provider数据库的访问
System.Data.Oracle,完成对Oracle数据库的访问
每一个NameSpace下面又有Connection、Command以及DataReader等类完成具体的工作
9.2 Performing Common Database Tasks
Opening a Database Connection
先创建一个Connection的实例,然后配置ConnectionString,使用Connection.Open()方法建立连接
Retrieving Records from a Database Table
在创建完Connection之后,创建Command实例,Command.Connection配置好,创建Command.CommandText,执行Command.ExecuteReader()方法,返回一个DataReader,使用DataReader.Read()方法循环DataReader获取所有数据
使用DataReader.HasRows属性,可以获取是否返回了记录
使用DataReader.ExecuteScalar()方法,获取单个记录
Using Parameters with Queries
使用Command.Parameters这个ParametersCollection类的Add方法,增加Parameter
Adding Records to a Database
使用SQL语句完成记录增加,需要设定Command的SQL语句,执行Command.ExecuteNonQuery()方法
Updating Database Records
使用SQL语句完成更新,需要设定Command的SQL聚于,执行Command.ExecuteNonQuery()方法
Deleting Database Records
使用SQL语句完成删除,需要设定Command的SQL聚于,执行Command.ExecuteNonQuery()方法
9.3 Improving Database Performance
Using SQL Stored Procedures
使用Stored Procedure可以提高一些性能,而且程序的结构也好于把SQL代码写在程序内部,至少可以不用修改程序,就可以完成某些变动—直接修改stored procedure就可以了
Retrieving Return Values and Output Parameters
通过增加Command的Parameter,并且指定Parameter的Direction为ReturnValue,可以获取Store Procedure的返回值
通过增加Command的Parameter,并且指定Parameter的Direction为Output,可以获取Store Procedure的Output类型的参数
Improving Performance with Connection Pooling
连接数据库是一个损耗时间和资源的动作,可以使用连接池来得到某些程度的缓解
只有使用了相同ConnectionString的连接才是会共享一个连接池的
通过在ConnectionString中加入某些属性字符串,可以配置连接池,具体如下:
Connection Lifetime,连接的生命长度,默认是无限长时间
Connection Reset,连接被放入连接池的时候是否需要被复位
Enlist,用于表明当前连接是否被自动加入Transaction事务上下文
Max Pool Size,连接池中保存的连接的最大数量
Min Pool Size,连接池中保存的连接的最小数量
Pooling,是否允许使用连接池
9.4 Advanced Database Topics
Executing Database Commands in a Transaction
事务Transaction保证了操作的原子性
Creating a Database Transaction
在SQL中使用Begin Transaction/Commit Transaction/Rollback Transaction,来完成事务的开始/结束/回滚
Creating an ADO.NET Transaction
CHAPTER 10 Binding Data to Web Controls
10.1 Overview of Data Binding
Data binding是一个处理过程,在运行时,动态的把数值赋值给控件的属性
使用<%#和%>标示了动态绑定表达式
通过使用Control.DataBind()方法,就将<%# %>中的动态绑定表达式进行计算,并赋值给Control的属性
必须注意的地方:Page也是一个Control的子类,所以Page上也有DataBind()方法,如果调用Page.DataBind()方法,那么Page上的所有Control都会进行一次数据绑定,即使在Page.DataBind()方法前已经有Control调用过DataBind()方法了,也要重新进行一次数据绑定,这就有可能引起一些程序结果上的变化
10.2 Binding a Server Control to a Data Source
Binding to the Repeater Control
Repeater控件如果没有绑定数据源,那么就不会显示任何信息。典型的做法是,使用Repeater控件显示DB中表的记录
可以使用DataReader获取数据,把DataReader赋值给Repeater.DataSource,最后调用Repeater.DataBind()方法完成数据绑定
Repeater也可以使用<%# Container.DataItem(string fieldName) %>来进行数据绑定
Using Templates
Repeater控件可以使用Template,在Template中放置其他Control设置是内联代码都是可以的,Template有五种类型,
HeaderTemplate,控制Repeater头部的格式
ItemTemplate,控制Repeater每一个Item的显示格式
AlternatingTemplate,控制交互的、替换的Item的格式
SeperatorTemplate,控制Repeater中Item之间的分隔符的显示格式
FooterTemplate,控制Repeater的尾部如何显示
View State and the Repeater Control
可以根据实际情况,通过设定Repeater.EnableViewState属性为false来禁止Repeater存储在ViewState中
Binding to the DropDownList Control
对DropDownList控件进行数据绑定和对Repeater进行数据绑定类似,有一个小小的不同是需要设置DropDownList.DataTextField属性,从而表明希望那个字段绑定到DropDownList上
如果希望DropDownList的每一个Item的Value和Text属性并不是一个值的时候,需要另外设定DropDownList.DataValueField属性
Binding to the RadioButtonList Control
RadioButtonList控件的数据绑定工作需要设定RadioButtongList.DataSource属性和RadioButtonList.DataTextField属性,然后调用RadioButtonList.DataBind()方法,就可以了
Binding to the CheckBoxList Control
CheckBoxList控件的数据绑定工作和RadioButtonList的数据绑定类似,设定CheckBoxList.DataSource和CheckBoxList.DataTextField属性,然后调用CheckBoxList.DataBind()方法就可以了
Binding to a ListBox Control
ListBox控件的数据绑定工作需要设定ListBox.DataSource属性和ListBox.DataTextField属性,然后调用ListBox.DataBind()方法就可以了
Binding to Other Controls
Binding to Image Controls
把一组Image控件绑定到数据源的方法是把Image控件放在Repeater控件中,通过绑定Repeater控件到数据源,就间接完成了Image控件到数据源的绑定
除了使用Image控件,还可以使用HTML语言的img标记来完成图片到数据的绑定
Binding to HyperLink Controls
和Image类似,通过把HyperLink控件放在Repeater控件的ItemTemplate中,也可以实现HyperLink到数据源的绑定
共5页: 上一页 1 [2] [3] [4] [5] 下一页