从DAO转换到ADO
2008-02-23 06:56:12来源:互联网 阅读 ()
Switch from DAO to ADO By Sam Huggill
Introduction
A few days ago, I started a new project that handles a large database containing HTML code for a complete Web site. The project has to allow the webmasters of the web site view all updates made to the site, when they were made and by whom. They can also edit the pages on the site, and automatically upload them.
This project requires the use of a fairly large database that needs to be Accessed by many people from different PCs. I decided to use SQL Server as the back end to the project, but this meant that I couldn注释:t use DAO to connect to it! What a pain!
So, I decided it was about time I started to learn ADO. I took a quick glance around on the net at my usual VB sites, but found little or no help for me on ADO.
Well, as we pride ourselves here at VB Square on adding original content, I decided I would write an article on using ADO.
This article is only really to get you started on ADO, and only discusses the connection and recordset objects. There are many more features of ADO that you will need to look into before you take on a project using ADO.
Connecting to local and external databases
With ADO, you can build all your code around a local database and then, very easily change one line of code that will allow you to access a database on a SQL Server.
The thing that took me a while to figure out, was how to connect to a database. With DAO, you use the OpenDatabase command passing the path of the database as one of the arguements. But with ADO, you need to build a connection string. To connect to a local database, use the following connection string:
ConnectionString = "Provider=Microsoft.JET.OLEDB.3.51;Data Source=c:\mydb.mdb"
That may seem a bit cumbersome, but this flexibility provides you with the means to connect to almost any database in any format anywhere. The following connection string is used to connect to a SQL Sever database named 注释:people注释::
ConnectionString = "driver=[SQL Server];uid=admin;server=myserver;database=people"
Switch from DAO to ADO
By Sam Huggill
Using the Connection Object
The Connection object is the base from which almost all ADO functions derive from. You can use this object to carry out most of the actions performed in the sample code, using SQL statements. E.g.
mCN.Execute "DELETE FROM People WHERE ID = 1"
I won注释:t go into any detail about using SQL statements, but the MSDN has some info on them.
The connection object returns a recordset object if you use the Execute mehtod. You can use this to create a DLL and use COM to get the contents of a recordset. e.g.
Public Sub GetRecordSet() As ADODB.Recordset
GetRecordSet = mCN.Execute("SELECT * FROM People")
End Sub
This means that you can centralize all you database code into one component, preferably a DLL.
Using the Recordset Object
In ADO, the Recordset object is very similar to the DAO Recordset object. This makes things a lot easier when porting your code, although you will need to devise a few workarounds to overcome a few missing features.
For example, when you insert a record, but need to store its ID (AutoNumber) value in the same action, you would normally use this code in DAO:
With rs
.AddNew
.Fields("Name").value = sNewValue
.Update
.Bookmark = .Lastmodified
m_intRcdID = .Fields("ID").value
.Close
End With
The ADO Recordset object does not expose a LastModified or LastUpdated property, so we need to use the following workaround:
With rs
.AddNew
.Fields("Name").value = sNewValue
.Update
.Requery
.MoveLast
m_intRcdID = .Fields("ID").value
.Close
End With
After updating the recordset (which you don注释:t need to do if you are moving to another record, as ADO automatically updates changes made when you move records) you need to refresh the recordset using the Requery method. Then you need to move to the last record, which is the one you have just added. Now, just extract the ID value and store it in a member variable.
Sample Application
To help you move from DAO to ADO, I have made a similar sample application as I did for the Beginning Databases article. The sample offers these features:
Adding new records
Deleting records
Updating records
Getting record data
It is a very simple demo, but should help you to understand the basics. It use the latest version of ADO, version 2.1. See the section at the bottom for downloading the ADO Libraries and the sample applcation.
To get the sample application to work, start a new Standard EXE Project and add a reference to the Microsoft ActiveX Data Objects 2.1 Library (Project, References). Add four command buttons (cmdAdd, cmdDelete, cmdGet, cmdSave) and three text boxes (txtNotes, txtURL, txtName). Copy/paste the following code into the form:
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:字符串中包含双引号
下一篇:VB 如何将DBgrid印出来
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
