外文文献 翻译及原文 (电子 电气 自动化 通信…) 56

上传人:r****d 文档编号:123637396 上传时间:2022-07-22 格式:DOC 页数:34 大小:93KB
收藏 版权申诉 举报 下载
外文文献 翻译及原文 (电子 电气 自动化 通信…) 56_第1页
第1页 / 共34页
外文文献 翻译及原文 (电子 电气 自动化 通信…) 56_第2页
第2页 / 共34页
外文文献 翻译及原文 (电子 电气 自动化 通信…) 56_第3页
第3页 / 共34页
资源描述:

《外文文献 翻译及原文 (电子 电气 自动化 通信…) 56》由会员分享,可在线阅读,更多相关《外文文献 翻译及原文 (电子 电气 自动化 通信…) 56(34页珍藏版)》请在装配图网上搜索。

1、ASP数据库基本原理Rich Smith2003第一部分:如果你学习过ASP,你可以做出相当酷的交互式网页。但怎样你都要利用ASP去共享各种数据库。许多应用都会因为大型数据库而受益,比如:论坛信息,购买清单,用户登录等。本系列文章的目的就是介绍ASP访问和连接数据库的基本概念。通过这些知识,你能利用ASP协议来创建动态数据站点。用ASP连接数据库的最基本方法就是使用ADO。ADO是Active X数据库专用对象集,是一个由微软编写的,用于编写Windows应用程序,去访问由微软或其他公司提供的相关的或不相关的数据库的交互接口。ADO能利用不同的Windows语言,包括VB和VC+。例如,你可以

2、用ADO去连接VB应用程序和Oracle数据库,或者,VC+应用程序和SQL Server数据库。我们的案例中,我们主要集中在ASP的使用。另个,在我们的例子中,我们会利用Microsoft Acces数据库作为后台的数据库。许多服务器提供商都提供ASP空间,但是大部分都会收取额外的费用而使用更好的数据库如SQL2000。但另一方面,一部分提供商使用MS Aceeess数据库,并只是收取很小的费用或根本不用收取任何费用。当使用ADO访问数据库,第一件事是先建立连接。对于数据库应用来说,有时候这会是一个难点。有两种方法去连接数据库,第一种是使用连接字完全限定数据库的名称,数据库驱动程序,位置。第

3、二种是为数据库创建一个与连接字相关的DSN(数据源名)比如,创建一个完全限制连接的例子如下:一旦一个DSN建立了,那么,数据库驱动,数据名,位置等信息就确定了。当要利用这些信息的时候,只需要使用DSN就可以了。例如:一个DSN叫做“MyDB”,连接信息将如下:ADO是一个程序交互对象。在外部组件中,意味着你会使用不同的对象去连接数据库。第一个使用的对象将会是连接对象。下面来看一个数据库连接对象的编码: 上面的例子中,创建了一个叫“conn”的作为ADO连接的对象。一量连接对象建立,就可尝试使用打开对象的方法去打开连接了。不得不说明的是,在对象创建方法注明一个“ADODB”的连接对象,事实上,如

4、此表达是行不通的,因为IIS不会知道这是什么对象和如何创建的。有两种方法可以让IIS认识到这些对象。第一种方法是在Global.asa文件中创建与ADO对象库相关的对象。如何做可以参见我的Global.asa的相关文章。另一种方法是,使用Adovbs.inc模型。它可以包括在任何ASP页面中的,来自IIS的一个包含文件,包括有在代码中使用ADO对象的适当声明。完成相关数据连接对象后,最重要的是正确地关闭和解除连接,否则有可能会因为内存被分配了数据而没有被释放而引潜在的数据溢出问题。跟着将是一个合理的关闭和解除ADO数据连接的方法: 可以看到,第一件事是关闭到数据库的虚拟连接,然后给对象一个“n

5、othing”的值,该供值可以解除虚拟连接并释放内存给操作系统。一旦建立了到数据库的连接,就可以向数据库的适当位置写入信息了,或者在网页上查找和读出已经存在的数据。下一章将会介绍Recordset对象,和利用该对象在数据库中读出信息。第二部分:第一部分,介绍了ADO相关知识,学习了用不同的方法去建立连接,如何管理连接对象。继续前面的方法,现在将学习利用Recordset对象在数据库中读出数据。什么是Recordset对象?一个ADO Recordset对象用于控制一组属于一个数据表或多个数据表的记录。类似于关系数据库,Recordset对象包括有记录和列,而且在ADO中是十分的重要和常用的。本

