VB如何修改数据库中的信息,为什么我改了后 数据库中的内容不变啊?? 请帮我改正
以下是我的代码DimconnAsNewADODB.ConnectionDimrsAsNewADODB.RecordsetDimSQLAsStringconn.Connec...
以下是我的代码
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim SQL As String
conn.ConnectionString = "Provider=MSDASQL.1;Password=aaa;Persist Security Info=True;User ID=eee;Data Source=test"
conn.Open
SQL = "Select * from PUser WHERE Log_name= '" & username_log & "' AND Password='" & password_log & "'"
rs.Open SQL, conn, adOpenStatic, adLockBatchOptimistic
rs.Fields(0) = Trim(Text1.Text)
rs.Fields(1) = Trim(Text2.Text)
rs.Fields(2) = Trim(Text3.Text)
rs.Fields(3) = Trim(Text4.Text)
rs.Fields(4) = Trim(Text5.Text)
rs.Fields(5) = Trim(Text6.Text)
rs.Fields(6) = Trim(Text7.Text)
rs.Fields(7) = Trim(Text8.Text)
rs.Fields(8) = Trim(Text9.Text)
rs.Fields(9) = Trim(Text10.Text)
rs.Fields(10) = Trim(Text11.Text)
rs.Fields(11) = Trim(Text12.Text)
rs.Fields(12) = 0
rs.Update
rs.Close
Set conn = Nothing
MsgBox ("修改成功!") 展开
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim SQL As String
conn.ConnectionString = "Provider=MSDASQL.1;Password=aaa;Persist Security Info=True;User ID=eee;Data Source=test"
conn.Open
SQL = "Select * from PUser WHERE Log_name= '" & username_log & "' AND Password='" & password_log & "'"
rs.Open SQL, conn, adOpenStatic, adLockBatchOptimistic
rs.Fields(0) = Trim(Text1.Text)
rs.Fields(1) = Trim(Text2.Text)
rs.Fields(2) = Trim(Text3.Text)
rs.Fields(3) = Trim(Text4.Text)
rs.Fields(4) = Trim(Text5.Text)
rs.Fields(5) = Trim(Text6.Text)
rs.Fields(6) = Trim(Text7.Text)
rs.Fields(7) = Trim(Text8.Text)
rs.Fields(8) = Trim(Text9.Text)
rs.Fields(9) = Trim(Text10.Text)
rs.Fields(10) = Trim(Text11.Text)
rs.Fields(11) = Trim(Text12.Text)
rs.Fields(12) = 0
rs.Update
rs.Close
Set conn = Nothing
MsgBox ("修改成功!") 展开
2个回答
展开全部
你未说明何种数据库,我就以Access数据库为例吧:
==========《模块级CODE》===========
Function conn() As ADODB.Connection
Set conn = New ADODB.Connection
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\XXX\XXX.mdb;Jet OLEDB:Database Password=123456;Persist Security Info=False"
End Function
'函数需先引用Microsoft Activex Data Objects 2.8 Library 库。
==========《子程序CODE》===========
一,不返回记录集(插入、更新2种方式)
conn.Execute ("INSERT INTO 数据表名(字段1,字段2,字段3) VALUES(" & 值1 & "," & 值2 & "," & 值3 & ")")
conn.Execute ("UPDATE 数据表名 SET 字段1=" & 值1 & " WHERE 字段1=" & 值2 & " AND 字段2=" & 值3)
二,返回记录集(字段赋值、批量更新2种方式)
Sub DataUpdate()
Dim strSQL As String,temTable As New ADODB.Recordset
strSQL = "SELECT * FROM 数据表名 WHERE 字段1=" & 值1 & " AND 字段2='" & 值2 & "'" AND 字段3=" &值3
temTable.CursorLocation = adUseClient
temTable.Open strSQL, conn, adOpenDynamic, adLockOptimistic
'移动到你要更新的记录数......
1,----------------------------------------------------------------------------
temTable.Fields("字段名").Value=值
'更新后要移动记录(temTable.MoveNext),不然不会保存数据。
2,----------------------------------------------------------------------------
temTable.Update Array("字段1","字段2”), Array(值1,值2)
'不需要移动记录。值也会保存。
temTable.Close
Set temTable = Nothing
End Sub
没注意上面你的代码,以下是改正。
把Persist Security Info=True改成False(安全考虑)
把rs.Open SQL, conn, adOpenStatic, adLockBatchOptimistic
改成rs.Open SQL, conn, adOpenDynamic, adLockBatchOptimistic(adOpenStatic为只读)
==========《模块级CODE》===========
Function conn() As ADODB.Connection
Set conn = New ADODB.Connection
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\XXX\XXX.mdb;Jet OLEDB:Database Password=123456;Persist Security Info=False"
End Function
'函数需先引用Microsoft Activex Data Objects 2.8 Library 库。
==========《子程序CODE》===========
一,不返回记录集(插入、更新2种方式)
conn.Execute ("INSERT INTO 数据表名(字段1,字段2,字段3) VALUES(" & 值1 & "," & 值2 & "," & 值3 & ")")
conn.Execute ("UPDATE 数据表名 SET 字段1=" & 值1 & " WHERE 字段1=" & 值2 & " AND 字段2=" & 值3)
二,返回记录集(字段赋值、批量更新2种方式)
Sub DataUpdate()
Dim strSQL As String,temTable As New ADODB.Recordset
strSQL = "SELECT * FROM 数据表名 WHERE 字段1=" & 值1 & " AND 字段2='" & 值2 & "'" AND 字段3=" &值3
temTable.CursorLocation = adUseClient
temTable.Open strSQL, conn, adOpenDynamic, adLockOptimistic
'移动到你要更新的记录数......
1,----------------------------------------------------------------------------
temTable.Fields("字段名").Value=值
'更新后要移动记录(temTable.MoveNext),不然不会保存数据。
2,----------------------------------------------------------------------------
temTable.Update Array("字段1","字段2”), Array(值1,值2)
'不需要移动记录。值也会保存。
temTable.Close
Set temTable = Nothing
End Sub
没注意上面你的代码,以下是改正。
把Persist Security Info=True改成False(安全考虑)
把rs.Open SQL, conn, adOpenStatic, adLockBatchOptimistic
改成rs.Open SQL, conn, adOpenDynamic, adLockBatchOptimistic(adOpenStatic为只读)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询