dim conn as new adodb.connection conn.open ... '与数据库建立连接'增加记录: conn.execute " insert into bm(bmname) values('行政部')" '修改记录: conn.execute " update bm set bmname='行政部1' where bmname='行政部'" '删除记录: conn.execute " delete from bm where bmname='行政部'"
用记录集,不直接调用SQL语句
dim conn as new adodb.connection dim rs as new adodb.recordset dim str as string 'sql语句 dim constring as string '连接字符 conn.open constring str="select * from tablename" rs.open str,conn
Dim Cn As New ADODB.Connection '数据库连接字符串 Cn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;" _ & "Persist Security Info=False;User ID=sa;" _ & "Initial Catalog= database1;Data Source= localhost" If Cn.State <> ADODB.ObjectStateEnum.adStateClosed Then Cn.Close Cn.Open Dim rs As ADODB.Recordset Set rs = New ADODB.Recordset With rs Set .ActiveConnection = Cn .CursorLocation = adUseClient .CursorType = adOpenKeyset .LockType =adLockOptimistic .Open "select * from …… "'这里是你的sql语句(添加、删除、修改) If .RecordCount > 0 Then …… End If .Close End With
conn.open ... '与数据库建立连接'增加记录:
conn.execute " insert into bm(bmname) values('行政部')"
'修改记录:
conn.execute " update bm set bmname='行政部1' where bmname='行政部'"
'删除记录:
conn.execute " delete from bm where bmname='行政部'"
dim rs as new adodb.recordset
dim str as string 'sql语句
dim constring as string '连接字符
conn.open constring str="select * from tablename"
rs.open str,conn
'数据库连接字符串
Cn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;" _
& "Persist Security Info=False;User ID=sa;" _
& "Initial Catalog= database1;Data Source= localhost"
If Cn.State <> ADODB.ObjectStateEnum.adStateClosed Then Cn.Close
Cn.Open Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
With rs
Set .ActiveConnection = Cn
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType =adLockOptimistic
.Open "select * from …… "'这里是你的sql语句(添加、删除、修改)
If .RecordCount > 0 Then
……
End If
.Close
End With