6、质上,一个Recordset对象是一个数据库的记录集合。当使用ADO读取数据的时候,就上使用Recordwet对象的过程。当创建一个对数据库的连接,创建了Recordset对象,指出你要检索的数据,你就可以选择一个表格或多个表格中的数据记录,或一个表格中的记录子集。为了更好地检索数据记录,你可以指定某些列。比如,有一个15栏的表格,但只需要在开始的两栏进行检索,那么,工作在系网络环境中,只返回两栏数据是十分的有效率的。建立一个Recordset对象的方法类似于连接对象的建立。下面是一个例子:n)conn.openConnectionStringsetrs=Server.CreateObject

7、(ADODB.recordset)%同数据库连接对象一样,使用完Recordset对象后,需要关闭和解散。看下面的例子:现在我们学会了如何建立和解除Recordset对象,下面学习如何提取数据。先从简单的开始。例如,有一个关于机器的数据库,DSN叫做“MyDB”。在该数据库中,建立一个叫“Customers”的表格。表格有五栏,分别为:“CustomerID”,“FirstName”,“LastName”,“PhoneNumber”,和“Status”。建立Recordset对象时,打开方法中的参数将决定你所要检索的数据。如果想要检索一个表格中的所有的记录,那么只要指定表格名称就足够了。下面的

8、例子会创建一个Recordset对象,用于检索Customers表格中的所有数据。在打开方式中指定表名称不是唯一指定你所要数据记录的方法,你可以使用SQL进行所要数据的精确检索。使用SQL,你能精确指定到要返回的具体的行和列。下面的例子使用SQL所实现的结果,与最近一个通过指定表名的一样:注意到两者的区别并不大,但如果对SQL的表述作小小变动,那么返回的数据将变化很大。例如:rs.open“select*fromCustomerswherestatus=Active”,conn注意到上一行在SQL语句中使用了“where”条件,将精确指定在ADO中,那些记录会被放入记录集。本文不会探究复杂的S

9、QL语句,同时假设你只是熟悉SQL最基本的东西。现在已经建立一个Recordset对象,并且打开了,但是我们怎么才知道找到了什么数据,并且找到多少数据呢?Recordset对象有很多属性,其中最有用的属性之一是Recordcount。通过检查Recordcount,就可以知道你是否查询并找到数据,例如:=0thenresponse.write“Thereare“&rs.recordcount&“rows”response.write“thatmatchyoursearch”elseresponse.write“Errorwithrecordset”endifrs.closesetrs=noth

10、ingconn.closesetconn=nothing%从上面的例子可以看出,返回一个-1表示出现了某些错误,并且在不打开数据记录集前通过检查Recordcount的属性触发。我们已经越来越看到轨迹的结束了。现在我们学会打开数据集并知道是否有数据在里面。但是我们怎么读取数据呢?字段数据可以通过名称查询。例如,下面是一段代码读取数据集的第一个和最后一个字段的数据:rite“Thecustomersnameis“&rs(“FirstName”)&_“&rs(“LastName”)rs.closesetrs=nothing同样也可以通过数据集的字段集对数据进行访问,这此字段集包括数据集中的所有栏。

11、请看下面例子:ldsResponse.Write(x.name)Response.Write(=)Response.Write(x.value&)nextrs.closesetrs=nothing这个例子会检索数据集中的所有栏并显示字段名称和值。是时候进入有趣的环节了。假设有一个包括复杂行的数据集,如何显示所有的数据呢?我们可以通过不同的方法移动数据指针,最重要的方法就是“MoveFirst”,“MoveLast”,“MoveNext”和“MovePrevious”,各自的意义就如其字面表述一样。但是移动记录时,你要知道什么时候停止,这主要是检查两个属性,分别为EOF(记录的结尾)和BOF(记

12、录的开始)。下面是一个例子:setrs=Server.CreateObject(ADODB.recordset)rs.open“select*fromCustomers”,conndountilrs.EOFresponse.write“Thecustomersnameis“&_rs(“FirstName”)&“&rs(“LastName”)rs.MoveNextlooprs.closesetrs=nothing记录集对象有很多方法和对象,你可以利用来做很多事情。但本文的目的在于基础知识,所以,要想学习如何转换记录集到数组,或者利用记录集对象移动记录指针到任何位置,建议学习有关方面更专业的详细资

