为何ADOQuery.LockType := ltBatchOptimistic时批写入无效呢?(100)

  • 主题发起人 主题发起人 iibug
  • 开始时间 开始时间
I

iibug

Unregistered / Unconfirmed
GUEST, unregistred user!
本人新手,在DBGrid中想用缓存的方式自己控制是否提交POST,但是虽然已经设定ADOQuery.LockType := ltBatchOptimistic,DBGrid1.AutoEdit := False,在移动记录时,每次都会自动Post, 而且更新了数据表中的内容!!!在网上搜索数日,困扰多时,实在没有办法了!!环境: Delphi7 + WinXp + 本机SQL2000数据库!后又在服务器WIN2003+SQL2000中测试,仍然无效,不解中!特向各位前辈请教,这是本人在大富翁的第一个贴子!
 
1:检查ADOQuery的AfterPost事件中是否有执行批量提交的代码。2:检查DBGrid1是否是和这个ADOQuery建立的连接。建议新建个工程,单独测试,很快就知道答案了。
 
好的,试试先!再问一下,在数据表记录指针移动时,是不是也会 POST,但是写入缓存不写进数据库中?
 
unit test;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Grids, DBGrids, DB, ADODB;type TFrmTest = class(TForm) myADOConnection: TADOConnection; myADOQuery: TADOQuery; myDataSource: TDataSource; DBGrid1: TDBGrid; ButtonSave: TButton; ButtonCancel: TButton; procedure FormActivate(Sender: TObject); procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean); procedure ButtonSaveClick(Sender: TObject); procedure ButtonCancelClick(Sender: TObject); procedure DBGrid1KeyPress(Sender: TObject; var Key: Char); private { Private declarations } public { Public declarations } end;var FrmTest: TFrmTest;implementation//uses DataModuleUnit;//var// lCanEdit: Boolean;{$R *.dfm}procedure TFrmTest.FormActivate(Sender: TObject);var i: Integer; Str1, StrUserName, StrPassword, StrTableName: String; ServerName, DatabaseName: String; s: String;begin ServerName := '.'; DatabaseName := 'eway2000'; StrTableName := 'TB_Area'; StrUserName := 'sa'; StrPassword := ''; Str1 := ''; Str1 := Str1 + 'Provider=' + 'SQLOLEDB.1' + ';'; Str1 := Str1 + 'Persist Security Info=' + 'False' + ';'; Str1 := Str1 + 'User ID=' + StrUserName + ';'; Str1 := Str1 + 'Password=' + StrPassword + ';'; Str1 := Str1 + 'Initial Catalog=' + '"' + DataBaseName + '"' + ';'; Str1 := Str1 + 'Data Source=' + '"' + ServerName +'"'; with myADOConnection do begin try Close; LoginPrompt := False; ConnectionString := Str1; Open; except ServerName := ''; ShowMessage('连接指定数据库' + DatabaseName + '失败!'); Exit; end; end; myADOQuery.LockType := ltBatchOptimistic; with myADOQuery do begin Connection := myADOConnection; Close; SQL.Clear; SQL.Add('select * from ' + '"' + StrTableName + '"'); Open; end; myDataSource.DataSet := myADOQuery;// myDataSource.AutoEdit := False; DBGrid1.DataSource := myDataSource; case myADOQuery.LockType of ltUnspecified: s := 'ltUnspecified'; ltReadOnly: s := 'ltReadOnly'; ltPessimistic: s := 'ltPessimistic'; ltOptimistic: s := 'ltOptimistic'; ltBatchOptimistic: s := 'ltBatchOptimistic'; else s := 'Nothing?'; end; ShowMessage('ADOQuery.LockType: ' + s); for i := 0 to DBGrid1.Columns.Count -1 do //设置标题居中 begin DBGrid1.Columns.Title.Alignment := taCenter; end;// DBGrid1.Columns[0].Color := clYellow; DBGrid1.ReadOnly := False; FrmTest.KeyPreview := True;end;procedure TFrmTest.FormCloseQuery(Sender: TObject; var CanClose: Boolean);begin if MessageBox(Self.Handle,'您真的要退出程序吗?','退出提示', MB_OKCANCEL+MB_DEFBUTTON2+MB_ICONEXCLAMATION)=idOk then CanClose := True else CanClose := Falseend;procedure TFrmTest.ButtonSaveClick(Sender: TObject);begin if myADOQuery <> nil then myADOQuery.CancelUpdates;end;procedure TFrmTest.ButtonCancelClick(Sender: TObject);begin if myADOQuery <> nil then myADOQuery.UpdateBatch;end;procedure TFrmTest.DBGrid1KeyPress(Sender: TObject; var Key: Char);begin if Key = #13 then //#13: 回车键; 如果是回车键,模拟按Tab键; begin Key := #0; Keybd_Event(VK_TAB, MapvirtualKey(VK_TAB, 0), 0, 0); end;end;//initialization// lCanEdit := False;//finalizationend.
 
还是不行呢?我贴出来了代码
 
//修改时,移动记录焦点,才触发Post //没有多大的问题,估计是你把2个按钮事件写反了,导致你的误解。
 
呵呵,,是写反了,,,我改回去试一下!!![:D][:D][:D] 我好粗心啊!!!以为别的问题呢!!!
 
谢谢,应该可以了!!!!
 
后退
顶部