扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
Create procedure "Employee Sales by Country" @Beginning_Date DateTime, @Ending_Date DateTime AS SELECT Employees.Country, Employees.LastName, Employees.FirstName, Orders.ShippedDate, Orders.OrderID, "Order Subtotals".Subtotal AS SaleAmount FROM Employees INNER JOIN (Orders INNER JOIN "Order Subtotals" ON Orders.OrderID = "Order Subtotals".OrderID) ON Employees.EmployeeID = Orders.EmployeeID WHERE Orders.ShippedDate Between @Beginning_Date And @Ending_Date GO |
DT10 = Format(Trim(DT1.Value), "yyyy-mm-dd") DT20 = Format(Trim(DT2.Value), "yyyy-mm-dd") |
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 |
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 |
* 本文使用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 '打开连接 |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。