标签:
最近接手一个改善项目,需要从Access转化到SQL Server 2014,使用原有的ODBC连接方式只能查询,不能更新插入。网上一直找不到解决方案,然后自己测试一下使用ADO方式竟然可以连接了。具体问题还未找到,现在贴出例子代码,希望给自己一个帮助找出真正问题所在。
1 Private Sub Command1_Click() 2 On Error GoTo Err 3 Dim db As Database 4 Dim Rs As Recordset 5 Dim Qry As QueryDef 6 Set db = DBEngine(0).OpenDatabase("", False, False, _ 7 "ODBC;driver={SQL Server};server=(local);uid=sa;pwd=你的密码;database=数据库") 8 9 Set Qry = db.CreateQueryDef("") 10 With Qry 11 .SQL = "SELECT * from P_STATUS;" 12 End With 13 14 Set Rs = Qry.OpenRecordset() 15 With Rs 16 Do While Not .EOF 17 MsgBox !SEQNO, vbOKOnly + vbCritical, "提示" 18 .MoveNext 19 Loop 20 End With 21 22 ‘db.Updatable 23 24 Dim intA As Integer 25 intA = 2 26 db.Execute "UPDATE P_STATUS SET STATUS = ‘2‘", dbFailOnError ‘这里出错,不能更新 27 Debug.Print "To Paris:", db.RecordsAffected 28 db.Close 29 Exit Sub 30 Err: 31 Print Err.Number 32 Dim ss As String 33 ss = Err.Description 34 35 db.Close 36 Exit Sub 37 End Sub
1 ‘数据源信息常量 2 Const conn As String = "driver={SQL Server};server=(local);uid=sa;pwd=你的密码;Database=数据库" 3 Const CONNECT_LOOP_MAX = 10 ‘一次执行connect操作,可以访问数据库的次数 4 5 Private IsConnect As Boolean ‘标记数据库是否连接 6 7 Private Connect_Num As Integer ‘标记执行Connect()函数后访问数据的次数 8 9 Private cnn As ADODB.Connection ‘连接数据库的Connect对象 10 11 Private re As ADODB.Recordset ‘保存结果集的Recordset对象 12 13 14 ‘连接数据库 15 Private Sub Connect() 16 17 18 ‘如果连接标记为真,则返回。 19 If IsConnect = True Then 20 Exit Sub 21 End If 22 23 Set cnn = New ADODB.Connection ‘关键new用于创建新对象cnn 24 25 cnn.ConnectionString = conn 26 27 cnn.Open 28 ‘判断连接的状态 29 If cnn.State <> adStateOpen Then 30 MsgBox "数据库连接失败" 31 End 32 End If 33 34 ‘设置连接标识,表示已经连接到数据库 35 IsConnect = True 36 End Sub 37 38 39 ‘断开与数据库的连接 40 Private Sub DisConnect() 41 Dim rc As Long 42 43 If IsConnect = False Then 44 Exit Sub 45 End If 46 ‘关闭连接 47 cnn.Close 48 ‘释放cnn 49 Set cnn = Nothing 50 IsConnect = False 51 End Sub 52 53 ‘使用Connect_Num控制数据连接 54 Public Sub DB_Connect() 55 Connect_Num = Connect_Num + 1 56 Connect 57 End Sub 58 59 ‘使用Connect_Num控制数据断开 60 Public Sub DB_Disconnect() 61 If Connect_Num >= CONNECT_LOOP_MAX Then 62 Connect_Num = 0 63 DisConnect 64 End If 65 End Sub 66 67 ‘强制关闭api方式访问俄的数据库,计数器复位 68 Public Sub DBapi_Disconnect() 69 Connect_Num = 0 70 DisConnect 71 End Sub 72 73 ‘执行数据库操作语言 74 ‘byval 就是按参数的值传递,再传递过程中,参数不会发生变化(也就是将参数值而不是将地址传递给过程的方式,这就使过程访问发哦变量的副本,过程不可改变变量的值); 75 ‘与之对应的是byref,指按参数的地址传值,byref可以省略 76 Public Sub SQLExt(ByVal TmpSQLstmt As String) 77 78 On Error GoTo Err: 79 Dim cmd As New ADODB.Command ‘创建Command对象cmd 80 81 DB_Connect ‘连接数据库 82 83 Set cmd.ActiveConnection = cnn ‘设置cmd的ActiveConnect属性,指定与其关联的数据库连接 84 85 cmd.CommandText = TmpSQLstmt ‘设置要执行的命令文本 86 87 ‘MsgBox TmpSQLstmt 88 89 cmd.Execute ‘执行命令 90 91 Set cmd = Nothing 92 93 DB_Disconnect ‘断开与数据库的连接 94 Exit Sub 95 96 Err: 97 Dim ss As String 98 ss = Err.Description 99 Print ss 100 DB_Disconnect ‘断开与数据库的连接 101 Exit Sub 102 103 End Sub 104 105 ‘执行数据库查询语句 106 Public Function QueryExt(ByVal TmpSQLstmt As String) As ADODB.Recordset 107 108 Dim rst As New ADODB.Recordset ‘创建Rescordset对象rst 109 110 DB_Connect ‘连接数据库 111 112 Set rst.ActiveConnection = cnn ‘设置rst的ActiveConnection属性,指定与其相关的数据库的连接 113 114 rst.CursorType = adOpenDynamic ‘设置游标类型 115 116 rst.LockType = adLockOptimistic ‘设置锁定类型 117 118 rst.Open TmpSQLstmt ‘打开记录集 119 120 Set QueryExt = rst ‘返回记录集 121 122 End Function 123 124 125 126 Private Sub Command2_Click() 127 Set re = QueryExt("SELECT * from P_STATUS;") 128 With re 129 Do While Not .EOF 130 MsgBox !SEQNO, vbOKOnly + vbCritical, "提示" 131 .MoveNext 132 Loop 133 End With 134 Call SQLExt("UPDATE P_STATUS SET STATUS = ‘8‘") 135 End Sub
【VB】操作ODBC-DAO方式操作只能查询,不能更新插入操作解决。
标签:
原文地址:http://www.cnblogs.com/wulishun/p/4285422.html