14 1 ADO NET Application Driver Manager ODBC
14. 1 数据库接口ADO. NET Application Driver Manager ODBC API Driver ODBC API Data Source ODBC的结构图
14. 1 数据库接口ADO. NET ASP. NET或其它 应用程序 数据提供程序 Data. Adapter 数据库 Connection Data. Set Web窗体 Command Windows窗体 Data. Reader 其他 ADO. NET的结构图
14. 2 连接数据源Connection对象 • Connection. String为可以设置的属性。针对特定的数据源,需 要给出特定的“连接字符串”,其中包含了数据库地址、名称、 用户名和密码等信息。下面代码给出两个典型的 Connection. String值: Data Source=192. 168. 1. 100; Initial Catalog=crm_db; Persist Security Info=True; User ID=crm_user; Password=123456; Connect Timeout=30 • 或者 Data Source=. ; Initial Catalog=Db. Examples; Integrated Security=True; Connect Timeout=30
14. 2 连接数据源Connection对象 • Db. Connection类常用方法 using System. Data. Sql. Client; …… //构建连接字符串 string connection. String = "Data Source=. ; Initial Catalog=Db. Examples; Integrated Security=True; Connect Timeout=30"; //实例化Sql. Connection对象 Sql. Connection connection = new Sql. Connection(connection. String); connection. Open(); 名称 �明 /* Open 根据�接字符串�接数据�。 关�数据��接。 这里添加代码完成数据的操作 Close Begin. Db. Transaction 开始一个数据�事�。 */ �建一个与当前�接相关�的 Db. Command connection. Close(); Create. Db. Command Get. Schema �象。 从数据源中�取架构信息。
14. 3 执行SQL命令Command对象 //实例化Sql. Connection对象,并规定了Sql. Connection对象的范围 using (Sql. Connection connection = new Sql. Connection(connection. String)) { Sql. Command command = new Sql. Command(query. String, connection); //实例化Sql. Command对象 try { //在异常处理try/catch块中打开数据连接并完成数据的操作 connection. Open(); int i = command. Execute. Non. Query(); Response. Write("执行完毕," + i + "条数据受到影响"); } catch (Exception ex) //当出现异常时要执行的语句 { Response. Write(ex. Message); } finally //无论如何都要执行的语句 { connection. Close(); } } //结束Sql. Connection对象的范围,并释放对象
DEMO Command增�改
14. 3 执行SQL命令Command对象 //构建SQL字符串 string query. String = "SELECT TOP 10 * FROM tb_客户 WHERE 简称='浩瀚'"; //实例化Sql. Command对象 Sql. Command command = new Sql. Command(query. String, connection); //实例化Sql. Data. Reader对象 Sql. Data. Reader reader = command. Execute. Reader(); while (reader. Read()) { Response. Write("简称: " + reader["简称"] + "; "); Response. Write("全称: " + reader["全称"] + "; "); Response. Write("<br/>"); } ……
DEMO Command ��
14. 4 读取数据Data. Reader • 使用Command对象执行SQL查询命令时已经给出了使用Data. Reader对象的方法,其中与 Data. Reader对象有关的核心代码如下: string query. String = "SELECT TOP 10 * FROM tb_客户 WHERE 简称='浩瀚'"; //构建SQL字符 串 ……//实例化 Sql. Command对象 Sql. Command command = new Sql. Command(query. String, connection); Sql. Data. Reader reader = command. Execute. Reader(); //实例化Sql. Data. Reader对象 while (reader. Read()) { Response. Write("简称: " + reader["简称"] + "; "); Response. Write("全称: " + reader["全称"] + "; "); Response. Write("<br/>"); } reader. Close();
14. 4 读取数据Data. Reader • 在Response对象输出字符串时,输出一个完整的<table>内容。 Response. Write("<table border='1'>"); //输出Table的起始标签 Response. Write("<tr><td>简称</td><td>全称</td></tr>"); //输出标题行 while (reader. Read()) { Response. Write("<tr><td>" + reader["简称"] + "</td><td>" + reader["全称"] + "</td></tr>"); //输出数据行 } Response. Write("</table>"); //输出Table的终结标签
14. 4 读取数据Data. Reader • 为了提高开发效率,ASP. NET提供了大量的Web服务器控件,其中在服务器端运 行的Table控件就可以很好地帮助构建HTML中的<table>。 while (reader. Read()) { Table. Row temp. Row = new Table. Row(); Table. Cell temp. Cell = new Table. Cell(); temp. Cell. Text = reader["简称"]. To. String(); temp. Row. Cells. Add(temp. Cell); temp. Cell = new Table. Cell(); temp. Cell. Text = reader["全称"]. To. String(); temp. Row. Cells. Add(temp. Cell); Table 1. Rows. Add(temp. Row); }
14. 4 读取数据Data. Reader <asp: Repeater ID="Repeater 1" runat="server"> <Header. Template> <table border="1" width="100%"> <tr> <th>简称</th> <th>全称</th> </tr> </Header. Template> <Item. Template> <tr> <td><%#Eval("简称")%></td> <td><%#Eval("全称")%></td> </tr> </Item. Template> <Footer. Template> </table> </Footer. Template> </asp: Repeater>
14. 4 读取数据Data. Reader • 在Data. Reader. Example. aspx中添加按钮“bt. Repeater”,并设置此按钮的点 击事件响应函数如下: protected void bt. Repeater_Click(object sender, Event. Args e) { …… //设置并绑定数据源 Repeater 1. Data. Source = reader; Repeater 1. Data. Bind(); …… }
DEMO Repeater
- Slides: 38