13、料!我们已经学会创建数据连接,并从连接中读取数据。这足以解决大部分ASP应用程序的数据交互问题。下一部分,我们将会学习另一个非常重要的数据交互操作,就是添加数据到数据库。第三部分现在为止,我们已经学过在ASP中如何使用ADO,复习过如何创建数据连接,学习了如何利用记录集对象读取数据。本部分,将复习一下利用ADO添加信息到数据库的各种方法。利用ADO添加数据到数据库有两种通用的方法,第一种方法是记录集对象的外部组件。该方法非常容易理解,但有其缺陷性。另一种更加容易混淆的方法是直接利用连接对象。我们会探究不同的方法,并讨论每种方法的最适合使用时机。目前为止,已经学习了如何利用Recordset检索

14、数据,这是与添加数据是一样的工作原理。为了插入数据,数据库提供商应支持Recordset的可更新性。插入数据的关键词是记录集对象的“Addnew”方法,其使用毫无争议。它允许你创建一个空数据库,修改其内容并在数据库中更新。下面是一个例子:当使用记录集对象的“Addnew”方法创建新的记录时,有两个参数可用,分别为字段名称集和值集合。字段集参数可以是一个字段名称,也可以为一组字段名称或新记录字段的数据位置。无论是单独的名称或是多个名称,每个名称都必须在双引号里面。数据值参数是你想在字段中添加的一个或一组值,并且如果字段一个数组,那么数值集合也必须是一个数组,而且两者个数要相等并一一对应。如下例子

15、:以上例子使用了“Addnew”方法创建记录,而不是使用一组代码去执行添加记录到数据库。在文章的开头曾经提过使用连接对象进行添加记录。使用这个方法,你必须对SQL有所了解。事实上,可以使用适当的SQL语句去完成添加记录到记录集对象的任务,这是由连接对象的“execute”方法完成的。例如:该方法有一点复杂,因为它需要SQL方面的某些知识。以上所讲到的插入数据的方法都实现同样的功能,但是,应该使用哪个呢?这是取决于你的数据库知识和你的应用需要的。比如,当数据库在同一台机器上面,或你非常熟悉SQL,使用记录集对象属性去添加记录会更加有效。但是使用连接对象属性将会非常容易混淆,而且不胜任兼没有效率。

16、迄今为止,我们已相当熟练地利用ADO操作数据库中的数据,我们学习了创建连接,添加和检索数据。这些将满足你创建一个ASP应用程序的时候85%的数据库应用需要。下一章,也是本系列的最后一章,我们将学习如何维护数据库中的数据,包括更新和修改数据库中的数据,移除先前添加的数据。第四部分本系列的文章中,我们介绍了在ASP中利用ADO访问数据库。我们学习过如何建立一个ADO连接,使用这个连接去插入数据以及检索数据最后两个关于交互数据方面的问题是最重要的。如果仅仅是插入和查看数据,很多应用是没有办法完全实现的。没有数据维护方面的相关机制,程序的应用范围将非常之有限。现在我们已走完四分之三的路程并将最终达到终

17、点。我们还欠缺什么呢?最后两个关于交互数据方面的问题是最重要的。如果仅仅是插入和查看数据,很多应用是没有办法完全实现的。没有数据维护方面的相关机制,程序的应用范围将非常之有限。如果你的数据应用程序没有更新和修改数据的能力,其潜能将被限制。例如,一个客户表,你没有修改客户联系方式的能力,你就不能修改客户的通信地址, ,电子邮箱。那么,对于一个库存管理系统又如何更新库存中的每一件物品的价格和数量信息呢?目前主流数据库应用程序,数据维护都是十分必要的。最后,利用ADO更新信息的主要方法有两个,这些方法与我们在学习插入数据时讨论的方法非常相似,包括使用记录集对象和连接对象。如同插入记录到一个记录集一样

18、,更新记录也有一个特别的方法。“update”方法就是用于保存在记录集中,或连接对象中所作的修改的。下面有一个简单的例子:如同“addnew”方法一样,利用“update”同样也有额外的方法。类似“addnew”方法,有两个可选参数,分别为字段集和数值集,而且其使用方式与“addnew”方法是一样的。字段集参数可以是一个字段名称或一组字段名称,或一个新的记录中的数字的依次位置,并且必须在双引号内。数值集可以为一个或一组你想往字段中添加的数值,如果是一组字段集那么数值也必须是一组,而且字段集数量与数值数量相同并一一对应。参见下面的例子:ordset)rs.open“Customers”,conn

