with MYRECORDSET .ActiveConnection = 连接字符串 .CursorLocation = adUseServer .CursorType = adOpenDynamic .LockType = adLockOptimistic .Source = "查询语句" .Open .AddNew .Fields("name").Value = "TESTNAME" .Update .Close End With
第二句改成 MYRECORDSET!NAME="TESTNAME" 点改叹号
关键是.CursorLocation = adUseServer
在写SQL语句时写成“select * from 表名....."就万无一失了
三个疑点?完整的代码如下:Private Sub Command1_Click() Dim cnn1 As ADODB.Connection Dim xcom As ADODB.Command Dim xrs As ADODB.Recordset Dim strCnn As String Dim strID As String Dim strFirstName As String Dim strLastName As String Dim booRecordAdded As Boolean ' 打开连接。 Set cnn1 = New ADODB.Connection strCnn = "dsn=myado_vfp" cnn1.Open strCnn
' 打开雇员表。
Set xcom = New ADODB.Command Set xcom.ActiveConnection = cnn1 xcom.CommandText = "SELECT * FROM PEOPLE" Set xrs = New ADODB.Recordset xrs.CursorLocation = adUseServer 'xrs.CursorLocation = adUseClient xrs.CursorType = adOpenDynamic xrs.LockType = adLockOptimistic 'xrs.Open xcom, , adOpenStatic, adLockBatchOptimistic xrs.Open xcom, , adOpenDynamic, adLockOptimistic
xrs!Name = "testnew" xrs.Update xrs.Close cnn1.CloseEnd Sub
请参阅David Sceppa 的《ADO编程技术》 有用!!
唉?我现在还在看M$的ADO210.chM,还是不行啊:(
写成xrs.CursorLocation = adUseClient也不成吗?
最后是改成这样才行了。Private Sub Command1_Click()'看来只有使用adUseClient才能正确地查到准确的记录数, '而采用adUseClient时只有采用adLockBatchOptimistic才能正确地保存数。 Dim cnn1 As ADODB.Connection Dim xcom As ADODB.Command Dim xrs As ADODB.Recordset Dim strCnn As String Dim strID As String Dim strFirstName As String Dim strLastName As String Dim booRecordAdded As Boolean ' 打开连接。 Set cnn1 = New ADODB.Connection strCnn = "dsn=myado_vfp" cnn1.Open strCnn
' 打开雇员表。
Set xcom = New ADODB.Command Set xcom.ActiveConnection = cnn1 xcom.CommandText = "SELECT * FROM PEOPLE" Set xrs = New ADODB.Recordset 'xrs.CursorLocation = adUseServer xrs.CursorLocation = adUseClient xrs.CursorType = adOpenDynamic xrs.LockType = adLockOptimistic xrs.Open xcom, , adOpenStatic, adLockBatchOptimistic 'xrs.Open xcom, , adOpenDynamic, adLockOptimistic
Print xrs.RecordCount
xrs.AddNew
xrs!Name = "testnew" 'xrs.Update xrs.UpdateBatch (adAffectCurrent) xrs.Close cnn1.CloseEnd Sub
现在知道了。 http://support.microsoft.com/support/kb/articles/Q165/4/92.ASPLimitations Recordsets, or cursors, generated by Visual FoxPro through the Visual FoxPro ODBC Driver must be of type STATIC, or READONLY. !!!The Visual FoxPro ODBC driver does not support the KEYSET, or DYNAMIC cursor types. !!!
Microsoft OLE DB Provider for ODBC 属性 仅向前 动态 键集 静态 RecordCount 不可用 不可用 只读 只读
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
.LockType = adLockOptimistic
.Open "表名", oCon
.AddNew
.Fields("name") ="TESTNAME"
.Update
End With
我在调试时发现,当我UPDATE 后还是MYRECORDSET.EDITMODE 还是为AdEditAdd,
不知这是不是问题的关键,因为文档说这里当会是AdEditNone才对啊!!!
,但我还是不知如何处理
.ActiveConnection = 连接字符串
.CursorLocation = adUseServer
.CursorType = adOpenDynamic
.LockType = adLockOptimistic
.Source = "查询语句"
.Open
.AddNew
.Fields("name").Value = "TESTNAME"
.Update
.Close
End With
MYRECORDSET!NAME="TESTNAME"
点改叹号
Dim xcom As ADODB.Command
Dim xrs As ADODB.Recordset
Dim strCnn As String
Dim strID As String
Dim strFirstName As String
Dim strLastName As String
Dim booRecordAdded As Boolean ' 打开连接。
Set cnn1 = New ADODB.Connection
strCnn = "dsn=myado_vfp"
cnn1.Open strCnn
' 打开雇员表。
Set xcom = New ADODB.Command
Set xcom.ActiveConnection = cnn1
xcom.CommandText = "SELECT * FROM PEOPLE" Set xrs = New ADODB.Recordset xrs.CursorLocation = adUseServer
'xrs.CursorLocation = adUseClient
xrs.CursorType = adOpenDynamic
xrs.LockType = adLockOptimistic 'xrs.Open xcom, , adOpenStatic, adLockBatchOptimistic
xrs.Open xcom, , adOpenDynamic, adLockOptimistic
'一经打开,XRS.CURSORTYPE 又变回了adOpenStatic,?为何?
Print xrs.RecordCount
'当 xrs.CursorLocation = adUseServer时记录数为-1?
'只有当 xrs.CursorLocation = adUseClient时才为真正的记录数。? xrs.AddNew
xrs!Name = "testnew"
xrs.Update xrs.Close
cnn1.CloseEnd Sub
的《ADO编程技术》
有用!!
'而采用adUseClient时只有采用adLockBatchOptimistic才能正确地保存数。 Dim cnn1 As ADODB.Connection
Dim xcom As ADODB.Command
Dim xrs As ADODB.Recordset
Dim strCnn As String
Dim strID As String
Dim strFirstName As String
Dim strLastName As String
Dim booRecordAdded As Boolean ' 打开连接。
Set cnn1 = New ADODB.Connection
strCnn = "dsn=myado_vfp"
cnn1.Open strCnn
' 打开雇员表。
Set xcom = New ADODB.Command
Set xcom.ActiveConnection = cnn1
xcom.CommandText = "SELECT * FROM PEOPLE" Set xrs = New ADODB.Recordset 'xrs.CursorLocation = adUseServer
xrs.CursorLocation = adUseClient
xrs.CursorType = adOpenDynamic
xrs.LockType = adLockOptimistic xrs.Open xcom, , adOpenStatic, adLockBatchOptimistic
'xrs.Open xcom, , adOpenDynamic, adLockOptimistic
Print xrs.RecordCount
xrs.AddNew
xrs!Name = "testnew"
'xrs.Update
xrs.UpdateBatch (adAffectCurrent) xrs.Close
cnn1.CloseEnd Sub
及CursorType 很不明白,其中的关联好象很深但ADO210.CHm 好象对此的说明又很不深入:(
http://support.microsoft.com/support/kb/articles/Q165/4/92.ASPLimitations
Recordsets, or cursors, generated by Visual FoxPro through the Visual FoxPro ODBC Driver must be of type STATIC, or READONLY.
!!!The Visual FoxPro ODBC driver does not support the KEYSET, or DYNAMIC cursor types. !!!
属性 仅向前 动态 键集 静态
RecordCount 不可用 不可用 只读 只读
而当xrs.CursorLocation = adUseClient时只会是静态所以。。