1 / 20

第 10 章 数据库开发技术

第 10 章 数据库开发技术. 王德俊 上海交通大学继续教育学院. 第 10 章 数据库开发技术. 10.1 数据库系统与ADO.NET概述 10.2 SQL语言简介 10.3 ADO.NET对象. ADO.NET 体系结构:. 四个核心类对象 用于实现对数据库的数据处理 : (1) Connection 对象:实现数据连接 ,在使用过程中,根据不同的数据库的类型使用不同的 Connection 控件 (2) Command 对象: 用于 执行数据库的命令 操作,包括检索、插入、删除以及更新操作

rafael-mann
Download Presentation

第 10 章 数据库开发技术

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第10章 数据库开发技术 王德俊 上海交通大学继续教育学院

  2. 第10章 数据库开发技术 10.1 数据库系统与ADO.NET概述 10.2 SQL语言简介 10.3 ADO.NET对象

  3. ADO.NET体系结构:

  4. 四个核心类对象用于实现对数据库的数据处理:四个核心类对象用于实现对数据库的数据处理: (1) Connection对象:实现数据连接,在使用过程中,根据不同的数据库的类型使用不同的Connection控件 (2) Command对象:用于执行数据库的命令操作,包括检索、插入、删除以及更新操作 (3) DataAdapter对象:在DataSet对象和数据源之间架起了一座“桥梁”,来实现数据源和数据集之间的数据交换 (4) DataReader对象:DataReader对象用于从数据库中读取由SELECT命令返回的只读、只进的数据流,需要一直保持与数据库的连接,不提供非连接数据访问

  5. 10.3 ADO.NET对象 10.3.1 Connection对象 10.3.2 Command对象 10.3.3 DataReader对象 10.3.4 DataAdapter 对象 10.3.5 DataSet对象

  6. 10.3 ADO.NET对象 10.3.1 Connection对象 • Connection对象用于连接数据库,不同的数据库有不同的Connection对象: • SQL Server数据库——SqlConnection对象 • Access数据库——OleDbConnection对象

  7. 10.3 ADO.NET对象 10.3.1 Connection对象 • 连接到SQL Server数据库的连接方式有两种: • Windows身份验证登录,可以用类似以下连接字串: • string strConnection = "Data Source=localhost; InitialCatalog=MyDatabase; IntegratedSecurity=SSPI;"; • SQL身份(sa)验证登录,可以用类似以下连接字串:: • string strConnection = "DataSource=localhost; InitialCatalog=MyDatabase; Persist Security Info =True; User ID=sa; Password=123456"; • 创建连接到SQL Server数据库的Connection对象: • SqlConnection conn = new SqlConnection( strConnection );

  8. 10.3 ADO.NET对象 10.3.1 Connection对象 • Connection对象最常用的方法: • Open()方法:打开与数据库的连接 • Close()方法:关闭与数据库的连接 • Dispose()方法:释放由Connection占用的资源 • 如: • conn.Open(); • conn.Close(); • conn.Dispose(); • 属性DataSource:返回数据库服务器的信息; • 属性State.ToString():返回和数据库服务器的连接状态信息

  9. 10.3 ADO.NET对象 10.3.2 Command对象 • Command对象用于执行针对数据库的SQL命令。其常用属性有: • Connection属性,用于设置Command对象所依赖的连接对象,如: • SqlCommandcommand=newSqlCommand(); • command.Connection=conn; • CommandText属性,用于设置Command对象要执行的命令文本,如: • command.CommandText = "Select * From student";

  10. 10.3.2 Command对象 • CommandTimeOut属性 用于设置或返回终止执行命令之前需要等待的时间(单位为秒),默认为30。 • CommandType属性 用于决定CommandText属性值的格式: Text:表示CommandText属性值为SQL语句 StoredProcedure:表示CommandText属性值为存储过程 TableDirect:表示CommandText属性值为要读取的表。 • 例如: command.CommandType = CommandType.Text;

  11. 10.3.2 Command对象 • 常用方法包括: • (1)构造函数:有多个重载版本,以SqlCommand对象为例,其主要版本包括: public SqlCommand(); //用于创建一个SqlCommand对象,但没有做其他的初始化工作 public SqlCommand(string cmdText); //在创建对象的同时用参数cmdText定义命令文本初始化 public SqlCommand(string cmdText, SqlConnection connection); //在创建的同时用用参数cmdText定义命令文本和已有的SqlConnection对象初始化。

  12. 10.3.2 Command对象 • 例如,假设strSQL已经定义如下: • string strSQL = "INSERT INTO student VALUES('20102001','阎妮','女', 98)"; • 且conn是已经创建的SqlConnection对象,则下面三组语句是等价的: //第一组 SqlCommand command = new SqlCommand(); command.Connection = conn; command.CommandText = strSQL; //第二组 SqlCommand command = new SqlCommand(strSQL); command.Connection = conn; //第三组 SqlCommandcommand =newSqlCommand( strSQL, conn );

  13. 10.3.2 Command对象 • (2) ExecuteNonQuery()方法: • 该方法用于执行没有返回结果集的SQL语句,如建表语句、Insert、Update、Delete语句等。 • 命令的文本是在CommandText属性中设置的。其返回结果是执行命令后受到影响的行数。

  14. 10.3.2 Command对象 • 例如: stringConnectionString= "DataSource=localhost; InitialCatalog=MyDatabase; Persist Security Info =True; User ID=sa; Password=123456"; SqlConnection conn = new SqlConnection(ConnectionString); string strSQL = "INSERT INTO student VALUES('20102001','阎妮','女', 98)"; SqlCommand command = new SqlCommand(); command.Connection = conn; command.CommandText = strSQL; conn.Open(); int n = command.ExecuteNonQuery(); //执行SQL语句

  15. 10.3 ADO.NET对象 10.3.3 DataReader对象 • DataReader对象用于从数据库中读取由SELECT命令返回的只读、只进的数据流,在此过程中一直保持与数据库的连接。 • 优点:执行效率高,在体积和开销上它比数据集小,占用内存少 • 缺点:在读取数据时它与服务器的连接始终是打开的,它只能以单向向前的次序访问记录,所以仅用于数据检索等功能非常单一的设计中。

  16. 10.3.3 DataReader对象 • DataReader对象常用属性和方法: • FieldCount属性: 返回字段的数目。 • IsClosed属性: 返回DataReader对象是否关闭的状态,如果关闭则返回true,否则返回false。 • RecordsAffected属性: 返回执行insert、delete或update后受到影响的行数。

  17. 10.3.3 DataReader对象

  18. 例1,下面代码先利用Command对象执行Select语句,并将返回的结果集放到DataReader对象中,然后利用DataReader对象提供的属性和方法逐行、逐项提取结果集中的数据,并显示到ListBox对象中。 若要创建 SqlDataReader,必须调用 SqlCommand对象的 ExecuteReader方法,而不要直接使用构造函数。

  19. ……//连接数据库字串赋值 SqlConnection conn = new SqlConnection(ConnectionString); string strSQL = "SELECT * FROM student"; SqlCommand Command = newSqlCommand(strSQL, conn); conn.Open(); SqlDataReader reader = Command.ExecuteReader(); //结果集放到reader对象中 object[] row = new object[reader.FieldCount]; while(reader.Read()==true) { reader.GetValues(row); //获取结果集的当前行 for (int i = 0; i < reader.FieldCount; i++) { listBox1.Items.Add(row[i].ToString()); //将逐项输出行中的项 } listBox1.Items.Add("---------------"); }

  20. 本讲小结 Connection对象 Command对象 DataReader对象

More Related