19、Method1rs.Update“FirstName”,“Rich”Method2rs.UpdateArray(FirstName,LastName),Array(Rich,”Smith)Method3myFieldList=Array(FirstName,LastName)myValueList=Array(Rich,Smith)rs.UpdatemyFieldList,myValueListrs.closesetrs=nothingconn.closesetconn=nothing%以上例子重温了在记录集中使用“update”方法进行信息的更新,这就如同“addnew”方法一样。有时作出了

20、记录的修改,但你想取消或不更新了。如果你对一个记录作出了修改,然后你移动你另一记录,那么,“update”方法将会自动地执行。幸运的是,还有另一个记录集方法叫做“canclequdate”,它会取消你对现记录的任何修改,但是,必须是你没有使用“update”方法,或移动到另一个记录之前。例如:reateobject(adodb.connection)conn.openConnectionStringsetrs=Server.CreateObject(ADODB.recordset)rs.open“Customers”,connrs(“FirstName”)=“Rich”ifUpdateReco

21、rds=“Y”thenrs.updateelsers.cancelupdateendifrs.closesetrs=nothingconn.closesetconn=nothing%正如在插入数据中所讨论的一样,连接对象的“execute”方法能执行一个正确结构的SQL语句。使用该方法,能够快速容易地更新数据而不需要建立记录集对象。例如:再次重申,该方法会比记录集对象稍令人畏惧,因为它需要最基本的SQL方面的知识。但这是最有效,最强大的操作数据的方法。最后,我们来到最后一个话题。删除或移除数据是我们所未接触过的最后的一个基本功能。同样,如同添加和修改数据,删除数据有两种基本的方法。记录集对象有

22、一个“delete”方法用于删除现在记录,该方法有一个可选参数,但为了简明,这里将不作探究。使用“delete”方法时,务必谨记,记录虽然被删除,但依然在记录集中存在,当尝试进行数据检索时将会发生错误。被删除的记录依然在记录集中存在,直到你移到另一个记录。例子:dset)rs.open“Customers”,connGothroughallrecordsdeleting“unused”customersdountilrs.EOFifrs(“Status”)=“InActive”thenrs.deleteendifrs.MoveNextlooprs.closesetrs=nothingconn.

23、closesetconn=nothing%当然,也可以利用了连接对象的“execute”方法或一点SQL语句进行记录删除,如下:如上所见,一个简单的语句将删除所有的记录。连接对象和记录集对象还有很多其它的属性和方法,他们将会使你能对数据库进行更多的操作和控制。我会提出一些关于ADO对象的额外阅读建议,以使你能更加受益。我希望该系列文章能给你开始利用ADO和ASP进行数据库应用所需要的知识。事实上,通过不同数据库管理代码的结合使用,你可以进行更深入的数据库应用。负责声明:文章中所提到的内容没有得到了开发者等的授权和保证。提供这些内容的目的是为了教育或娱乐,以给读者一些关键的想法,概念或商品回顾。

24、读者有义务以最优的安全或执行方法去购买正版。我们不对任何我们的文章所带来的负面的结果进行负责。如果这是硬件,将不会开放或修改你的硬件。ASP Database FundamentalsContributed by Rich Smith2003Part 1Ok. So youve learned ASP and you can make some pretty cool and interactive web pages. But sooner or later you are going to have a need to use ASP to access a database of som

25、e sorts. There are many applications that would benefit from database integration. Examples might include message forums, product listings, and user sign-in databases among other things.The purpose of these articles is to introduce you to the base concepts of accessing and utilizing a database with

26、ASP. With this knowledge, you should be able to begin creating dynamic data-driven websites using the ASP architecture.The default method to communicate with a database from within ASP is to use ADO. ADO stands for ActiveX Data Objects, and is an application program interface from Microsoft that let

27、s a programmer writing Windows applications get access to a relational or non-relational database from both Microsoft and other database providers. ADO can be utilized in many different development languages for Windows, including Visual Basic and Visual C+. As an example, you might using ADO to con

28、nect your Visual Basic application to an Oracle Database, or your Visual C+ application to SQL Server. In our case, we will focus on its use within ASP.In addition, in our examples we will utilize a Microsoft Access database as the back-end data repository. There are many web hosting providers avail

29、able that will host your ASP websites, but most of them charge extra for usage of a robust database like SQL2000. On the other hand, most of these providers offer the usage of MS Access databases at a very small fee, if any at all.When accessing a database using ADO, the first thing you need to do i

