本文首先阐述了基于.NET Framework平台的图书馆管理信息系统的开发背景以及其实践意义,其次说明了图书馆管理信息系统的功能以及相比同类软件的创新之处。然后就图书馆管理系统开发中所使用的一些的技术进行研究探讨。主要针对数据库的设计技术、存储过程技术、ADO.NET技术以及用SQL Server .NET Framework 数据提供程序访问SQLserver2000数据库技术四个方面进行了研究。最后还附上了图书馆管理信息系统的安装说明、简单使用说明和一些通用性的经典代码。笔者化了大量的时间用于程序设计,这也是笔者工作的重点,所开发的软件具有实用价值,所编写的代码对.NET程序员也有很好的参考性。在系统的开发过程中,笔者深刻体会到了.NET平台的强大性、优越性以及存储过程在提高数据访问性能和增强数据访问安全性上所起到的重要作用。此外,对于关系代数在数据库科学里的应用,笔者在编程过程中也做了一定的研究。
关键词: 数据库;.NET Framework;ADO.NET;存储过程;SQL Server .NET Framework 数据提供程序
ABSTRACT
The Management Information System of Library using .NET
This paper expatiate the Management Information System of Library using .NET, its background and the significance it has when application. Then, it presents the function of the Management Information System of Library and also discuss some technologies using when develop this system. Especially, it discuss the design technology of database, Stored Procedure, ADO.NET and control SQL Server 2000 using SQL Server .NET Framework Data Provider . In the end, it details the setup introduction and the user’s guide for the Management Information System of Library and also some classic code. The writer used a lot of time to design the program which is the keystone of all the works. The writer realized the importance of Stored Procedure for better performance and improving the security and also the advantage of .NET when programming. Moreover, the writer also investigates the applications of relational algebra in database science when programming.
Keywords: Database; NET Framework; ADO.NET; Stored Procedure; SQL Server .NET Framework Data Provider
摘要.........................................................
ABSTRACT.....................................................
1系统开发背景以及主要内容阐述..............................
2系统概述..................................................
2.1系统提供的功能.......................................
2.2系统一些新特点.......................................
3系统设计过程及原理阐述....................................
3.1数据库设计 ...........................................
3.1.1数据库设计重点.................................
3.1.2规范化设计.....................................
3.1.3数据库完整性设计...............................
3.1.4数据库的保护...................................
3.2使用存储过程..........................................
3.3.NET Framework平台概述...............................
3.4 ADO.NET技术探讨.....................................
3.4.1ADO.NET概述....................................
3.4.2ADO.NET访问数据的基本方法......................
3.4.3使用ADO.NET访问SQLserver2000 ................
4软件使用说明..............................................
4.1环境要求.............................................
4.2软件的安装...........................................
4.3常见操作.............................................
5程序源代码经典部分选录....................................
5.1存储过程.............................................
5.2winForms代码选录.....................................
5.3ASP.NET WebForms代码选录.............................
毕业设计成果.................................................
致谢.........................................................
参考文献.....................................................
附录一 英文翻译..............................................
附录二 英文原文.............................................. | 1 2 4 5 5 7 9 9 13 13 14 15 15 17 18 18 19 20 24 24 24 25 26 26 29 42 45 46 47 48 53 |
1 系统开发背景以及主要内容阐述
随着当今社会的迅猛发展,知识变的越来越重要,终身学习的观念已经深入人心。而图书馆――这一传统的获取知识的场所,它的地位也变的越来越重要。而图书馆的管理方法也更新了很多次。从最初的手工管理到现在的计算机管理等等。
笔者经过调查一些图书馆的现状,发现各图书馆使用的管理系统基本都是几年前的产品,大多使用VB+ADO方式,web平台也主要使用asp或php开发。开发技术比较陈旧,功能上也有一些不足。而且以前的VB面向对象化编程(OOP)支持的不是很好,软件维护起来也相当困难。
Microsoft.Net技术的推出,将计算带入一个新时代。现在.Net已经引起业界广泛的关注。.Net是专门为程序员设计的、功能强大的开发工具,利用该工具能够构建各类应用程序。.NET可以说是博大精深,蕴含者丰富的新技术新思想。
.Net使应用程序的功能更强大,使系统开发更简单,使系统的部署更轻松。而.NET包含的统一的类库,统一的数据结构使得各类编程模型更统一。
笔者个人对.NET技术非常感兴趣,在这方面也做过大量的研究。又鉴于当前流行的图书馆管理信息系统技术都比较陈旧,就萌发了用.NET开发一个新图书馆管理信息系统的想法。
很高兴它成为为我毕业设计研究的课题。
系统的实践意义有两个:1开发出的系统功能强大,可以应用于各大中型图书馆使用,可以根据客户的要求添加功能。2由于.NET推出的时间不算很长,所以网上很少有用.NET开发的公开源代码的大型系统。本系统可以给广大的.NET编程爱好者一些参考。
本文理论部分主要针对数据库设计技术、存储过程技术、ADO.NET技术以及用SQL Server .NET Framework 数据提供程序访问SQLserver2000数据库技术这四个方面进行了研究和探讨。
图书馆管理信息系统的每一行代码都是精雕细琢出来的,从里面体现出.NET的强大性以及优越性。笔者在编程过程中查阅了大量的书籍,参考了大量MSDN以及各大网站的技术文章,全部代码约有一万两千行左右。笔者为此做了大量的工作。
在系统的开发过程中,笔者深刻体会到了.NET平台的强大性以及优越性以及存储过程在提高数据访问性能和增强数据访问安全性上所起到的重要作用。
2
系统概述
图书馆管理系统基于Microsoft公司最新的.NET Framework平台构建,编码的基本语言是VB.NET。开发过程中统一使用.NET框架的数据类型、方法,抛弃了原来vb6的内容。充分体现了.NET的优越性,使软件易安装部署、易维护。
系统使用ADO.NET+SQLserver2000模式,数据层基本都基于存储过程构建,使得速度更快、安全性更高。
2.1系统提供的功能:
针对图书馆管理员设计的部分:
管理部分是一个基于WinForms的windows窗体应用程序,主要为图书馆管理人员提供服务。截图如下:
管理部分提供以下功能:
1.用户功能。(提供登陆验证、用户切换,管理员锁定系统等功能。)
2.事务处理
图书证挂失管理。(可以针对读者情况对图书证进行挂失,以防冒借。)
借书、还书管理。(实现日常图书借阅及归还。)
3.数据管理
图书、读者管理。(实现图书、读者的添加,删除,编辑。)
4.综合查询统计。(可以根据不同条件的组合检索图书或读者以及借阅数据,对检索出来的数据可提供多种方式的操作:导出为Excel格式、pdf格式、或导出为水晶报表以便打印等。)
5.系统设定。(设置一些图书馆基本规则,如读者类别、图书类别、读者借阅最大天数等,此外设定数据服务器信息也在这里。)
6.帮助功能。(提供一个chm帮助文档,帮助图书馆工作人员快速适应本系统,此外还提供在线问题反馈,软件更新提示等功能。)
针对读者设计的部分:
由于读者的多元性和位置不确定性,该部分采取基于B/S的方式实现。代码使用ASP.NET编写,利用ASP.NET的优点,可以自动适应多种客户浏览器。截图如下:
读者只需要有一台联网的计算机和一个常用WEB浏览器。就可以轻松的进行图书查询、更改个人信息、向图书馆反馈信息等。该部分主要提供以下功能:
1.登陆验证。(防止读者信息被盗用。)
2.图书查询。(一个综合的图书查询系统,可以方便的使读者查到需要的书籍。)
3.读者个人信息查询以及管理。(可以适当的更改一些读者个人的资料,如email等,此外在这里还可以查到读者当前的、所有的借阅记录等。)
4.信息服务。(定期公布超期的读者列表,以及新到的图书等信息。)
2.2系统创新部分以及一些特点
1)首先本软件是基于.net平台构建的,这是以前从未有过的。
2)软件很好的解决了一个关于时间冲突的问题。
时间冲突问题阐述如下:
图书馆管理信息系统是一个网络版的系统,其运作方式是有一台主数据服务器、还有若干个负责各项功能的客户端。举个例子:图书馆可能有若干台客户端电脑用于还书管理、还可能有若干台用于借书管理、还可能有若干台用于图书、读者信息维护等。因为每台电脑的系统时间可能会发生改变,这就会造成数据错误。比如张某于2月10日在负责借书的A号电脑上借了一本书,可能A的系统时间被误设定为2月5日。如果张某的最大借书时间为一个月,则他应该还书的时间就被误设定为3月5日。
笔者在开发过程中也参考了一些相关的图书馆软件,发现有的系统根本没有注意到这个问题。有的是在软件客户端设定一个时间校准的提示或者干脆就手工输入时间。这样无疑给操作人员增加了负担,而且校准还不一定正确。
在本系统中,所有的时间都是基于服务器时间的。即软件中必要的与时间有关的操作。都采用标准的T-SQL函数GETDATE()从数据服务器返回时间。这就避免了功能上的错误,也避免了给操作人员带来的不必要负担。
3)用ADO.NET的开放式并发进行并发控制
在图书馆系统的使用过程中,可能同时有多台客户机在操作同一个数据行。比如负责修改图书信息的A号电脑正在修改编号为XXX的书籍,而编号为B的电脑正试图删除编号为XXX的书籍。这样做肯定会有一些冲突,这就要讨论并发控制。
当多个用户试图同时修改数据时,需要建立控制机制来防止一个用户的修改对同时操作的其他用户所作的修改产生不利的影响。处理这种情况的系统叫做“并发控制”。
在以前的编程模型中,大多使用保守式并发控制,即在从获取记录直到记录在数据库中更新的这段时间内,该行对用户不可用。ADO.NET的数据集DataSet是一个断开式的模型,不适合保守式并发。在我所编写的图书馆管理信息系统中,使用了开放式并发控制(参见附录中我翻译的英文文章),即只有当实际更新数据时,该行才对其他用户不可用。更新将在数据库中检查该行并确定是否进行了任何更改。如果试图更新已更改的记录,则将导致并发冲突。
4)全部数据层都基于存储过程构建
使用存储过程,可以减少网络流量,提高程序的速度以及安全性。具体内容请参见笔者文章里对存储过程的研究讨论。
3系统设计过程及原理阐述
管理信息系统(以下简称MIS)服务于现代化的企事业管理,是提供企事业工作效率和经济效益的计算机网络系统。
管理信息系统涉及的因素非常多、非常复杂,开发起来具有一定的难度。如果掌握了一套行之有效的开发方法,以及把握好MIS开发中的重点,开发中就可以大量的解约人力、财力等。
我个人经过大量的研究、实践。认为在MIS开发中,首先数据库技术是MIS开发中的重要技术。数据库开发成败直接影响到整个MIS的开发。所以,构架一个优秀的MIS一定要有好的数据库设计。本文讨论的重点就是数据库设计理论。主要是两方面,一是基本的数据库理论;二是.NET平台上的ADO.NET数据访问技术。
其次,在MIS开发过程中,所使用的开发工具和应开发应用程序的种类也很重要。这里就不能不提到Microsoft公司的.NET Framework平台。.NET Framework 是一种新的计算平台,它简化了在高度分布式 Internet 环境中的应用程序开发。它提供一个一致的面向对象的编程环境、提供一个将软件部署和版本控制冲突最小化的代码执行环境等等非常优秀的特点。在以后介绍开发过程中将简单阐述。
再次,界面的设计也是MIS的一个重点。界面一定要具有易学性及易使用性。在当今流行的程序设计中,设计包含用户界面的应用程序时有两种选择:Windows 窗体和 Web 窗体。两者在开发环境中都具有完全的设计时支持,并且可以提供丰富的用户界面和高级应用程序功能以解决业务问题。要根据应用程序的具体功能来选择到底开发何种应用程序。我所开发的这个图书馆管理信息系统是这两者的结合。在管理功能方面,我使用了传统的WinForms技术。而在读者使用方面的功能模块,是用webForms开发的。
3.1 数据库设计
数据库技术是20世纪60年代后期产生何发展起来的一项计算机数据管理技术,现广泛运用与各种信息处理以及科学研究上。数据库技术已经成为当今时代非常重要的技术。
当今流行的数据库系统主要是以关系数据库为代表的第二代数据库系统。
当今流行的大型数据库管理系统主要有SQL Server、Oracle、Sybase等。
图书馆管理信息系统选用的是SQLserver2000数据库管理系统。
选择它的原因有:SQLserver2000功能强大,能完整实现数据库系统的所有功能。再者,我们选用的系统开发平台是微软公司的.Net Framework平台,它很好的支持了其公司的数据库产品SQLserver2000,而且为了加快数据处理速度,.NET Framework平台中还专门有一个SQL Server数据提供程序(System.Data.SqlClient)。
图书馆管理信息系统使用SQLserver2000数据库管理系统,最后设计的数据库表如下:
数据库名称lib2004
数据表有:
管理员表(manager) | |||
名称 | 类型 | 长度 | 说明 |
Man_id | nvarchar | 50 | 管理员账号(主键) |
Man_pwd | nvarchar | 12 | 管理员密码(not null) |
Man_purview | nvarchar | 10 | 管理员权限(not null) |
图书馆信息表(libInfo)存放图书馆信息,只有一条记录 | |||
字段名称 | 类型 | 长度 | 说明 |
No | Int | 4 | 等于1 |
Lib_name | nvarchar | 50 | 图书馆名称 |
Lib_org | nvarchar | 50 | 隶属单位 |
Lib_tel | nvarchar | 50 | 电话号码 |
Lib_adr | Nvarchar | 100 | 详细地址 |
图书表(book) | |||
字段名称 | 类型 | 长度 | 说明 |
Book_id | Int | 4 | 图书编号(自动增长) unique约束 |
Book_code | nvarchar | 50 | 条码号(主键) |
Book_name | nvarchar | 50 | 图书题名(not null) |
Book_pub | nvarchar | 50 | 出版社 |
Book_isbn | nvarchar | 50 | ISBN号 |
Book_pubdate | smalldatetime | 4 | 出版日期 |
Book_author | nvarchar | 50 | 图书作者 |
Book_page | Int | 4 | 图书页数 |
Book_price | money | 8 | 图书价格 |
Book_adddate | smalldatetime | 4 | 入馆日期 |
Book_place | nvarchar | 50 | 存放位置(外部键- place. Book_place) |
Book_sort | nvarchar | 50 | 图书分类(外部键- booksort. Book_sort) |
Book_remarks | nvarchar | 4000 | 备注 |
读者表(au) | |||
字段名称 | 字段类型 | 长度 | 说明 |
Au_serial | int | 4 | 读者序号(自动增长) unique约束 |
Au_id | nvarchar | 50 | 借书证号(主键) |
Au_name | nvarchar | 50 | 读者姓名 |
Au_sex | nvarchar | 2 | 读者性别(只能为‘男’或‘女’) |
Au_sort | nvarchar | 50 | 读者类别(外部键- ausort.Au_sort) |
Au_adddate | smalldatetime | 4 | 添加日期 |
Au_adr | nvarchar | 50 | 读者地址 |
Au_password | nvarchar | 12 | 读者密码(12位) |
Au_email | nvarchar | 50 | 读者email |
Au_remarks | nvarchar | 4000 | 读者详细资料 |
借阅情况表(borrow) | |||
字段名称 | 字段类型 | 长度 | 说明 |
Number | int | 4 | 编号(自动编号 主键) |
Au_id | nvarchar | 50 | 读者编号(外部键-au.Au_id) |
book_code | nvarchar | 50 | 图书编号(外部键-book.book_code) |
Borrow_date | smalldatetime | 4 | 借阅日期(not null) |
Should_date | smalldatetime | 4 | 应归还日期(not null) |
Return_date | smalldatetime | 4 | 归还日期 |
forfeit | money | 4 | 罚款金额 |
图书存放位置(place) | |||
字段名称 | 字段类型 | 字段长度 | 说明 |
Book_place | nvarchar | 50 | 存放位置(主键) |
Place_remarks | nvarchar | 4000 | 存放位置具体说明 |
图书分类(booksort) | |||
字段名称 | 字段类型 | 字段长度 | 说明 |
Book_sort | nvarchar | 50 | 图书类型(主键) |
Sort_remarks | nvarchar | 4000 | 分类具体说明 |
读者分类(ausort) | |||
字段名称 | 字段类型 | 长度 | 说明 |
Au_sort | nvarchar | 50 | 读者类型(主键) |
Au_borrowdays | Int | 4 | 最长借阅时间 |
Au_borrowbooks | Int | 4 | 最大借阅图书数 |
借书证挂失(aulost) | |||
字段名称 | 字段类型 | 字段长度 | 说明 |
Lost_id | nvarchar | 50 | 挂失的读者编号 主键 |
Lost_date | smalldatetime | 4 | 挂失时间 |
图书损坏赔偿 (booklost) | |||
字段名称 | 字段类型 | 字段长度 | 说明 |
Number | Int | 4 | 编号(自动增加,主键) |
Au_id | nvarchar | 50 | 丢失图书的读者编号 |
Book_name | nvarchar | 50 | 图书名称 |
Book_isbn | nvarchar | 50 | ISBN |
Lost_money | Money | 8 | 赔偿金额 |
图3-1
以下我们讨论数据库设计方法时均以图书馆管理信息系统数据库(lib2004)为例.
3.1.1数据库设计重点
(1) 数据规范化
(2) 数据完整性
(3) 数据库的保护
3.1.2规范化设计
良好的数据规范可以减少数据冗余和一些插入异常和删除异常。使关系模式尽量满足3NF。规范化设计的内容比较多,在这里就不一一阐述。请详细体会lib2004数据库中每个表的构建。如在这方面想有进一步研究的化,请参阅一些数据库原理与技术方面的书籍。
3.1.3数据库完整性设计
数据库完整性共分三类
(1) 实体完整性规则
实体完整性规则是指主关键字的任何部分不能为空值
比如在图书表(book)中
图书表(book) | |||
字段名称 | 类型 | 长度 | 说明 |
Book_id | Int | 4 | 图书编号(自动增长) unique约束 |
Book_code | nvarchar | 50 | 条码号(主键) |
Book_name | nvarchar | 50 | 图书题名(not null) |
主关键字不一定由一个字段组成,可以是多个字段的组合。
(2)引用完整性,又称参照完整性,主要是描述存在关系间引用时,不能引用不存在的元组.
图3-2
比如在图书分类表(booksort)与图书表(book)之间的关系(如图3-2),这是一个典型的一对多关系。关系的主键方是booksort表中的Book_sort(图书类别)字段,关系的外键方是book表中的Book_code(图书类别)字段。
其引用完整性就是book表中的每一本书的图书类别Book_sort或者为空值,或等于图书分类表(booksort)中某行的Book_sort值。
通俗的说法就是图书不能属于一个没有定义的类别。
(3)用户自定义完整性
这是一种比较有针对性的完整性,由具体环境决定。例如:在读者表(au)中,读者的性别字段(Au_sex)的值必须为‘男’或‘女’。这就是一条用户自定义完整性规则。
这类完整性在SQLserver2000中的实现就是对相关表建一个check约束即可。比如在au表中建立一个CK_au约束,其约束表达式为([Au_sex] = '男' or [Au_sex] = '女'),就实现了值必须为‘男’或‘女’的用户自定义完整规则。
3.1.4数据库的保护
数据库的保护主要内容是数据库的备份恢复、数据的事务性、以及如何进行并发控制。其中备份和恢复容易理解,这里主要阐述其它两项。
(1) 事务是一组操作序列.但是从用户的角度看来,事务是一个不可分割的操作序列。事务中的操作要么都做,要么都不做.事务不应该丢失,不应该被分割完成。
举个例子,从某个银行帐户向另一个帐户转入资金。这个过程分两步,首先将资金从一个帐户扣除,然后转入另一帐户。这个过程中,如果资金扣除以后由于一些因素导致失败。则资金就从一个帐户上扣除但没有转到另一个帐户上,就导致了两个用户都失去了这笔资金。如果将这两步组成一个事务,充分利用事务的共进共退性就可以避免这一点。
SQLserver2000事务语句主要包括BEGIN TRANSACTION(开始一个事务),COMMIT TRANSACTION(提交事务),ROLLBACK TRANSACTION(回滚事务)。
(2)数据库并发控制:在图书馆管理信息系统中,同时可能有多个客户端在进行数据操作。当多个用户试图同时修改数据时,需要建立控制机制来防止一个用户的修改对同时操作的其他用户所作的修改产生不利的影响。处理这种情况的系统叫做“并发控制”。处理并发的技术主要是封锁。ADO.NET数据结构基于断开的数据,使用开放式并发。
3.2 使用存储过程
在使用 SQL Server 2000 创建应用程序时,T-SQL 编程语言是应用程序和 SQL Server 数据库之间的主要编程接口。使用 T-SQL 程序时,可用两种方法存储和执行程序。可以在本地存储程序,并创建向 SQL Server 发送命令并处理结果的应用程序;也可以将程序在 SQL Server 中存储为存储过程,并创建执行存储过程并处理结果的应用程序。
SQL Server 中的存储过程与其它编程语言中的过程类似,原因是存储过程可以:
∙接受输入参数并以输出参数的形式将多个值返回至调用过程或批处理。
∙包含执行数据库操作(包括调用其它过程)的编程语句。
∙向调用过程或批处理返回状态值,以表明成功或失败(以及失败原因)。
使用 SQLServer 中的存储过程而不使用存储在客户计算机本地的 Transact-SQL 程序的优点有:
∙允许模块化程序设计。
只需创建过程一次并将其存储在数据库中,以后即可在程序中调用该过程任意次。存储过程可由在数据库编程方面有专长的人员创建,并可于程序源代码而单独修改。
∙允许更快执行。
如果某操作需要大量 T-SQL 代码或需重复执行,存储过程将比 T-SQL 批代码的执行要快。将在创建存储过程时对其进行分析和优化,并可在首次执行该过程后使用该过程的内存中版本。每次运行 T-SQL 语句时,都要从客户端重复发送,并且在SQL Server每次执行这些语句时,都要对其进行编译和优化。
∙减少网络流量。
一个需要数百行 T-SQL 代码的操作由一条执行过程代码的单独语句就可实现,而不需要在网络中发送数百行代码。
∙可作为安全机制使用。
即使对于没有直接执行存储过程中语句的权限的用户,也可授予他们执行该存储过程的权限。
此外在web编程方面,存储过程在安全问题上还可以防止动态SQL漏洞攻击,笔者经过一些调查,发现现在很多网站都有动态sql漏洞。只需要将密码字段的值设定为(' or '1'='1)即可使用该网站的管理功能。
我所开发的图书馆管理信息系统,无论在windows窗体编程和asp.net Web编程上都使用存储过程与数据库交互。
3.3 .NET Framework平台概述
图书馆管理信息系统的开发是基于.NET Framework平台的,所有在这里我们简单的介绍一下.NET Framework平台以及它的一些重要的技术
.NET Framework 是一种新的计算平台,它简化了在高度分布式 Internet 环境中的应用程序开发。.NET Framework 旨在实现下列目标:
∙提供一个一致的面向对象的编程环境,而无论对象代码是在本地存储和执行,还是在本地执行但在 Internet 上分布,或者是在远程执行的。
∙提供一个将软件部署和版本控制冲突最小化的代码执行环境。
∙提供一个保证代码(包括由未知的或不完全受信任的第三方创建的代码)安全执行的代码执行环境。
∙提供一个可消除脚本环境或解释环境的性能问题的代码执行环境。
∙使开发人员的经验在面对类型大不相同的应用程序(如基于 Windows 的应用程序和基于 Web 的应用程序)时保持一致。
∙按照工业标准生成所有通信,以确保基于 .NET Framework 的代码可与任何其他代码集成。
图3-3
.NET Framework 具有两个主要组件:公共语言运行库和 .NET Framework 类库。公共语言运行库是 .NET Framework 的基础。您可以将运行库看作一个在执行时管理代码的代理,它提供核心服务(如内存管理、线程管理和远程处理),而且还强制实施严格的类型安全以及可确保安全性和可靠性的其他形式的代码准确性。事实上,代码管理的概念是运行库的基本原则。以运行库为目标的代码称为托管代码,而不以运行库为目标的代码称为非托管代码。.NET Framework 的另一个主要组件是类库,它是一个综合性的面向对象的可重用类型集合,您可以使用它开发多种应用程序,这些应用程序包括传统的命令行或图形用户界面 (GUI) 应用程序,也包括基于 ASP.NET 所提供的最新创新的应用程序(如 Web 窗体和 XML Web services)。
.NET Framework还具有多编程语言,现在可提供Visual Basic .NET、Visual C# .NET、Visual C++ .NET、Visual J# .NET等。.NET Framework为几种语言提供了统一的平台和统一的编程模型。
ADO.NET 是一组向 .NET 程序员公开数据访问服务的类。ADO.NET 为创建分布式数据共享应用程序提供了一组丰富的组件。它提供了对关系数据、XML 和应用程序数据的访问,因此是 .NET Framework 中不可缺少的一部分。ADO.NET 支持多种开发需求,包括创建由应用程序、工具、语言或 Internet 浏览器使用的前端数据库客户端和中间层业务对象。
可使用 .NET Framework 开发下列类型的应用程序和服务:
∙控制台应用程序。
∙Windows GUI 应用程序(Windows 窗体)。
∙ASP.NET 应用程序。
∙XML Web services。
∙Windows 服务。
我的图书馆管理信息系统中包括Windows窗体和ASP.NET两种应用程序。
3.4 ADO.NET技术探讨
3.4.1 ADO.NET概述
ADO.NET 提供对 Microsoft SQL Server 等数据源以及通过 OLE DB 和 XML 公开的数据源的一致访问。数据共享使用者应用程序可以使用 ADO.NET 来连接到这些数据源,并检索、操作和更新数据。
ADO.NET 有效地从数据操作中将数据访问分解为多个可以单独使用或一前一后使用的不连续组件。ADO.NET 包含用于连接到数据库、执行命令和检索结果的 .NET Framework 数据提供程序。您可以直接处理检索到的结果,或将其放入 ADO.NET DataSet 对象,以便与来自多个源的数据或在层之间进行远程处理的数据组合在一起,以特殊方式向用户公开。ADO.NET DataSet 对象也可以于 .NET Framework 数据提供程序使用,以管理应用程序本地的数据。
3.4.2 ADO.NET访问数据的基本方法
ADO.NET 结构如下:
图3-4
从图3-4可以看出,ADO.NET提供了两种访问数据的基本方法:通过DataReader和DataSet。下面分别讨论这两种方法:
1)DataReader:
ADO.NET DataReader 从数据库中检索只读、只进的数据流。查询结果在查询执行时返回,在并存储在客户端的网络缓冲区中,直到您使用 DataReader 的 Read 方法对它们发出请求。使用 DataReader 可以提高应用程序的性能,因为一旦数据可用,DataReader 方法就立即检索该数据,而不是等待返回查询的全部结果;并且在默认情况下,该方法一次只在内存中存储一行,从而降低了系统开销。
DataReader 提供未缓冲的数据流,该数据流使过程逻辑可以有效地按顺序处理从数据源中返回的结果。由于数据不在内存中缓存,所以在检索大量数据时,DataReader 是一种适合的选择。
2)DataSet
ADO.NET DataSet 是数据的一种内存驻留表示形式,无论它包含的数据来自什么数据源,它都会提供一致的关系编程模型。一个 DataSet 表示整个数据集,其中包含对数据进行包含、排序和约束的表以及表间的关系。
如图3-4所示,一个DataSet可以包含多个DataTable,利用数据存储的信息对它们进行填充.可以在DataSet中包含DataRelation对象, DataRelation对象用来描述DataSet中表之间的关系。这个DataRelation对象的所包含的约束就是我们在3.14中所讲的数据库完整性设计的内容。
总之DataSet最大的特点就是:不依赖于连续的活动连接,其值是数据源的一个内存副本。
图3-5
图3-5描述了用DataSet操作SQLserver2000的一般方法。
用DataSet访问常规数据源的基本步骤是,先建立一个DataAdapter对象,如果只需只读访问数据,那么只要对DataAdapter指定一个SelectCommand即可。然后用DataAdapter填充DataSet。如果需要插入,更新或者删除数据,则需要在DataAdapter中相应的指定InsertCommand、UpdateCommand以及DeleteCommand,最后调用DataAdapter的Update方法。
3.4.3 使用ADO.NET操作SQLserver2000
我的图书馆管理信息系统使用的是SQLserver2000数据库。
在阐述了一些ADO.NET的基本内容后,我们来有针对性的讲一下如何通过ADO.NET访问SQLserver2000数据库。
最新的.NET Framework1.1 包括 SQL Server .NET Framework 数据提供程序(用于 Microsoft SQL Server 7.0 版或更高版本)、OLE DB .NET Framework 数据提供程序和 ODBC .NET Framework 数据提供程序以及Oracle .NET Framework 数据提供程序。分别可以建立四种不同连接,其主要功能如下。
(1) SqlConnection - 该对象管理与 SQL Server 7.0 版或更高版本的连接。通过忽略 OLE DB 层(还包括其他措施),对该对象进行优化以便将其用于 SQL Server 7.0 或更高版本。
(2) OleDbConnection - 该对象管理与可通过 OLE DB 访问的任何数据存储区的连接。
(3) OdbcConnection – 该对象管理与通过使用连接字符串或 ODBC 数据源名称 (DSN) 创建的数据源的连接。
(4) OracleConnection – 该对象管理与 Oracle 数据库的连接。
虽说SQL Server .NET Framework 数据提供程序、OLE DB .NET Framework 数据提供程序和 ODBC .NET Framework 数据提供程序都能访问SQLserver数据库,但微软公司推荐使用SQL Server .NET Framework 数据提供程序
图书馆管理信息系统使用的就是SQL Server .NET Framework 数据提供程序。以下通过一些例子介绍图书馆管理系统中是如何通过SQL Server .NET Framework 数据提供程序与SQLserver2000数据库进行交互的。
在.net中使用SQL Server .NET Framework 数据提供程序,首先要导入System.Data.SqlClient命名空间。我们举例说明ADO.NET与SQLserver2000数据库交互的两种常见方式:A.直接使用数据命令;B.使用数据集。
A. 使用数据命令与数据库交互
要在数据库中执行操作,应执行 SQL 语句或存储过程(它包括 SQL 语句)。您使用 SQL 语句或存储过程读写行并执行聚合函数,例如添加或求平均值。您还使用 SQL 语句或存储过程创建或修改表或列、执行事务等。
以下是一个图书馆管理系统中一个验证登陆的函数
Public Function validateLogin(ByVal uid As String, ByVal pwd As String) As Boolean
Dim IsCorrect As Boolean = False
Dim cnmy As New SqlConnection(“server=(local);database=lib2004;uid=sa;pwd=”)
Dim cmd As New SqlCommand
With cmd
.Connection = cnmy
.CommandType = CommandType.Text
.CommandText = "select count(*) from manager where Man_id=@Man_id and Man_pwd=@Man_pwd"
End With
Dim mMan_id As New SqlParameter("@Man_id", SqlDbType.NVarChar, 50)
Dim mMan_pwd As New SqlParameter("@Man_pwd", SqlDbType.NVarChar, 12)
With cmd.Parameters
.Add(mMan_id)
.Add(mMan_pwd)
End With
mMan_id.Value = uid
mMan_pwd.Value = pwd
Try
cnmy.Open()
If cmd.ExecuteScalar = 1 Then
IsCorrect = True
End If
Catch ex As Exception
MessageBox.Show("登陆验证时数据库发生错误" + vbCrLf + ex.Message, _
"error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Return False
Finally
cnmy.Close()
If Not cnmy Is Nothing Then
cnmy.Dispose()
End If
End Try
Return IsCorrect
End Function
现在对这个函数按步骤详细说明。
该例子体现了如何执行sql语句。
该实例用到了System.Data.SqlClient命名空间中两个重要的对象:1.连接对象SqlConnection;2.sql命令对象SqlCommand。
SqlConnection 对象表示与 SQL Server 数据源的一个唯一的会话。对于客户端/服务器数据库系统,它相当于到服务器的网络连接。
其重要属性如下表:
ConnectionString | 获取或设置用于打开 SQL Server 数据库的字符串。 |
使用sql验证方式:(server=服务器名称;database=数据库名称;uid=用户名;pwd=密码)
使用windows集成验证(server=服务器名称;database=数据库名称;Integrated Security=SSPI)
SqlCommand对象表示要对 SQL Server 数据库执行的一个 T-SQL 语句或存储过程。其重要属性如下:
CommandText | 获取或设置要对数据源执行的 Transact-SQL 语句或存储过程。 |
CommandTimeout | 获取或设置在终止执行命令的尝试并生成错误之前的等待时间。 |
CommandType | 获取或设置一个值,该值指示如何解释 CommandText 属性。 |
Connection | 获取或设置 SqlCommand 的此实例使用的 SqlConnection。 |
ExecuteReader | 执行返回行的命令。为了提高性能,ExecuteReader 使用 Transact-SQL sp_executesql 系统存储过程调用命令。因此,如果用于执行像 Transact-SQL SET 语句这样的命令,ExecuteReader 可能无法获得预期效果。 |
ExecuteNonQuery | 执行 T-SQL INSERT、DELELE、UPDATE 及 SET 语句等命令。 |
ExecuteScalar | 从数据库中检索单个值(例如一个聚合值)。 |
ExecuteXmlReader | 将 CommandText 发送到 Connection生成一个 XmlReader 对象。 |
(1)新建一个SqlConnection对象,设置其ConnectionString属性。
(2)新建一个SqlCommand对象,分别设置其CommandText、CommandTimeout、CommandType、Connection属性。
(3)如果要执行的T-SQL 语句或存储过程带有参数,则先要设定SqlCommand对象的Parameters属性。
(4)调用SqlConnection对象的open方法打开连接。
(5)执行SqlCommand命令。执行的方式及相应的返回结果见上表。
(6)调用SqlConnection对象的close方法关闭连接。
本例使用ExecuteScalar方法返回一个聚合函数count(*)的值,count(*)返回结果为所查询到记录的个数。如果用户名和密码均符合的话,则返回一条记录,此时Count(*)值为1,符合条件,就成功的通过了验证。
B.使用数据集
使用数据集DataSet与SQLserver数据库交互请参见3.3.2中关于DataSet的介绍。只是将通用的DataAdapter对象改为专用的SqlClient命名空间中的SqlDataAdapter对象即可,这里就不再重复。
4 软件使用说明
图书馆管理信息系统功能齐全,界面友好,高效的异常处理使程序不会突然中断。这里主要介绍系统的软硬件要求及安装方法。
4.1环境要求
方案 | 操作系统 |
数据服务器 | Win2000 Server Win2000 Advanced Server Windows Server 2003 系列(推荐) |
网页服务器 | Win2000 Pro Win2000 Server Win2000 Advanced Server WinXP Professional Windows Server 2003 系列(推荐) 注意:在所有这些系统上,还要求安装有IIS。此外win2000系列至少要更新到sp2 |
客户端 | Win9X系列版本 Win2K,XP,Win2003 |
数据服务器的安装
1.安装SQLserver2000
2.在SQLserver2000的查询分析器中,执行我们提供的脚本lib2004.sql
3.建立一个用于专门操作“图书馆管理信息系统”数据库的帐号
网页服务器的安装
1.确认已经安装了IIS
2.安装.Net Framework
3.安装MDAC 2.8
4.安装图书馆管理信息系统(WebForms)
5.然后在IIS对应的虚拟目录中设定sql连接字符串的值
客户端的安装
1.安装.Net Framework
2.安装MDAC 2.8
3.安装图书馆管理信息系统(管理员版)
4.第一次运行程序时会提醒设置数据服务器
数据服务器连接信息以及登陆信息请咨询管理员
4.3常见操作
登陆验证、借书、还书、综合查询等常见的图书馆事务,在软件中都有体现。详细的使用方法请参考程序的帮助文档,这里不再重复。
5 程序源代码经典部分选录
由于篇幅所,只能附上一些比较有代表性的代码。全部代码请到 http://cast.cumt.edu.cn/zhai/lib2004.rar下载。
5.1存储过程
--备份数据的存储过程
--将数据库备份到服务器C:\下,文件名为 当前日期+.zjx
ALTER PROCEDURE BACKUPmyData
AS
DECLARE @path varchar(15)
set @path=convert(varchar,getdate(),112)
set @path='C:\\'+@path+'.zjx'
BACKUP DATABASE lib2004
TO Disk = @path
WITH FORMAT,
NAME = 'Full Backup of lib2004'
RETURN
/*检索某图书是哪位读者借去的*/
ALTER PROCEDURE Book1IsBorrowByWho
@Book_code nvarchar(50)
AS
SELECT Au_id
FROM borrow
WHERE (book_code = @Book_code) AND (Return_date IS NULL)
RETURN
/*检索某图书是否在馆*/
/*检索在馆的图书 只要图书id不在 借阅记录没有归还的记录 里 就表示在馆*/
/*不在馆 则返回0
在馆 返回1 */
ALTER PROCEDURE Book1IsInLib
@Book_code nvarchar(50)
AS
SELECT COUNT(*) as IsInLib
FROM book
WHERE Book_code = @Book_code AND Book_code IN
(SELECT book_code
FROM Book
WHERE Book_code NOT IN
(SELECT borrow.Book_code
FROM borrow
WHERE Return_date IS NULL))
RETURN
/* 用于获取一位读者的
当前借阅书数,
总共借阅书数,
可借书数
最大借阅时间
输入参数Au_id */
ALTER PROCEDURE Get1AuInfo
@Au_id nvarchar(50), /*1*/
@BorrowNo int output,
@BorrowNoCurrent int output,
@MaxBorrowDays int output,
@MaxBorrowBooks int output
AS
begin transaction
SELECT @BorrowNo=COUNT(*)
FROM borrow
WHERE (Au_id = @Au_id)
SELECT @BorrowNoCurrent=COUNT(*)
FROM borrow
WHERE (Au_id = @Au_id) AND (Return_date IS NULL)
SELECT @MaxBorrowDays=ausort.Au_borrowdays ,
@MaxBorrowBooks=ausort.Au_borrowbooks
FROM ausort INNER JOIN
au ON ausort.Au_sort = au.Au_sort
WHERE (au.Au_id = @Au_id)
commit transaction
RETURN
/* 用于获取一位读者所有超期的借书列表 Au_id */
/*author:翟靖轩 */
ALTER PROCEDURE Select1AuMaxDateBookList
@Au_id nvarchar(50) /*1*/
AS
SELECT borrow.book_code AS 图书条码号, book.Book_name AS 图书题名,
borrow.Borrow_date AS 借阅日期, borrow.Should_date AS 应还日期
FROM book INNER JOIN
borrow ON book.Book_code = borrow.book_code
WHERE (borrow.Au_id = @Au_id) AND (borrow.Return_date IS NULL) AND
(borrow.Should_date < GETDATE())
ORDER BY borrow.Should_date DESC
RETURN
/*返回超期的所有读者*/
/*author:翟靖轩 2004-5-10*/
ALTER PROCEDURE SelectMaxDateAuList
AS
SELECT borrow.Au_id AS 借书证号, au.Au_name AS 读者姓名,
borrow.book_code AS 图书条码号, book.Book_name AS 图书名称,
borrow.Borrow_date AS 借书日期, borrow.Should_date AS 应归还日期
FROM borrow INNER JOIN
au ON borrow.Au_id = au.Au_id INNER JOIN
book ON borrow.book_code = book.Book_code
WHERE (borrow.Return_date IS NULL) AND (GETDATE() > borrow.Should_date)
ORDER BY borrow.Au_id
RETURN
/*查询一个读者的所有借阅记录 包括已经借过的已经归还的书籍
作者:翟靖轩
日期:2004-5-2 */
ALTER PROCEDURE SelectAuAllBorrowList
@Au_id nvarchar(50)
AS
SELECT book.Book_code AS 条码号, book.Book_name AS 图书题名,
borrow.Borrow_date AS 借阅日期, borrow.Return_date AS 归还日期,
borrow.Should_date AS 应归还日期, book.Book_isbn AS ISBN号,
book.Book_pub AS 出版社, book.Book_sort AS 图书分类,
book.Book_author AS 图书作者
FROM book LEFT OUTER JOIN
borrow ON book.Book_code = borrow.book_code
WHERE (borrow.Au_id = @Au_id)
ORDER BY borrow.Return_date, borrow.Borrow_date DESC
RETURN
/* 综合图书查询 */
-- 查询所有在馆图书
ALTER PROCEDURE searchBookInLib
@Book_code nvarchar(50)='',
@Book_name nvarchar(50)='',
@Book_pub nvarchar(50)='',
@Book_isbn nvarchar(50)='',
@Book_pubdate smalldatetime='1900-01-01',
@Book_author nvarchar(50)='',
@Book_sort nvarchar(50)=''
AS
SELECT Book_code AS 条形码, Book_name AS 图书名称, Book_author AS 图书作者,
Book_pub AS 出版社, Book_isbn AS ISBN号, Book_pubdate AS 出版日期,
Book_page AS 图书页数, Book_price AS 图书价格, Book_adddate AS 入馆日期,
Book_place AS 存放位置, Book_sort AS 图书分类
FROM book
WHERE (Book_code LIKE '%' + @Book_code + '%') AND
(Book_name LIKE '%' + @Book_name + '%') AND
(Book_pub LIKE '%' + @Book_pub + '%') AND
(Book_isbn LIKE '%' + @Book_isbn + '%') AND
(Book_author LIKE '%' + @Book_author + '%') AND
(Book_sort LIKE '%' + @Book_sort + '%') AND
(Book_pubdate > @Book_pubdate) and
Book_code not IN
(SELECT borrow.Book_code
FROM borrow
WHERE Return_date IS NULL)
RETURN
5.2 winForms代码选录
‘一个用于执行存储过程的类
Imports System.Data
Imports System.Data.SqlClient
Public Class CdataService
Protected Connection As SqlConnection
Private m_DSN As String
'构建一个带有参数的sqlcommand
Private Overloads Function CreateCommand(ByVal sprocName As String, ByVal parameters() As IDataParameter) As SqlCommand
Dim command As SqlCommand = New SqlCommand(sprocName, Connection)
command.CommandType = CommandType.StoredProcedure
Dim para As SqlParameter
For Each para In parameters
command.Parameters.Add(para)
Next
command.Parameters.Add(New SqlParameter("Return Value", _
SqlDbType.Int, _
4, _
System.Data.ParameterDirection.ReturnValue, _
False, _
0, _
0, _
String.Empty, _
DataRowVersion.Default, _
Nothing))
Return command
End Function
'构建一个没有参数的sqlcommand
Private Overloads Function CreateCommand(ByVal sprocName As String) As SqlCommand
Dim command As SqlCommand = New SqlCommand(sprocName, Connection)
command.CommandType = CommandType.StoredProcedure
command.Parameters.Add(New SqlParameter("Return Value", _
SqlDbType.Int, _
4, _
System.Data.ParameterDirection.ReturnValue, _
False, _
0, _
0, _
String.Empty, _
DataRowVersion.Default, _
Nothing))
Return command
End Function
Public ReadOnly Property DSN() As String
Get
Return m_DSN
End Get
End Property
Public Overloads Function RunSP(ByVal sprocName As String) As Int32
Dim result As Int32
Connection.Open()
Dim command As SqlCommand = Me.CreateCommand(sprocName)
command.ExecuteNonQuery()
result = CType(command.Parameters("Return Value").Value, Int32)
Me.Connection.Close()
Return result
End Function
Public Overloads Function RunSP(ByVal sprocName As String, ByVal parameters() As IDataParameter) As Int32
Dim result As Int32
Connection.Open()
Dim command As SqlCommand = Me.CreateCommand(sprocName, parameters)
command.ExecuteNonQuery()
result = CType(command.Parameters("Return Value").Value, Int32)
Me.Connection.Close()
Return result
End Function
Public Overloads Function RunSP(ByVal sprocName As String, _
ByVal parameters() As IDataParameter, ByVal DataSet As DataSet) As Int32
Dim result As Int32
Dim sqlDA As New SqlDataAdapter
Connection.Open()
sqlDA.SelectCommand = Me.CreateCommand(sprocName, parameters)
sqlDA.Fill(DataSet, "SourceTable")
result = CType(sqlDA.SelectCommand.Parameters("Return Value").Value, Int32)
Connection.Close()
Return result
End Function
Public Overloads Function RunSP(ByVal sprocName As String, _
ByRef DataSet1 As DataSet) As Int32
Dim result As Int32
Dim sqlDA As New SqlDataAdapter
Connection.Open()
sqlDA.SelectCommand = Me.CreateCommand(sprocName)
sqlDA.Fill(DataSet1)
result = CType(sqlDA.SelectCommand.Parameters("Return Value").Value, Int32)
Connection.Close()
Return result
End Function
Public Overloads Function RunSP(ByVal sprocName As String, _
ByVal parameters() As IDataParameter, ByRef DataSet As DataSet, ByVal TableName As String) As Int32
Dim result As Int32
Dim sqlDA As New SqlDataAdapter
Connection.Open()
sqlDA.SelectCommand = Me.CreateCommand(sprocName, parameters)
sqlDA.Fill(DataSet, TableName)
result = CType(sqlDA.SelectCommand.Parameters("Return Value").Value, Int32)
Connection.Close()
Return result
End Function
Public Function Construct(ByVal ConstructString As String)
m_DSN = ConstructString
End Function
Public Function Activate()
Connection = New SqlConnection(m_DSN)
End Function
Public Function Deactivate()
Connection = Nothing
End Function
Public Sub New()
End Sub
Public Sub New(ByVal ConstrucString As String)
Construct(ConstrucString)
Me.Activate()
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
Me.Deactivate()
End Sub
End Class
图书查询窗体的全部代码frmBookQuery.vb
Imports System.Data.SqlClient
Imports System.Drawing.Printing
Public Class frmBookQuery
Inherits library.frmBase
#Region " Windows 窗体设计器生成的代码 "
‘此处略去…
#End Region
Private Sub frmBookQuery_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With Me.dbgBookList
.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
.BackColor = System.Drawing.Color.Snow
.BackgroundColor = System.Drawing.SystemColors.ActiveCaptionText
.CaptionVisible = False
.HeaderForeColor = System.Drawing.SystemColors.ControlText
.ReadOnly = True
End With
BindComboBox1()
Me.cboBook_sort.Text = ""
End Sub
Function search()
Dim cn As New SqlClient.SqlConnection(cnstr)
Dim cmd As New SqlCommand
Dim da As SqlDataAdapter
Dim ds As New DataSet
With cmd
If Me.radAllBook.Checked = True Then
.CommandText = "searchBook"
Else
.CommandText = "searchBookInlib"
End If
.CommandType = CommandType.StoredProcedure
.Connection = cn
End With
Dim mBook_code As New SqlParameter("@Book_code", SqlDbType.NVarChar, 50) '1
Dim mBook_name As New SqlParameter("@Book_name", SqlDbType.NVarChar, 50) '2
Dim mBook_pub As New SqlParameter("@Book_pub", SqlDbType.NVarChar, 50) '3
Dim mBook_isbn As New SqlParameter("@Book_isbn", SqlDbType.NVarChar, 50) '4
Dim mBook_pubdate As New SqlParameter("@Book_pubdate", SqlDbType.SmallDateTime, 4)
Dim mBook_author As New SqlParameter("@Book_author", SqlDbType.NVarChar, 50) '6
Dim mBook_sort As New SqlParameter("@Book_sort", SqlDbType.NVarChar, 50) '11
With cmd.Parameters
.Add(mBook_code)
.Add(mBook_name)
.Add(mBook_pub)
.Add(mBook_isbn)
.Add(mBook_pubdate)
.Add(mBook_author)
.Add(mBook_sort)
End With
'赋值
mBook_code.Value = Me.txtBook_code.Text.Trim '1
mBook_name.Value = Me.txtBook_name.Text.Trim '2
mBook_pub.Value = Me.txtBook_pub.Text.Trim '3
mBook_isbn.Value = Me.txtBook_isbn.Text.Trim '4
mBook_pubdate.Value = Me.dtpBook_pubdate.Value '5
mBook_author.Value = Me.txtBook_author.Text.Trim '6
mBook_sort.Value = Me.cboBook_sort.Text.Trim '11
da = New SqlDataAdapter(cmd)
Try
da.Fill(ds)
ds.Tables(0).Columns(0).Caption = "图书条码号"
Me.dbgBookList.DataSource = ds.Tables(0)
Me.Label8.Text = "共检索到" + ds.Tables(0).Rows.Count.ToString + "条记录"
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cn.Close()
If Not cn Is Nothing Then
cn.Dispose()
End If
End Try
End Function
Function BindComboBox1()
Dim cnLib2004 As SqlConnection
Dim cmdbooksort As SqlCommand
Dim drBooksort As SqlDataReader
cnLib2004 = New SqlConnection(cnstr)
cmdbooksort = New SqlCommand
With cmdbooksort
.CommandType = CommandType.StoredProcedure
.Connection = cnLib2004
.CommandText = "selectBookSort"
End With
Try
cnLib2004.Open()
drBooksort = cmdbooksort.ExecuteReader
Dim myarr As New ArrayList
While drBooksort.Read
myarr.Add(drBooksort.Item(0))
End While
Me.cboBook_sort.DataSource = myarr
Catch ex As Exception
MessageBox.Show(ex.Message)
Me.Close()
Finally
cnLib2004.Close()
End Try
End Function
Private Sub ToolBar1_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar1.ButtonClick
If e.Button Is Me.tbtnQuery Then
search()
ElseIf e.Button Is Me.tbtnExit Then
Me.Close()
ElseIf e.Button Is Me.tbtnReset Then
frmClear()
ElseIf e.Button Is Me.tbtnExcel Then
excel()
ElseIf e.Button Is Me.tbtnPrint Then
print()
End If
End Sub
Private Sub frmClear()
Dim p1 As Control
Dim u As New Label
For Each p1 In Me.Panel2.Controls
If Not p1.GetType Is u.GetType Then
p1.Text = ""
End If
Next
Me.cboBook_sort.Text = ""
Me.radAllBook.Checked = True
Me.dtpBook_pubdate.Value = "1990-1-1"
Me.txtBook_code.Focus()
Me.dbgBookList.DataSource = Nothing
End Sub
Sub excel()
Dim tableAu As DataTable
tableAu = CType(Me.dbg.DataSource, DataTable)
If tableAu Is Nothing Then
MessageBox.Show("没有任何记录可以导出!", "error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
Dim myfrm As New frmShowProgress
myfrm.Show("正在建立excel对象...")
Try
Dim exl As New Microsoft.Office.Interop.Excel.ApplicationClass
Dim workbook As Microsoft.Office.Interop.Excel.Workbook
Dim sheet As Microsoft.Office.Interop.Excel.Worksheet
workbook = CType(exl.Workbooks.Add(), Microsoft.Office.Interop.Excel.Workbook)
sheet = CType(workbook.Sheets(1), Microsoft.Office.Interop.Excel.Worksheet)
Dim mycount, mycolumn As Int16
With tableAu
myfrm.setMsg("正在导出数据,请等待......")
For mycount = 0 To .Rows.Count - 1
For mycolumn = 0 To .Columns.Count - 1
With sheet
.Cells(mycount + 2, mycolumn + 1) = tableAu.Rows(mycount).Item(mycolumn)
End With
Next
'MsgBox(CType(mycount / .Rows.Count * 100, Int16))
myfrm.setProgress(CType(mycount / .Rows.Count * 100, Int16))
Next
myfrm.setMsg("正在设定excel表头格式...")
For mycolumn = 0 To .Columns.Count - 1
exl.Workbooks(1).Worksheets(1).cells(1, mycolumn + 1) = .Columns(mycolumn).ColumnName
exl.Workbooks(1).Worksheets(1).cells(1, mycolumn + 1).Font.Bold = True
Next
End With
myfrm.setProgress(100)
myfrm.Close()
'workbook.SaveAs(myfile)
sheet.Application.Visible = True
'exl.Quit()
Catch ex As Exception
MessageBox.Show("导出失败!", "fail", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
myfrm.Close()
End Try
End Sub
Private Sub print()
Dim tableAu As DataTable
tableAu = CType(Me.dbgBookList.DataSource, DataTable)
If tableAu Is Nothing Then
MessageBox.Show("没有任何记录可以导出!", "error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
Dim myfrm2 As New frmStatus
myfrm2.Show("正在导出报表...")
Dim myfrm As New frmShowReport
Dim rtd As New crtBook
rtd.SetDataSource(tableAu)
myfrm.Text = "报表输出窗口..."
myfrm.rpt.ReportSource = rtd
myfrm.rpt.DisplayGroupTree = False
myfrm2.Close()
myfrm.ShowDialog()
End Sub
End Class
登陆窗体代码frmLogin.vb
Public Class frmLogin
Inherits System.Windows.Forms.Form
Public IsLogSuccess As Boolean
Public strLogUser As String
#Region " Windows 窗体设计器生成的代码 "
此处略……
#End Region
Private Sub frmLogin_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
IsLogSuccess = False
End Sub
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
IsLogSuccess = validateLogin(Me.txtUser.Text.Trim, Me.txtPwd.Text.Trim)
If IsLogSuccess = True Then
strLogUser = Me.txtUser.Text.Trim
Me.Close()
Else
MessageBox.Show("密码错误!", "请重新登陆!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
Dim grbrush As Drawing2D.LinearGradientBrush
grbrush = New Drawing2D.LinearGradientBrush(New Point(0, 0), _
New Point(sender.Width, sender.Height), Color.DarkSlateGray, Color.White)
sender.CreateGraphics.FillRectangle(grbrush, New Rectangle(0, 0, sender.Width, sender.Height))
End Sub
End Class
将数据服务器信息序列化为xml文件的类
Imports System.Xml.Serialization
Imports System.IO
Imports System.Collections
Public Class CsetCnInfo
Structure CnInfo
Public server As String
Public database As String
Public uid As String
Public password As String
End Structure
Private mCC(4) As String
Public Function setCnTofile()
Dim mcn As CnInfo
mcn.server = mCC(0)
mcn.database = mCC(1)
mcn.uid = mCC(2)
mcn.password = mCC(3)
Dim xmlS As New XmlSerializer(GetType(CnInfo))
Dim txtwt As TextWriter = New StreamWriter("cninfo.xml")
xmlS.Serialize(txtwt, mCn)
txtwt.Close()
End Function
Public Function GetCnFromfile() As String
Dim mcn As CnInfo
Dim xmls As New XmlSerializer(GetType(CnInfo))
Dim txtwt As TextReader = New StreamReader("cninfo.xml")
Try
mcn = xmls.Deserialize(txtwt)
mCC(0) = mcn.server
mCC(1) = mcn.database
mCC(2) = mcn.uid
mCC(3) = mcn.password
Catch ex As Exception
Throw ex
Finally
txtwt.Close()
End Try
Return returnCnstr()
End Function
Public Function returnCnstr() As String
Return "server=" + mCC(0) + ";database=" _
+ mCC(1) + ";uid=" + mCC(2) + ";pwd=" + mCC(3)
End Function
Public Property cnstr() As String()
Get
Return mCC
End Get
Set(ByVal Value As String())
Value.CopyTo(mCC, 0)
End Set
End Property
Public Sub New()
End Sub
End Class
验证登陆的函数
Public Function validateLogin(ByVal uid As String, ByVal pwd As String) As Boolean
Dim IsCorrect As Boolean = False
Dim cnmy As New SqlConnection(cnstr)
Dim cmd As New SqlCommand
With cmd
.Connection = cnmy
.CommandType = CommandType.Text
.CommandText = "select count(*) from manager where Man_id=@Man_id and Man_pwd=@Man_pwd"
End With
Dim mMan_id As New SqlParameter("@Man_id", SqlDbType.NVarChar, 50)
Dim mMan_pwd As New SqlParameter("@Man_pwd", SqlDbType.NVarChar, 12)
With cmd.Parameters
.Add(mMan_id)
.Add(mMan_pwd)
End With
mMan_id.Value = uid
mMan_pwd.Value = pwd
Try
cnmy.Open()
If cmd.ExecuteScalar = 1 Then
IsCorrect = True
End If
Catch ex As Exception
MessageBox.Show("登陆验证时数据库发生错误" + vbCrLf + ex.Message, _
"error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Return False
Finally
cnmy.Close()
If Not cnmy Is Nothing Then
cnmy.Dispose()
End If
End Try
Return IsCorrect
End Function
5.3 ASP.NET WebForms代码选录
显示读者基本信息Userinfo.aspx的页面后置代码(CodeBehind)
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
If Session("UID") Is Nothing Then
Page.Response.Redirect("login.aspx")
End If
If Not IsPostBack Then
If Not Session("UID") Is Nothing Then
AuTextBind(Session("UID"))
setBorrowState(Session("UID"))
Else
Page.Response.Redirect("login.aspx")
End If
End If
End Sub
'显示某读者基本情况
Function AuTextBind(ByVal strAu_id As String)
Dim cnlib2004 As SqlConnection
Dim cmdAuBorrowList As SqlCommand
Dim drAuBorrowList As SqlDataReader
cnlib2004 = New SqlConnection(ConfigurationSettings.AppSettings("cncstr"))
cmdAuBorrowList = New SqlCommand
With cmdAuBorrowList
.CommandType = CommandType.StoredProcedure
.CommandText = "Select1Au"
.Connection = cnlib2004
End With
Dim mAu_id As New SqlParameter("@Au_id", SqlDbType.NVarChar, 50) '1
cmdAuBorrowList.Parameters.Add(mAu_id)
mAu_id.Value = strAu_id
Try
cnlib2004.Open()
drAuBorrowList = cmdAuBorrowList.ExecuteReader(CommandBehavior.SingleRow)
With drAuBorrowList
If .Read() Then
Me.spAuadddate.InnerHtml = CType(.Item("Au_adddate"), String)
Me.spAuadr.InnerHtml = .Item("Au_adr")
Me.spAuname.InnerHtml = .Item("Au_name")
Me.spAusex.InnerHtml = .Item("Au_sex")
Me.spAusort.InnerHtml = .Item("Au_sort")
Me.spAuid.InnerHtml = strAu_id
Me.spAuemail.InnerHtml = .Item("Au_email")
Me.spAuremarks.InnerHtml = .Item("Au_remarks")
Else
End If
End With
Catch ex As Exception
Response.Write(ex.Message)
Finally
cnlib2004.Close()
End Try
End Function
'显示读者借书状态 最大借书册书,当前借书册数 等
Function setBorrowState(ByVal strAu_id As String)
Dim cnlib2004 As SqlConnection
Dim cmd As SqlCommand
Dim dr As SqlDataReader
Dim CurBorrowNo As Int16
Dim AllborrowNo As Int16
Dim BorrowDays As Int16
Dim BorrowBooks As Int16
cnlib2004 = New SqlConnection(ConfigurationSettings.AppSettings("cncstr"))
cmd = New SqlCommand
With cmd
.CommandType = CommandType.StoredProcedure
.CommandText = "SelectAuCurrentBorrowNo"
.Connection = cnlib2004
End With
Dim mAu_id As New SqlParameter("@Au_id", SqlDbType.NVarChar, 50) '1
cmd.Parameters.Add(mAu_id)
mAu_id.Value = strAu_id
Try
cnlib2004.Open()
CurBorrowNo = CType(cmd.ExecuteScalar, Int16)
cmd.CommandText = "SelectAuAllBorrowNo"
AllborrowNo = CType(cmd.ExecuteScalar, Int16)
cmd.CommandText = "SelectAuSortInfo"
dr = cmd.ExecuteReader(CommandBehavior.SingleRow)
While dr.Read()
BorrowDays = dr.Item("Au_borrowdays")
BorrowBooks = dr.Item("Au_borrowbooks")
End While
Me.spTotalBorrowNo.InnerHtml = "共借书" + AllborrowNo.ToString + "册"
Me.spMaxBorrowDays.InnerHtml = BorrowDays.ToString + "天"
Me.spBorrowQinkuang.InnerHtml = "[" + CurBorrowNo.ToString + "/"
Me.spBorrowQinkuang.InnerHtml += BorrowBooks.ToString + "]"
Catch ex As Exception
Response.Redirect("error.aspx?errmsg=" + ex.Message)
Finally
cnlib2004.Close()
End Try
End Function
毕业设计成果
开发了一套基于.NET Framework平台的图书馆管理信息系统,系统可以正常投入使用,并且功能非常强大,性能十分良好。
此外在论文部分还讨论了大量的开发技术。
参 考 文 献
1.刘方鑫 数据库原理与技术 电子工业出版社,2002
2.Kevin Hoffman .NET Framework高级编程 清华大学出版社,2002
3.Jason Bell Windows Forms高级编程 清华大学出版社,2002
4.Tony Bain VB.NET 和SQLserver 2000高级编程 清华大学出版,2002
5.Evangelos Petroutsos Visual Basic.NET 从入门到精通 电子工业出版社,2002
6.Microsoft SQL Server 2000数据开发手册 科学出版社,2001
7.Marty Hall Web 编程指南 清华大学出版社,1999
8.Scott Worley ASP.NET 技术内幕 人民邮电出版社,2002
9.A.Russell Jones ASP.NET与C# 从入门到精通 电子工业出版社,2003
10.Steve Stein How Visual Studio .NET Generates SQL Statements for Concurrency Control MSDN for Visual Studio .NET 2003,2002
英文翻译
Visual Studio .NET如何为并发控制生成SQL语句
翻译:翟靖轩
原作者:Steve Stein
Visual Studio Team
Microsoft Corporation
2002年2月
摘要:这篇文章研究Visual Studio® .NET为不同的并发控制方式所产生的SQL语句,如何对它们进行修改可以提高执行效率,以及如何生成不带并发控制的SQL语句。
引言
阅读此文章时应具备的一些知识
生成的SQL语句在哪里
并发与数据适配器(DataAdapter)
优化生成的SQL语句
并发与CommandBuilder对象
结论
引言
任何可能同时被多个用户访问或修改数据的应用程序,都需要进行并发控制。否则,一个用户更改记录时可能不经意的覆盖了其他用户的更改。Visual Studio .NET的设计工具可以生成“保持所有值”方式的开放式并发SQL语句或生成“最后的更新生效”方式的SQL语句来更新数据。这篇文章将解释:
●不同的SQL语句是如何生成的
●如何修改自动生成的SQL语句可以提高执行效率
阅读此文章时应具备的一些知识
你需要具备以下知识:
●基本的ADO.NET概念,包括数据集(DataSet)以及数据适配器(DataAdapters)。更多信息请参见ADO.NET 数据访问介绍(Introduction to Data Access with ADO.NET)。
●数据并发机制以及会操作Visual Studio .NET。更多内容请参见 介绍 ADO.NET 中的数据并发(Introduction to Data Concurrency in ADO.NET)。
自动生成的SQL语句在哪里
自动生成的SQL语句在command对象的CommandText属性里。在设计阶段配置DataAdapter对象时或使用CommandBuilder对象时SQL命令被自动生成。更多信息,请参见 并发与CommandBuilder对象(Concurrency and Command Builder Objects)。
配置DataAdapter对象
●从工具箱的数据选项卡中拖一个DataAdapter对象
●从服务器资源管理器拖一个数据表
●选中已有的DataAdapter对象,然后单击在属性窗口底部的“配置数据适配器”链接
CommandBuilder对象
●CommandBuilder对象在运行时刻被创建,更多信息请参阅 SqlCommandBuilder 或 OleDbCommandBuilder。
并发控制与数据适配器(DataAdapter)
使用“数据适配器配置向导”配置数据适配器时,你可以选择是否使用开放式并发来生成Update和Delete语句。
一些思考和注意事项
●你的数据源必须有一个主键才能以开放式并发方式生成SQL语句
●当使用从“服务器资源管理器”拖放一个数据表的方式来创建DataAdapter对象时,DataAdapter对象自动生成基于开放式并发的Update和Delete语句。如果你不想使用开放式并发,右击DataAdapter对象,从快捷菜单中选择“配置数据适配器”,然后在“高级SQL生成选项”对话框中清除“使用开放式并发”选项的选定。向导则会重新创建不带并发检测的SQL语句。
●当重新配置现有的DataAdapter时,应注意“高级SQL生成选项”对话框里的选项已经全部恢复默认。例如最初配置DataAdapter时没有选定“使用开放式并发”选项,但是当重新配置DataAdapter时,“使用开放式并发”选项却会被选定,即便你根本没有打开过“高级SQL生成选项”对话框。
●如果你在“数据适配器配置向导”的“选择查询类型”页面选择“使用现有的存储过程”,则“使用开放式并发”选项将不可用。存储过程仍按其原来的方式执行。如果想使用并发检测的话,必须将其包括到存储过程中、或在你的应用程序中编写相应的代码。
●当使用开放式并发来创建SQL命令时,不会对二进制数据列验证进行并发处理。这将导致用这种方法对大的二进制记录集执行按位比较算法时的效率低下。
用向导生成SQL语句
为了理解Visual Studio .NET如何使用开放式并发来生成SQL语句,让我们来看看用“数据适配器配置向导”生成的Update语句。我们将查看同一条语句在选择“使用开放式并发”选项和不选择“使用开放式并发”选项时的不同状态。
你会注意到,选择开放式并发与不选择开放式并发所生成SQL语句的区别只存在于Where子句上。
注:以下的例子使用用“数据适配器配置向导”生成的Update语句,并从NorthWind示例数据库的Customers表中选择了若干列。
使用开放式并发的Update语句
这个例子使用了“数据适配器配置向导”的默认配置,即选中了“使用开放式并发”选项。
注: 当使用开放式并发时,生成的command的参数集里还存在一个参数副本。第二个参数集(带@Original_前缀的那个)保存了最初从数据源里读取的值。
检查Where子句发现,每一个生成的语句都要检测数据库当前的值是否等于最初读取的值(例,WHERE City = @Original_City)。通过数据库中的每个字段与最初读取的值相比较,我们很容易确定是否同时有其他用户修改了某个字段。如果Where子句不成立,就没有记录会被修改,与此同时还引发了一个“数据库并发”异常。如果数据源的某个字段为空值(NULL),生成的SQL语句同样验证最初读取的记录是否也为空值。
UPDATE Customers
SET CustomerID = @CustomerID, CompanyName = @CompanyName, ContactName
= @ContactName, ContactTitle = @ContactTitle, City = @City
WHERE (CustomerID = @Original_CustomerID) AND (City = @Original_City
OR @Original_City IS NULL AND City IS NULL) AND (CompanyName =
@Original_CompanyName) AND (ContactName = @Original_ContactName OR
@Original_ContactName IS NULL AND ContactName IS NULL) AND (ContactTitle =
@Original_ContactTitle OR @Original_ContactTitle IS NULL AND
ContactTitle IS NULL);
SELECT CustomerID, CompanyName, ContactName, ContactTitle, City
FROM Customers WHERE (CustomerID = @CustomerID)
不使用开放式并发的Update语句
这个例子更改了“数据适配器配置向导”的高级选项,没有选中“使用开放式并发”选项。
以下的语句表明:只要数据库中一条记录满足CustomerID = @Original_CustomerID ,则所有的字段都会被更新。不管这条记录现在是什么样的值,它都将被设置为通过SQL语句传递到数据源的值。在这里没有任何关于并发的检测,也无法得知是否同时有其它用户在更改这条记录。 这种方式称为“最后的更新生效”方式。无论以前对这条记录进行过什么样的修改,更新操作都会执行。
UPDATE Customers
SET CustomerID = @CustomerID, CompanyName = @CompanyName,
ContactName = @ContactName, ContactTitle = @ContactTitle, City = @City
WHERE (CustomerID = @Original_CustomerID);
SELECT CustomerID, CompanyName, ContactName, ContactTitle, City
FROM Customers WHERE (CustomerID = @CustomerID)
优化生成的SQL语句
Visual Studio .NET生成“保持所有值”方式的SQL语句来实现开放式并发。虽然这可能没有生成最高效的SQL语句,但是它的却生成了可以对数据源所有列(包括主键)进行并发检测的SQL语句。
使用“保持所有值”方式实现开放式并发,当执行效率非常低下时,你可以手工修改生成的SQL语句以使它们不检查数据源的所有列。最常见的方式是使用时间戳或版本号字段。如果你的数据源包含一个每次修改记录时都会更新的时间戳字段,你只需要验证数据源中的时间戳和你程序中的时间戳二者是否匹配,就可以知道是否同时有其他用户修改了记录。
下面这条SQL语句使用检查时间戳模式。
注: 这个例子假设数据库已经设置了时间戳字段
UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName,
ContactName = @ContactName, ContactTitle = @ContactTitle, City = @City
WHERE (CustomerID = @Original_CustomerID) AND
(TimeStamp = @Original_TimeStamp);
SELECT CustomerID, CompanyName, ContactName, ContactTitle, City,
TimeStamp FROM Customers WHERE (CustomerID = @CustomerID)
并发与CommandBuilder对象
当应用程序使用SqlCommandBuilder或者OleDbCommandBuilder时,生成的Update和Delete语句的CommandText属性被自动的以开放式并发方式创建。如果你不想用开放式并发,则可以通过修改DataAdapter对象的Update和Delete命令的CommandText属性来实现。更多内容,请参见OleDbCommand.CommandText属性 或 SqlCommand.CommandText属性。
结论
当使用开放式并发的“保存所有值”方法时,SQL语句在设计时由Visual Studio .NET的设计工具自动生成或在运行时由CommandBuilde自动生成。它把数据库所有字段(包括主键)当前值与初始值进行比较,这可能不是一种最高效的方式。如果你的数据使用版本号或者时间戳方式控制并发,则可以通过修改生成的SQL语句来获取更高效的执行。
英文原文
Visual Studio .NET Technical Articles |
Steve Stein
Visual Studio Team
Microsoft Corporation
February 2002
Summary: This paper examines the SQL statements Visual Studio® .NET generates for different kinds of concurrency control, how to modify them for better performance, and how to generate a statement that does not use concurrency control. (5 printed pages)
Contents
Introduction
Prerequisites
Where Are the SQL Statements?
Concurrency and Data Adapters
Optimizing the Generated SQL Statement
Concurrency and Command-Builder Objects
Conclusion
Introduction
Any application that might have multiple users simultaneously attempting to access and modify data needs some form of concurrency control. Otherwise, one user's changes could inadvertently overwrite modifications from other users. The design tools in Visual Studio .NET can create SQL statements that use the "check all values" approach to optimistic concurrency or the "last-in wins" approach to updating data. This paper will explain:
∙How each of these statement types are generated.
∙How to modify the generated SQL statement for better performance.
Prerequisites
You should have an understanding of:
∙Fundamental ADO.NET data concepts, including datasets and data adapters. For more information, see Introduction to Data Access with ADO.NET.
∙Concurrency control basics and the options available in Visual Studio .NET. For more information, see Introduction to Data Concurrency in ADO.NET.
Where Are the SQL Statements?
SQL statements are located in the CommandText property of command objects. SQL commands are automatically generated at design time when configuring data adapters, and at run time when using command builder objects. For more information, see Concurrency and Command Builder Objects.
Configuring Data Adapters
∙Drag a data adapter from the Data tab of the Toolbox
∙Drag a table from Server Explorer
∙Modifying an existing adapter, by selecting a data adapter and clicking the Configure Data Adapter link at the bottom of the Properties window.
Command Builder objects
∙Command builder objects are created programmatically at run time. For more information, see (SqlCommandBuilder or OleDbCommandBuilder)
Concurrency and Data Adapters
When configuring data adapters with the Data Adapter Configuration Wizard, you can decide whether to use optimistic concurrency for the generated Update and Delete statements.
Considerations and Caveats
∙Your data source must have a primary key in order for the SQL statements to be generated to use optimistic concurrency.
∙When creating data adapters by dragging tables from Server Explorer, the data adapter creates Update and Delete statements that are automatically configured for optimistic concurrency. If you do not want to use optimistic concurrency, you can reconfigure the data adapter: Right-click the adapter and select Configure Data Adapter from the shortcut menu, then clear the Use optimistic concurrency option of the Advanced SQL Generation Options Dialog Box. The wizard will recreate the statements without the additional code to check for concurrency violations.
∙When reconfiguring an existing data adapter, note that the advanced settings all revert to their default state. For example, if you cleared the Use optimistic concurrency option when the adapter was originally configured, it will automatically be selected if you reconfigure it, even if you do not access the Advanced SQL Generation Options dialog box.
∙If you select the Use existing stored procedures option in the Choose a Query Type section of the Data Adapter Configuration Wizard, the option to use optimistic concurrency is not available. The stored procedures will execute as is, and any desired concurrency checking must be done within the stored procedure, or programmatically built into your application.
∙When commands are generated to use optimistic concurrency, no verification will be performed on binary columns to determine whether concurrent changes have been made. The resources to perform a bit-by-bit comparison of a large binary record would be extremely inefficient.
SQL Statements Generated by the Wizard
To understand how Visual Studio .NET constructs SQL statements that use optimistic concurrency, let us inspect the Update statement generated by the Data Adapter Configuration Wizard. We will look at the same statement generated both with and without the Use optimistic concurrency option selected in the Advanced SQL Generation Options dialog box of the wizard.
You will notice the differences between statements that either use optimistic concurrency or not are located in the Where clause.
Note The following examples use the Update command that is generated by running the Data Adapter Configuration Wizard, and selecting several columns from the Customers table in the Northwind sample database.
Update Statement Using Optimistic Concurrency
This example uses the default settings of the Data Adapter Configuration Wizard, which has the Use optimistic concurrency option selected.
Note When using optimistic concurrency, the commands are generated with a second set of parameters. This second set of parameters (the ones with the @Original_ prefix) store the values that are initially read from the data source.
Examining the Where clause in the following statement reveals that all fields are inspected to make sure the current value for each field in the database is equal to the value that was originally read into the dataset (for example, WHERE City = @Original_City). By comparing each field in the database with the original value, it is easy to determine if a concurrent user has modified a field. If the Where clause is not satisfied, no records are updated and a DBConcurrencyException is raised. If a field in the data source contains a null value, the statement also verifies the original record contained a null value.
UPDATE Customers
SET CustomerID = @CustomerID, CompanyName = @CompanyName, ContactName
= @ContactName, ContactTitle = @ContactTitle, City = @City
WHERE (CustomerID = @Original_CustomerID) AND (City = @Original_City
OR @Original_City IS NULL AND City IS NULL) AND (CompanyName =
@Original_CompanyName) AND (ContactName = @Original_ContactName OR
@Original_ContactName IS NULL AND ContactName IS NULL) AND (ContactTitle =
@Original_ContactTitle OR @Original_ContactTitle IS NULL AND
ContactTitle IS NULL);
SELECT CustomerID, CompanyName, ContactName, ContactTitle, City
FROM Customers WHERE (CustomerID = @CustomerID)
Update Statement Without Optimistic Concurrency
This example modifies the advanced settings of the Data Adapter Configuration Wizard and clears the Use optimistic concurrency option.
Examining the following statement reveals that all fields will be updated as long as a record exists in the database where CustomerID = @Original_CustomerID. No matter what values exist in this record, they will all be set to the values passed through this statement. There is no verification to check if a concurrent user has modified the record. This is called the "last-in wins" approach, because no matter what modifications have been performed on the record, the update will still be performed.
UPDATE Customers
SET CustomerID = @CustomerID, CompanyName = @CompanyName,
ContactName = @ContactName, ContactTitle = @ContactTitle, City = @City
WHERE (CustomerID = @Original_CustomerID);
SELECT CustomerID, CompanyName, ContactName, ContactTitle, City
FROM Customers WHERE (CustomerID = @CustomerID)
Optimizing the Generated SQL Statement
Visual Studio .NET generates SQL statements that use the "check all values" approach to optimistic concurrency. Although this may not generate the most efficient statement, it does create a statement that can check for concurrency violations on any data source containing a primary key.
If the "check all values" approach to optimistic concurrency proves inefficient, you can modify the generated command text so it does not have to check every original value against the values in the data source. The most common way to accomplish this is with a timestamp or version field. If your data contains a timestamp field that is updated every time the data changes, you need only check the timestamp in your application's record against the timestamp in the data source to determine if a concurrent user has changed the record.
The following SQL statement has been modified to check the timestamp.
Note This example presumes the timestamp has been generated in the database.
UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName,
ContactName = @ContactName, ContactTitle = @ContactTitle, City = @City
WHERE (CustomerID = @Original_CustomerID) AND
(TimeStamp = @Original_TimeStamp);
SELECT CustomerID, CompanyName, ContactName, ContactTitle, City,
TimeStamp FROM Customers WHERE (CustomerID = @CustomerID)
Concurrency and Command-Builder Objects
If your application uses SqlCommandBuilder or OleDbCommandBuilder, the command text of the Update and Delete statements is automatically configured for optimistic concurrency. If you do not want to use optimistic concurrency, you can programmatically modify the CommandText property of the data adapter's Update and Delete commands. For more information, see OleDbCommand.CommandText property or SqlCommand.CommandText property.
Conclusion
The SQL statements that are automatically generated by the design tools in Visual Studio .NET or by command builder objects use the "check all values" method of optimistic concurrency. Although this may not be the most efficient approach for all situations, it generates a concurrency-checking statement on any data source that contains a primary key. If your data uses version numbers or timestamps, you can modify the generated SQL statements for better performance.
Send feedback on this topic to Microsoft
© Microsoft Corporation. All rights reserved.