谢谢

解决方案 »

  1.   

    之前给别人写了个例子:
        Dim cnn As New ADODB.Connection
        cnn.ConnectionString = "PROVIDER=MSDASQL;DRIVER={SQL Server};SERVER=192.168.1.100;DATABASE=database1;UID=sa;PWD=123"
        If cnn.State <> ADODB.ObjectStateEnum.adStateClosed Then cnn.Close
        cnn.Open
        
        Dim Rs As ADODB.Recordset
        Set Rs = New ADODB.Recordset
        With Rs
            Set .ActiveConnection = cnn
            .CursorLocation = adUseClient
            .CursorType = adOpenKeyset
            .LockType = adLockOptimistic
            .Open "SELECT good_code,customer,sum(price*amount)/sum(amount) AS avg_price FROM sales GROUP BY good_code,customer"
            If .RecordCount > 0 Then
                MSFlexGrid1.Clear
                MSFlexGrid1.Cols = 3
                MSFlexGrid1.Rows = .RecordCount + 1
                MSFlexGrid1.TextMatrix(0, 0) = "商品编号"
                MSFlexGrid1.TextMatrix(0, 1) = "顾客名称"
                MSFlexGrid1.TextMatrix(0, 2) = "平均价格"            .MoveLast: .MoveFirst
                For i = 1 To .RecordCount
                    MSFlexGrid1.TextMatrix(i, 0) = .Fields(0).Value & vbNullString
                    MSFlexGrid1.TextMatrix(i, 1) = .Fields(1).Value & vbNullString
                    MSFlexGrid1.TextMatrix(i, 2) = .Fields(2).Value & vbNullString
                    .MoveNext
                Next
            End If
            .Close
        End With
        Set Rs = Nothing