30、s make a connection. Creating a connection to a database can sometimes be the hardest part of building an ASP application. When you create a connection object, you can reference the database one of two ways. The first way is to fully qualify the database name, the driver, and location within the con

31、nection string for the database. The second is to create a DSN (Data-source name) for the database and refer to it by name in the connection string.For example, connecting to an access database using a fully qualified connection string may look like this:Being that information about database driver,

32、 name, and location are specified when a DSN is created in the system control panel, you only need to specify the DSN name when utilizing it. For example, to reference a DSN called MyDB, the connection information would look like this:ADO is an object-oriented programming interface. In laymans terms

33、, this means you will be working with different objects in order to communicate with the database. The first of these objects you will work with is the Connection Object.Lets take a look at a code sample that creates a database connection object:In the above example, you can see that we create an ob

34、ject called conn, which is defined as an ADO connection. Once we have an object for the connection, we attempt to open that connection using the open method of the object.One thing Id like to mention here is you will see that I am referencing the connection object of the adodb class in my createobje

35、ct method. By default that statement will fail, since IIS does not know that what object is or how to make it. There are two ways to make this information known to IIS. The first is to create an object reference to the ADO library inside your GLOBAL.ASA file. See my article on the GLOBAL.ASA to see

36、how this is done.Secondly, you can use the ADOVBS.INC module. This is an include file that comes with IIS, that you can include in any ASP page. It contains the appropriate declarations to be able to used the ADO objects within your code.When you are finished with the reference to the database conne

37、ction object, it is important to correctly close and destroy it. Not doing so can potentially cause issues on your web server, due to memory resources that have been allocated but not properly released.Follows is an acceptable method for closing and destroying a ADO connection object: As you can see

38、, the first thing we did was close the actual connection to the database. Next, we set the object to a value of nothing, which destroys the actual object reference and releases the memory back to the operating system.Once you have established your connection to the database, you are able to begin pu

39、tting the pieces in place to add information to the database, or to search and extract existing data for presentation on your website.In the next article in this series, we will go into recordset objects, and how you use them to pull information out of your database.Part 2 In part 1 of this series,

40、we had an introduction to ADO. We learned about the different methods of making a connection, and how to manage the connection object. Now, continuing along the path of enlightenment, we will learn how to extract data from a database using recordsets. What is a recordset? An ADO recordset object is

41、used to hold a set of records that belong to a database table or tables. Like any relational database, the recordset object consists of records and columns. In the world of ADO, this object is one of the most important and is the most commonly used.In essence, a recordset is a collection of records

42、from your database. If you are looking to read data using ADO, a recordset object is what you use. After making the proper connection to the database, you create a recordset object and specify the data you would like the retrieve. You can opt to retrieve all the records in a table, in multiple table

43、s, or even a small subset of records inside a table.In addition to controlling the records that are to be retrieved, you can also specify precisely which columns to return as well. For example, if your table has 15 columns, but you only want to see the first two columns, it is much more efficient to

44、 only bring back the 2 columns, especially when working over a network.A recordset object is created in the same manner as a connection object. Follows is an example:It is important to remember to close and destroy a recordset object when you are done with it, just as with a connection object. Here

45、is an example:Now that we know how to create and destroy a recordset object, lets learn how to fetch some data.Lets start simple. For our example, lets assume that we have a database on the machine, with a DSN called “MyDB”. In this database, we have a table named “Customers”. This table contains 5

46、columns, named “CustomerID”, “FirstName”, “LastName”, “PhoneNumber”, and “Status”.When creating a recordset, one of the parameters in the Open method determines the data that you wish to retrieve. If you want to return all of the records in a table, then you specify only the table name.The following

47、 example will create a recordset containing all of the records in the Customers table:Specifying a table name in your open method is not the only way to specify data for your recordset. You can also use SQL to determine the precise data you wish to retrieve. Using SQL, you can specify the exact rows

48、 and columns to be returned.Here is an example how to accomplish the same results with SQL, as we did in our last example by specifying a table name: You will notice that not a lot changed between the two examples. But if we make a small change to the SQL statement, the data returned can vary greatl

49、y. For example:rs.open“select*fromCustomerswherestatus=Active”,connYou will notice in the above line I added a WHERE condition to the SQL statement. This specifies to ADO exactly which records should be placed into the recordset. This article is not going to go into the intricacies of SQL, and will assume that you are familiar with at least the very basics of SQL.Well, now we have created a recordset obje

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!