4>、VB的程序代码实现动态调用的三种方法
①. 窗体变量的设置
Option Explicit Dim CNN1 As ADODB.Connection '连接 Dim RS As ADODB.Recordset '结果集 Dim StrCnn As String '连接字符串 Dim DT10, DT20 As Variant '日期变量 Dim i As Integer '字段的计数 |
②. [基本格式]存储过程的调用
Private Sub Command1_Click() '以[基本方式]调用(1)存储过程 On Error Resume Next Dim Cmd As ADODB.Command '命令 Dim Par As ADODB.Parameter '参数 '声明并初始化一个ADO 的Connection对象 DT10 = Format(Trim(DT1.Value), "yyyy-mm-dd") '起始日期赋值 DT20 = Format(Trim(DT2.Value), "yyyy-mm-dd") '截止日期赋值 Set Cmd = New ADODB.Command '指定cmd的当前连接CNN1 Cmd.ActiveConnection = CNN1 '指定该cmd 的当前活动连接 '设置要执行的是存储过程"[Employee Sales by Country] " Cmd.CommandType = adCmdStoredProc '表明cmd 为存储过程 Cmd.CommandText = "[Employee Sales by Country] " '调用存储过程名称 Set Par = Cmd.CreateParameter("Beginning_Date", adDBDate, adParamInput, , DT10) Cmd.Parameters.Append Par Set Par= Cmd.CreateParameter("Ending_Date", adDBDate, adParamInput, , DT20) Cmd.Parameters.Append Par Set RS = Cmd.Execute() '在Listview控件中显示RstByQuery记录集有效行 If DT10 < DT20 Then '调用网格的条件 If RS.Fields.Count > 0 Then '确认字段的列数 i = RS.Fields.Count Listrec RS, LV RS.Close End If End If End Sub |
③. [简捷格式]存储过程的调用
Private Sub Command2_Click() '以[简捷方式]调用(2)存储过程 On Error Resume Next DT10 = Format(Trim(DT1.Value), "yyyy-mm-dd") DT20 = Format(Trim(DT2.Value), "yyyy-mm-dd") Set RS = CNN1.Execute("[Employee Sales by Country] '" & DT10 & "' ,'" & DT20 & "'") If DT10 < DT20 Then '在Listview控件中显示RS记录集有效行 If RS.Fields.Count > 0 Then i = RS.Fields.Count Listrec RS, LV RS.Close End If End If End Sub |
④. [调用数据集的格式]执行存储过程
Private Sub Command3_Click() '以执行[结果集方式]调用(3)存储过程 On Error Resume Next Dim Sql1 As String DT10 = Format(Trim(DT1.Value), "yyyy-mm-dd") DT20 = Format(Trim(DT2.Value), "yyyy-mm-dd") Set RS = New Recordset '对字段进行中文提示的处理 Sql1 = "SELECT Employees.Country as '国别', Employees.LastName as '姓名', Employees.FirstName as '曾用名', Orders.ShippedDate as '日期' , Orders.OrderID as '序号', [Order Subtotals].Subtotal AS '销售合计' FROM Employees INNER JOIN (Orders INNER JOIN [Order Subtotals] ON Orders.OrderID = [Order Subtotals].OrderID) ON Employees.EmployeeID = Orders.EmployeeID WHERE Orders.ShippedDate Between '" & DT10 & "' AND '" & DT20 & "'" If Len(Sql1) > 0 Then RS.Open Sql1, CNN1, adOpenStatic, adLockOptimistic '在Listview控件中显示RstByQuery记录集有效行 If RS.Fields.Count > 0 Then i = RS.Fields.Count Listrec RS, LV RS.Close End If End If End Sub |
⑤. 将结果集装入到ListView控件中
Sub Listrec(ByRef RS As Recordset, ByRef LV As ListView) Dim head As ColumnHeader Dim Item As ListItem Dim K, P, Q As Integer K = 0 '初始化listview的某些属性 LV.ToolTipText = "" LV.View = lvwReport LV.GridLines = True LV.LabelEdit = lvwManual LV.ListItems.Clear LV.ColumnHeaders.Clear For i = 0 To RS.Fields.Count - 1 '由于item.text不接受null ,故预先于空串作连接 Set head = LV.ColumnHeaders.Add head.Text = RS.Fields(i).Name Next While Not RS.EOF Set Item = LV.ListItems.Add Item.Text = "" & RS.Fields(0).Value For i = 1 To RS.Fields.Count - 1 Item.SubItems(i) = "" & RS.Fields(i).Value Next K = K + 1 RS.MoveNext Wend Text1.Text = "" Text1.Text = CStr(K) LV.ToolTipText = "有效记录条数:" + CStr(K) End Sub |
⑥.工程/引用和连接SQL Server数据库
* 本文使用ADO,需要引用”Microsoft ActiveX data Objects 2.6 Library”对象。 * 连接SQL Server 2000数据库 Private Sub Form_Load() '在窗体的LOAD事件中加入如下代码: Set CNN1 = New ADODB.Connection '使用Connection对象的StrCnn属性,直接指定连接的SQL Server数据库 StrCnn="Provider=SQLOLEDB;DataSource=NO1;UserID=sa;pwd=;Initial Catalog=Northwind" CNN1.Open StrCnn '打开连接 |
结束语
现已将VB中实现动态调用含DateTime类型的存储过程介绍完毕,为您提供了解决调用存储过程又一途径,愿您在掌握SQL Server 2000数据库调用存储过程这一技术中,畅通无阻,马到成功。
本程序的环境是Win2000中文网络版;SQL Server 2000专业版;中文VB6.0。 
2/2 首页 上一页 1 2 |