在VB组件中使用串缓冲 - 1

Lesson 1 : Overview
--------------------------------------------------------------------------------

This article serves as a quick "How To" example for using string buffering to concatenate strings in a VB
component. The example VB code will generate a HTML TABLE record that will be sent to an asp file after
being populated with data from a database table. The GetRows() method will be used to acquire the data
from an example Access database.

In many circumstances, sending a Variant array populated by GetRows(), (or a RecordSet) to an asp file
from a VB component is the recommended way to build a HTML TABLE record. Other times you may choose to
build a HTML TABLE record, or other database dependant code, within the VB component itself before sending
it to an asp file. This may not set well with the "true believers" in not mixing HTML and VB code. I tend
to be agnostic and I find sacrificing orthodoxy for speed is sometimes called for with server-side
components. In any regard, this programming choice is exemplified here.

Rather than sending the HTML code from the VB component with the Response.Write method, you can optionally
collect the HTML within a string and send it back as a complete HTML TABLE record. This avoids the need to
instantiate the asp object within the VB component. It also allows other VB code to use your component
without reference to asp. You"re just generating pure HTML code that any COM object can use. But when
putting together a large HTML string in VB, the concatenation operant "&" can slow your code down
significantly.

We"ll be creating two methods, MethodName() and DoBuffer(). The MethodName() method will acquire data from
a database and then use this data to construct a HTML TABLE record it will send to the calling asp file in
one lump sum. Our asp file will initially call the MethodName() method with an sql statement, a connection
string, and the number of fields used in our sql statement. In order to generate the HTML string with the
least use of the concatenation operant, the MethodName() method will send the string fragments it uses to
build the HTML TABLE record to the DoBuffer() method.

The supporting database file, text, and code, for this article can be downloaded here. If you"re
unfamiliar with writing a server-side VB dll, you can read some introductory articles at my site. This
example will include a VB server-side dll (ActiveX dll) consisting of one class (ClassName) and two
methods (MethodName and DoBuffer). The VB project is named How2Project4.

For How2 articles on sending data to asp files with Variant Array/GetRows() and RecordSets see:
How To Pass a Variant Array Populated with a RecordSet From a VB Component to an asp File
How To Pass a RecordSet From a VB Component to an asp File.

NOTE:
The connection string value will need to contain the Data Source Name (DSN) that should first be
established with ODBC, although any valid database connection string will work here. ADO will also need to
be referenced in the VB project (see an introductory VB component article on how to do this).

Why Use String Buffering?
If you concatenate strings using the "&" operant, and you repeat this operation multiple times, processing
time will be significantly delayed compared to using string buffering. Here"s example code of both
techniques:

Concatenating using the "&" operant:

StrHold = "Testing one, "
StrHold = StrHold & "two, "
StrHold = StrHold & "three."

Concatenating using a string buffering method:

S1, S2, S3, "Testing one, "
S1, S2, S3, "two, "
S1, S2, S3, "three."

[This code fragment uses different method and variable names from the code in this article]

With large amounts of concatenating, I have experienced increases in speed of up to 1000 times greater
when using string buffering. This is primarily due to how VB manages the memory for strings with the "&"
operant.

时间: 2024-10-29 16:29:02

在VB组件中使用串缓冲 - 1的相关文章

在VB组件中使用串缓冲 - 2

Lesson 2 : The VB Component -------------------------------------------------------------------------------- How2Project4.vbp The DoBuffer() MethodThe DoBuffer() method will be sent a string buffer (strBuffer), the value of the length of the string d

在VB组件中使用串缓冲 - 3

Lesson 3 : The ASP File -------------------------------------------------------------------------------- How2Project4.asp The Asp CodeWe can now use our component from any asp page. Here we set our connection string, the sql statement, and the number

应用-VB代码中com+组件安装问题

问题描述 VB代码中com+组件安装问题 '函数名称:AddComponent '作用:添加组件到com+对应应用中 '参数:sFolder COM+目录,sSourcePath源文件夹路径,sTargetPath目标文件夹路径,sFileName组件名 Public Function AddComponent(sComFolder As String, ByVal sTargetPath As String, sFileName As String) As Boolean Dim oo As

VB.NET中的组件开发

组件开发 先看段组件的代码:(临时写的,写得比较乱) ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '' 登录验证组件 '' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Imports System.Security.CryptographyImports System.TextImports System.DataImpo

VB.NET中的多线程开发

多线程 引言 对于使用VB6的开发者而言,要在程序中实现多线程(multi-thread)功能,一般就是使用Win32 API调用.但凡是进行过这种尝试的开发者都会感觉到实现过程非常困难,而且总是会发生些null terminated strings GPF的错误.可是有了VB.NET,一切烦恼都成为过去. 自由线程(free threaded) 在VB6中,我们只能对组件设置多线程模式,这通常就是单元模式的多线程.对于单元线程组件而言,组件中的每个可执行方法都将在一个和组件相联系的线程上运行.

VB.NET中声音的播放 Montaque(原作)

VB.NET中声音的播放    Montaque(原作)          由VB6升级为.NET后,有些人不清楚声音的处理,比如程序出错的时候,自定义一个声音播放,或者程序的背景音乐.包括游戏音乐等等.下面介绍几种在VB.NET中计较简单可以实现的方案: 1.  Beep 最简单的一种方法,通过计算机的扬声器发出声响, 声响的音高与持续时间取决于硬件和系统软件,从而随计算机不同而不同. Beepg跟Msgbox等方法位于Microsoft.VisualBasic.Interaction 中,一

在VB.NET中进行抓屏

'Author:wgscd '功能:抓屏 'QQ153964481 'Date:2005-4-12 '*********************************  Public Class Form1     Inherits System.Windows.Forms.Form #Region " Windows 窗体设计器生成的代码 "     Public Sub New()         MyBase.New()         '该调用是 Windows 窗体设计器所

把握VB.NET中的流(Stream)

stream 当你第一次用VB.NET读写文件的时候,你肯定会发现VB.NET摒弃了传统的文件I/O支持,感觉不习惯.其实,在.NET里面,微软用丰富的"流"对象取代了传统的文件操作,而"流",是一个在Unix里面经常使用的对象.我们可以把流当作一个通道,程序的的数据可以沿着这个通道"流"到各种数据存储机构(比如:文件,字符串,数组,或者其他形式的流等).为什么我们会摒弃用了那么久的IO操作,而代之为流呢?其中很重要的一个原因就是并不是所有的数据

VB.NET中声音的播放

由VB6升级为.NET后,有些人不清楚声音的处理,比如程序出错的时候,自定义一个声音播放,或者程序的背景音乐.包括游戏音乐等等.下面介绍几种在VB.NET中计较简单可以实现的方案: 1.  Beep 最简单的一种方法,通过计算机的扬声器发出声响, 声响的音高与持续时间取决于硬件和系统软件,从而随计算机不同而不同. Beepg跟Msgbox等方法位于Microsoft.VisualBasic.Interaction 中,一般默认系统会自动加载.调用很简单,看下面的例子. Dim I As Inte