有无数种方法:
除了楼上的,介绍另一种常用的方法:ODBC+ADO
先在控制面板里设置ODBC,如:dsn=aaa,则可以这样
dim cn as new adodb.connection
cn.open "dsn=aaa;uid=sa;pwd;database=xxx"
即可连上数据库其实Msdn带有很多不错的例子,你可以看一看。

解决方案 »

  1.   

     Function exec_procedure(sql As String, Optional i As Integer = 0) As String
        exec_procedure = ""
        If sql = "" Then Exit Function
        Dim cnn1 As ADODB.Connection
        Dim rst1 As ADODB.Recordset
        Set cnn1 = New ADODB.Connection
        Call cnn1.Open(cnstr)
        Set rst1 = New ADODB.Recordset
        rst1.ActiveConnection = cnn1
        On Error GoTo errorhandle
        Call rst1.Open(sql, cnn1, adOpenKeyset, adLockReadOnly)
        If i <> 555 And i <> 999 Then
            If rst1.RecordCount = -1 Then exec_procedure = CStr(rst1.Fields(i).value)
            If rst1.RecordCount = 1 Then exec_procedure = CStr(rst1.Fields(i).value)
        End If
            If i = 999 Then
                ReDim arrary_rpt(rst1.Fields.Count) As String
                If (rst1.RecordCount = 1 Or rst1.RecordCount = -1) And rst1.Fields.Count >= 1 Then
                    Dim k As Integer
                    For k = 0 To rst1.Fields.Count - 1
                       arrary_rpt(k) = rst1.Fields(k).value
                    Next
                Else
                    For k = 0 To rst1.Fields.Count - 1
                       arrary_rpt(k) = ""
                    Next
                End If
            End If
        Set rst1 = Nothing
        cnn1.Close
        Set cnn1 = Nothing
        Exit Function
    errorhandle:
        Err.Clear
        Set rst1 = Nothing
        Set cnn1 = Nothing
        Exit Function
    End Function
    Public arrary_rpt() As String
    ...
    cnnstr = "Provider=SQLOLEDB.1;Password=1234567;Persist Security Info=True;User ID=sa;Initial Catalog=tempdb;Data Source=fphoenix"
      

  2.   

    应是:
    cnstr = "Provider=SQLOLEDB.1;Password=1234567;Persist Security Info=True;User ID=sa;Initial Catalog=tempdb;Data Source=fphoenix"
      

  3.   

    先用ADODC控件,然后看连接字符串
      

  4.   

    你可以用vb的数据库向导连接sql数据库
    跟着向导一步一步做就行了