爱问知识人 爱问教育 医院库

VB连SQL SERVER问题

首页

VB连SQL SERVER问题

各位高手好!
本人用VB+ACCESS做了一套系统,现需要把数据库改成SQL SERVER版.并且支持远程和多用户。
本人从未接解SQL数据库.特向各位高手求教.
我已经安装了SQL正式商业版.并在Local服务组里面建立了对应的数据库(数据库名为:CRC_TZ)
我以前的程序是在Bas模块里做了ADO连接函数块,内容包括(建立数据库链接、利用SQL语句返回记录集、利用SQL语句执行无返回的数据库命令)代码附后。
请高手帮我修改一下这个模块,以便我能连上SQL SERVER数据库。实现远程数据库和多用户同时操作的目的。
热心的朋友请留着您的联系方式,便于向您请教。
-------------------------
Private Function Connect() As Variant '建立数据库链接 
    On Error GoTo ErrorHandler 
    Dim CN As New ADODB.Connection 
    CN.Provider = "Microsoft.Jet.OLEDB.4.0" 
    'CN.Provider = "sqloledb.1" 
    CN.ConnectionTimeout = 5 
    CN.CursorLocation = adUseClient 
    CN.ConnectionString = "data source=" & App.Path & "\ b" '在此修改数据库路径 
    CN.Open 
    Set Connect = CN 
    Exit Function 
ErrorHandler: 
    Set Connect = Nothing 
    Set CN = Nothing 
    strerror = Err.Description 
End Function 
Public Function GetRecordset(strSQL As String) As ADODB.Recordset '形成记录集 
    On Error GoTo ErrorHandler 
    Dim CN As New ADODB.Connection 
    Dim rs As New ADODB.Recordset 
    Set CN = Connect() 
    If (CN Is Nothing) Then 
        Set GetRecordset = Nothing 
        Exit Function 
    End If 
    rs.CursorType = adOpenDynamic 
    rs.CursorLocation = adUseClient 
    rs.ActiveConnection = CN 
    rs.Open strSQL 
    Set CN = Nothing ' 
    Set GetRecordset = rs 
    Exit Function 
ErrorHandler: 
    strerror = Err.Description 
    Set CN = Nothing 
    Set rs = Nothing 
    Set GetRecordset = Nothing 
End Function 
Public Function ExcuteCmd(strSQL As String) As Boolean '执行无返回的SQL语句 
    On Error GoTo ErrorHandler 
    Dim CN As ADODB.Connection 
    Dim CMD As New ADODB.Command 
    Set CN = Connect() 
    If (CN Is Nothing) Then 
    GoTo ErrorHandler 
    End If 
    CMD.ActiveConnection = CN 
    CMD.CommandText = strSQL 
    CMD.CommandType = adCmdText 
    CMD.Execute 
    ExcuteCmd = True 
    Set CN = Nothing 
    Set CMD = Nothing 
    Exit Function 
ErrorHandler: 
    ExcuteCmd = False 
    strerror = Err.Description 
    Set CN = Nothing 
    Set CMD = Nothing 
End Function  

