手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Asp.Net编程>列表

Creating a Server Component with VB - Redesigned - Part 2

来源:互联网 作者:西部数码 时间:2008-04-10
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

ADO can be effectively used in many different ways to accomplish the same results when accessing database information. The code below is a template that will connect to the example database, using NotesDSN as an ODBC data source name, and then disconnect from the database without any fanfare.

Basic Database Code Template


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


    ''''///// DB Connectivity Variables
    Dim Rs As New ADODB.Recordset
    Dim objCmd As New ADODB.Command
    Dim objConn As New ADODB.Connection

    ''''///// OPEN DATABASE ///////////////////////////////////////////
    objConn.Open strDbConnectionString
    objConn.BeginTrans


    ''''--> DO DATABASE PROCESSING HERE (SELECT/UPDATE/DELETE/INSERT)


    ''''///// FINISH AND CLOSE UP DATABASE PROCESSES //////////////////
    objConn.CommitTrans
    objConn.Close


End Function

The database code first declares variables set to the Recordset, Command, and Connection objects of the ADODB class we referenced with VB previously.

    Dim Rs As New ADODB.Recordset
    Dim objCmd As New ADODB.Command
    Dim objConn As New ADODB.Connection 

Then the code opens a database connection using the data source name variable strDbConnectionString. Setting the strDbConnectionString method parameter from a calling ASP file can change this connection string variable, but it defaults to NotesDSN, which we''''ll use as our ODBC data source name for our example code.

objConn.Open strDbConnectionString

The next line of code isn''''t really necessary for our example program, but I included it to exemplify how to code for transactions. (See the article title "How to Utilize Database Transactions Within a VB Component From an asp File" at http://www.dougdean.com for more information on database transactions.)

objConn.BeginTrans

By using transactions, a method can treat a series of database processes (SQL statements) as a complete unit. If only one SQL statement in a series of SQL statements fails, then all the SQL statements within the same transaction are rolled back to the state the database was in before the transaction began. So if you''''re moving a record from one table to another table by adding it to the second table and then deleting it from the first table, you can be assured that one SQL statement won''''t take place without the other when they are within a transaction.

As I mentioned, our database sample code will not actually need this type of transaction commitment since all we''''re doing is reading from the tables. But if you do any editing or deleting, you''''ll find using the transaction code demonstrated here to be necessary at some point.

Once the database connection is opened, and we have indicated that we want a transaction to begin, we do some database processing (adding, deleting, or changing fields via SQL statements) and then finish up our database effort by telling ADO that we want all of our SQL statements processed as a single unit (objConn.CommitTrans) before closing the database connection (objConn.Close).


    ''''--> DO DATABASE PROCESSING HERE (SELECT/UPDATE/DELETE/INSERT)

    ''''///// FINISH AND CLOSE UP DATABASE PROCESSES //////////////////
    objConn.CommitTrans
    objConn.Close

Getting Note Text from the Database

To demonstrate how to retrieve one of the database records, we can add code to the template described above. This method code example will access the database and then send the note text of a record back to the ASP file code that called it. This method code assumes that a valid NoteID will be used as the first method parameter.

Adding Database Select 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

    ''''///// 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

    ''''///// Get NextText from database
    strNoteText = Rs.Fields("NoteText")
 
    ''''///// Close this Recordset
    Rs.Close


    ''''///// FINISH AND CLOSE UP DATABASE PROCESSES //////////////////
    objConn.CommitTrans
    objConn.Close
    Set Rs = Nothing
    Set objCmd = Nothing
    Set objConn = Nothing

    ''''///// SEND BACK NOTE TEXT FROM DATABASE
    ShowNotes = strNoteText

End Function

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!