'连接数据库 dim conn as new adodb.connection With conn If .State = adStateOpen Then .Close .ConnectionString = "driver=SQL Server;server=计算机名或IP地址;uid=SA;pwd=SA的密码;database=数据库名" .CommandTimeout = 0 .Open End With
'对数据库的增删改可以用事务来控制 private sub command1_click() on error goto errhandle conn.begintrans '开启事务 conn.execute "insert into tb(...) values(...)" '增加记录 conn.execute "delete from tb where ..." '删除记录 conn.execute "update tb set ... where ..." '修改记录 conn.committrans '提交事务 exit sub '错误处理 errhandle: conn.rollbacktrans '出错,回滚事务 exit sub end sub
dim conn as new adodb.connection
With conn
If .State = adStateOpen Then .Close
.ConnectionString = "driver=SQL Server;server=计算机名或IP地址;uid=SA;pwd=SA的密码;database=数据库名"
.CommandTimeout = 0
.Open
End With
private sub command1_click()
on error goto errhandle
conn.begintrans '开启事务
conn.execute "insert into tb(...) values(...)" '增加记录
conn.execute "delete from tb where ..." '删除记录
conn.execute "update tb set ... where ..." '修改记录
conn.committrans '提交事务
exit sub
'错误处理
errhandle:
conn.rollbacktrans '出错,回滚事务
exit sub
end sub
Adodc1.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=IQC"
Adodc1.RecordSource = "SELECT * from IQC报表"