提交回答
好评回答
  • 2006-06-16 11:38:07
      '数据源信息常量
    Public Const conn As String = "Provider=SQLOLEDB。1;Password=sa;User ID=sa;Initial Catalog=StudentFiles;Data Source=localhost;"
     '一次执行connect操作可以访问数据库的次数
    Public Const CONNECT_LOOP_MAX = 10
    '== 标记数据库是否连接 ==
    Private IsConnect As Boolean
    '== 标记执行Connect()函数后,访问数据库的次数 ==
    Private Connect_Num As Integer
    Private cnn As ADODB。
      Connection '连接数据库的Connection对象 Private rs As ADODB。Recordset '保存结果集的Recordset对象 '连接数据库 Private Sub Connect() '如果连接标记为真,则返回。
      否则会出错 If IsConnect = True Then Exit Sub End If '关键New用于创建新对象cnn Set cnn = New ADODB。Connection '设置连接字符串ConnectionString属性 cnn。
      ConnectionString = conn '打开到数据库的连接 cnn。Open '判断连接的状态 If cnn。State <> adStateOpen Then '如果连接不成功,则显示提示信息,退出程序 MsgBox "数据库连接失败" End End If '设置连接标记,表示已经连接到数据库 IsConnect = True End Sub '断开与数据库的连接 Private Sub Disconnect() Dim Rc As Long '如果连接标记为假,标明已经断开连接,则直接返回 If IsConnect = False Then Exit Sub End If '关闭连接 cnn。
      Close '释放cnn Set cnn = Nothing '设置连接标记,表示已经断开与数据库的连接 IsConnect = False End Sub '使用Connect_Num控制数据库连接 Public Sub DB_Connect() Connect_Num = Connect_Num + 1 Connect End Sub Public Sub DB_Disconnect() If Connect_Num >= CONNECT_LOOP_MAX Then Connect_Num = 0 Disconnect End If End Sub '强制关闭api方式访问的数据库,计数器复位 Public Sub DBapi_Disconnect() Connect_Num = 0 Disconnect End Sub '执行数据库操作语句 Public Sub SQLExt(ByVal TmpSQLstmt As String) '创建Command对象cmd Dim cmd As New ADODB。
      Command '连接到数据库 DB_Connect '设置cmd的ActiveConnection属性,指定与其关联的数据库连接 Set cmd。ActiveConnection = cnn '设置要执行的命令文本 cmd。
      CommandText = TmpSQLstmt ' MsgBox TmpSQLstmt '执行命令 cmd。Execute '清空cmd对象 Set cmd = Nothing '断开与数据库的连接 DB_Disconnect End Sub '执行数据库查询语句 Public Function QueryExt(ByVal TmpSQLstmt As String) As ADODB。
      Recordset '创建Recordset对象rst Dim rst As New ADODB。Recordset '连接到数据库 DB_Connect '设置rst的ActiveConnection属性,指定与其关联的数据库连接 Set rst。
      ActiveConnection = cnn '设置游标类型 rst。CursorType = adOpenDynamic '设置锁定类型 rst。LockType = adLockOptimistic '打开记录集 rst。
      Open TmpSQLstmt '返回记录集 Set QueryExt = rst End Function 。

    1***

    2006-06-16 11:38:07

其他答案

    2006-06-15 23:23:47
  •   这是我的系统模块里的代码。vb+sql的看看对你有帮助吗。
    Attribute VB_Name = "Module1"
    Public UserName  As String
    Public fMainForm As frmMain
    Public gintdMode As Integer
    Public gintvMode As Integer
    Public gintsMode As Integer
    Public gintrMode As Integer
    Public gintaMode As Integer
    Public flagdEdit As Boolean
    Public flagvEdit As Boolean
    Public flagsEdit As Boolean
    Public flagrEdit As Boolean
    Public flagaEdit As Boolean
    Sub Main()
        Dim fLogin As New frmLogin
        fLogin。
      Show vbModal If Not fLogin。OK Then 'Login Failed so exit app End End If Unload fLogin Set fMainForm = New frmMain fMainForm。
      Show End Sub Public Function ConnectString() _ As String 'returns a DB ConnectString ConnectString = "FileDSN=vehicle。
      dsn;UID=sa;PWD=" End Function Public Function ExecuteSQL(ByVal SQL _ As String, MsgString As String) _ As ADODB。
      Recordset 'executes SQL and returns Recordset Dim cnn As ADODB。Connection Dim rst As ADODB。Recordset Dim sTokens() As String On Error GoTo ExecuteSQL_Error sTokens = Split(SQL) Set cnn = New ADODB。
      Connection cnn。Open ConnectString If InStr("INSERT,DELETE,UPDATE", _ UCase$(sTokens(0))) Then cnn。Execute SQL MsgString = sTokens(0) & _ " query successful" Else Set rst = New ADODB。
      Recordset rst。Open Trim$(SQL), cnn, _ adOpenKeyset, _ adLockOptimistic 'rst。MoveLast 'get RecordCount Set ExecuteSQL = rst MsgString = "查询到" & rst。
      RecordCount & _ " 条记录 " End If ExecuteSQL_Exit: Set rst = Nothing Set cnn = Nothing Exit Function ExecuteSQL_Error: MsgString = "查询错误: " & _ Err。
      Description Resume ExecuteSQL_Exit End Function Public Sub EnterToTab(Keyasc As Integer) If Keyasc = 13 Then SendKeys "{TAB}" End If End Sub 。
      

    天***

    2006-06-15 23:23:47

类似问题

换一换
  • VB 相关知识

  • 电脑网络技术
  • 电脑网络

相关推荐

正在加载...
最新问答 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 171-190
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):