Once the "sql" variable is assigned an SQL select statement using the sent lngNoteID, we indicate that the command we''''re sending the Command object is an SQL text statement with the adCmdText VB constant (objCmd.CommandType = adCmdText). We then set the Command object to the Connection object (Set objCmd.ActiveConnection = objConn) and open the recordset (Rs.Open objCmd) with the SQL statement set within the Command object. We''''re ready to combine the error code we explored previously with the database code we just developed. Again, this code example will demonstrate how to use transactional processes by "rolling back" all SQL statements when an error occurs in any one SQL statement. Of course, we''''re only using a single select SQL statement, but the process is the same for including multiple SQL statements that necessitate an "all or nothing" database commitment.
We''''ll also use the error processing to send a customized error message back to the calling code if our SQL statement happens to be invalid because an incorrect NoteID was sent as a method parameter. This is important because we really don''''t have any control over what NoteID value might be sent to our method code and we want to let the calling code deal with an erroneous NoteID in its own way - since it''''s the code that sent it.
文章整理:西部数码--专业提供域名注册、虚拟主机服务Now that the recordset is open, the NoteText field of the NoteTable is used within the Rs.Field("NoteText") method to retrieve the NoteText field value from our database record and assign it to our strNoteText variable.
''''///// GET SELECTED NOTE TEXT FROM DATABASE ////////////////////
sql = "SELECT NoteText FROM NoteTable WHERE NoteID = " & lngNoteID
objCmd.CommandText = sql
objCmd.CommandType = adCmdText
Set objCmd.ActiveConnection = objConn
Rs.Open objCmd
The remaining added code closes up the recordset (Rs.Close), sets the database objects to "Nothing" (Set Rs = Nothing, Set objCmd = Nothing, Set objConn = Nothing), and sends the note text value back to the calling code (ShowNotes = strNoteText).
''''///// Get NextText from database
strNoteText = Rs.Fields("NoteText")
Handling Database Errors
''''///// Close this Recordset
Rs.Close
''''///// FINISH AND CLOSE UP DATABASE PROCESSES //////////////////
...
Set Rs = Nothing
Set objCmd = Nothing
Set objConn = Nothing
''''///// SEND BACK NOTE TEXT FROM DATABASE
ShowNotes = strNoteText
Adding Error Handling to Database Code
Public Function ShowNotes(ByVal lngNoteID As Long, _
Optional ByVal intStyleID As Integer = 0, _
Optional ByVal strURL As String = "NoteExample.asp", _
Optional ByVal strDbConnectionString As String = "NotesDSN") As String
''''///// ERROR CODE //////////////////////////////////////////////
On Error GoTo ErrorCode
''''///// DB Connectivity Variables
Dim sql As String
Dim Rs As New ADODB.Recordset
Dim objCmd As New ADODB.Command
Dim objConn As New ADODB.Connection
''''///// DB Value Storage variables
Dim strNoteText As String
''''///// OPEN DATABASE ///////////////////////////////////////////
objConn.Open strDbConnectionString
objConn.BeginTrans
''''///// GET SELECTED NOTE TEXT FROM DATABASE ////////////////////
sql = "SELECT NoteText FROM NoteTable WHERE NoteID = " & lngNoteID
objCmd.CommandText = sql
objCmd.CommandType = adCmdText
Set objCmd.ActiveConnection = objConn
Rs.Open objCmd
''''///// Check if any data was returned
If Rs.EOF = True And Rs.BOF = True Then
''''///// No data returned, set error information, raise error
Err.Number = 40001
Err.Source = "NoteProject::NoteClass::ShowNotes"
Err.Description = "00000002<BR>Possible invalid lngNoteID entered as method value"
Err.Raise Err.Number, Err.Source, Err.Description
Else
strNoteText = Rs.Fields("NoteText")
End If
''''--> ANY OTHER DATABASE CODE GOES HERE
''''///// FINISH AND CLOSE UP DATABASE PROCESSES //////////////////
objConn.CommitTrans
objConn.Close
Set Rs = Nothing
Set objCmd = Nothing
Set objConn = Nothing
''''--> OTHER METHOD CODE GOES HERE
''''///// SEND BACK NOTE TEXT FROM DATABASE
ShowNotes = strNoteText
''''///// EXIT FUNCTION IF NO ERROR RAISED ////////////////////////
Exit Function
''''///// CODE IF ERROR OCCURS ////////////////////////////////////
ErrorCode:
''''///// ROLLBACK, CLOSE DB REFERENCES, AND SEND BACK RAISED ERROR
objConn.RollbackTrans
If IsObject(Rs) Then Set Rs = Nothing
If IsObject(objCmd) Then Set objCmd = Nothing
If IsObject(objConn) Then Set objConn = Nothing
Err.Raise Err.Number, Err.Source, Err.Description
End Function
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
Creating a Server Component with VB - Redesigned - Part 2
来源:互联网
作者:西部数码
时间:2008-04-10
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!
Added to the database template code are two string variables -- sql and NoteText. The former is used to hold a valid SQL statement that selects the NoteText from the NoteTable. The value that was sent as the first method parameter (lngNoteID) will be used in the SQL statement to select which note text the user wants to view. The value of the "sql" string variable could be assigned directly to objCmd.ComandText, but using a string variable can add flexibility, especially if you later decide to make the SQL statement a method parameter value. The latter string variable is used to hold the note text returned from the database.
热点关注
- 在asp.net中为Web用户控件
- .NET3.5和VS2008中的ASP.N
- 对.NET Framework 反射的
- Asp.net Ajax 1.0 异步回
- ASP.NET2.0的控件状态和视
- 通过避免下列10个常见ASP.
- 中国地区三级联动下拉菜单
- 解析Asp.net中资源本地化
- 一个无刷新效果定时自动更
- 动态创建MSSQL数据库表存
- asp.net用url重写URLReWri
- 使用ASP.NET Atlas编写显
- 关于ASP.NET调用JavaScrip
- 使用ASP.Net Forms模式实
- asp.net ajax 使用updatep
- 优化ASP.NET应用程序性能
- ASP.NET中动态修改web.con
- ASP.NET中利用Crystal Rep
- asp.net如何生成图片验证
- 基于ASP.NET的Web动态控件
- 分享个极好的无刷新二级联
- asp.net 2.0 上传控件的使
- 创建ASP.NET监视服务器进
- Asp.net把UTF-8编码转换为
- 开发基于ASP.NET WebServi
- asp.net结合html,javascr
- asp.net 2.0下嵌套masterp
- 正则表达式提取数字
- 最新版FreeTextBox(版本3.
- 获取本机上配置好的Oracle
- ASP.Net全局变量的设置和
- VB几个有用的函数
- 用代码画折线图/柱形图/
- FCKeditor2.2 ASP.NET2.0
- 客户端回调实现gridView无
IDC资讯
虚拟主机
域名注册
托管租用
vps主机
智能建站
网站运营 建站经验 策划盈利 搜索优化 网站推广 免费资源
网站联盟 联盟新闻 联盟介绍 联盟点评 网赚技巧
行业资讯 业界动态 搜索引擎 网络游戏 门户动态 电子商务 广告传媒
网络编程 Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术 Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷 Internet Explorer
网页制作 FrontPages Dreamweaver Javascript css photoshop fireworks Flash
程序设计 Java技术 C/C++ VB delphi
网络知识 网络协议 网络安全 网络管理 组网方案 Cisco技术
操作系统 Win2000 WinXP Win2003 Mac OS Linux FreeBSD
网站运营 建站经验 策划盈利 搜索优化 网站推广 免费资源
网站联盟 联盟新闻 联盟介绍 联盟点评 网赚技巧
行业资讯 业界动态 搜索引擎 网络游戏 门户动态 电子商务 广告传媒
网络编程 Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术 Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷 Internet Explorer
网页制作 FrontPages Dreamweaver Javascript css photoshop fireworks Flash
程序设计 Java技术 C/C++ VB delphi
网络知识 网络协议 网络安全 网络管理 组网方案 Cisco技术
操作系统 Win2000 WinXP Win2003 Mac OS Linux FreeBSD



