100分求高手解答,来者有分。怎样才可以取消Delphi自动提交的动作。(100分)

  • 主题发起人 主题发起人 guorui_digi
  • 开始时间 开始时间
G

guorui_digi

Unregistered / Unconfirmed
GUEST, unregistred user!
eg:在frmedtUser的窗体上有三个TDBEdit及一个Button按钮。
分别对应如下:edtUserName,edtPassword,edtUserEmail,及btnSave按钮。
当用户点击btnSave按钮后,我不想让Delphi自动将这些内容增加到数据库中
而想手动的写SQL语句来增加记录,
例如:
insert into(userid,username,password,useremail)
valuses(seq_userid.nextval,edtUsername.text,edtPassword.text,edtUserEmail)
通过这个语句来完成。
因为数据库是Oracle的,而设定了自增序列seq_userid,
所以由delphi自动保存的动作qryUser.post会出错,

请问大家有没有好的办法解决。分不够可以再加。
 
改一下BDE里的 SQLPASSTHRU MODE 配置

Specifies whether or not the application will be able to access the SQL server via both desktop commands and passthrough SQL in the same alias connection. Possible modes and their meanings are listed in this table.

Setting Meaning

SHARED AUTOCOMMIT Passthrough SQL and non-passthrough SQL share the same connection, and passthrough SQL behaves in a similar fashion to non-passthrough. This means that, as long as the user is not in an explicit client transaction or batch mode, passthrough SQL statements are automatically committed.
SHARED NOAUTOCOMMIT Passthrough SQL and non-passthrough SQL share the same connection, but the SQL driver does not automatically commit passthrough statements. In this mode, passthrough behavior is server-dependent.

NOT SHARED Passthrough SQL and non-passthrough SQL do NOT share the same connection. Updateable SQL queries are not supported with aliases that have the SQLPASSTHRU MODE parameter set to NOT SHARED. (Default)

Default:
The default value is NOT SHARED (or blank setting) for all SQL Links drivers.

SHARED AUTOCOMMIT and SHARED NOAUTOCOMMIT modes do not support all passthrough SQL statements.When SHARED AUTOCOMMIT or SHARED NOAUTOCOMMIT mode is set, do not execute transaction control statements in passthrough SQL; use your BDE API to begin, commit, or roll back the transaction.
When passthrough SQL and non-passthrough queries share a single connection, the record cache does not immediately reflect updates performed in passthrough SQL.
For a discussion of how the application processes passthrough SQL queries, and information about using passthough SQL with your Borland application, see your application product documentation.

For an explanation of SQL query modes, including passthrough, see Default and passthrough SQL query modes.
 
to:tseug,
太谢谢你的回复了,可是我用的是DBADO组件,是在哪里改呢?
 
没太明白,不过,数据库自增字段,不写在insert里。如:
insert into(username,password,useremail)
valuses(edtUsername.text,edtPassword.text,edtUserEmail)
执行就可以了。
 
先增加一条空记录,然后再ApplyUpdate.
 
是自增序列seq_userid的话,你在插入数据时不做这样的语句就行了
insert into(userid,username,password,useremail)
~~~~~~
valuses(seq_userid.nextval,edtUsername.text,edtPassword.text,edtUserEmail)
~~~~~~~~~~~~~~~~~~
直接用
insert into(username,password,useremail)
valuses(edtUsername.text,edtPassword.text,edtUserEmail)
这可能涉及到你的用户ID不能控制问题,建议你用一个单独的表建立一个字段
userid。每当进行插入动作时触发事件时,把userid加一就可以了



 
Oracle没有用过,如果在数据库用触发器,那么随便给他一个值userid即可,如果是
数据库自动计数,空就可以了,多试试
 
各位可能我说的不够明白,我的意思是:假如窗体上有两个按钮,btnAdd,btnSave,
先点击btnAdd后(qryUser.insert),此时窗体上edtUsername,edtPasssword,edtUserEmail
的内容为空,在我填入内容后,然后点击btnSave按钮(qryUser.Post)此时便会出错,
原因是UserID字段没有被Delphi填入相应的值,(前提如果UserID是一个自增序列)
所以我就想更改btnSave中的代码,想手动的写
qryUser.open()
qryUser.Sql.text='insert into user(userid.......'
....
qryUser.Post;
可是Delphi却会自动增加一条记录,等于说现在是两条记录。
请问该怎样解决。

谢谢大家的回复。
 
可以有办法。
你添加一个TDBEdit,隐藏,绑定到userid字段,点击添加按钮后先添加一条记录
(append/insert),然后取Sequence的下一个值放到这个TDBEdit中——通过SQL来取,
select Seq.nextval from dual。这样就可以实现你的要求了。
 
to:desertsmoke
谢谢您的回复,也感谢大家的关注,
通过各位的发言我已获知两种解决办法:
1,在oracle数据库写一个触发器,在新增记录之前将seq_userid.nextval给userid
2.第二种就是desertsmoke所说的办法。

可我还想知道可否正面解决此问题,就是说在点击btnSave时,让Delphi不自动提交,而
通过自已写SQL语句来控制,毕竟写SQL语句比较灵活,
以后对于有相似的问题也就可以用这种办法解决,是不是?

所以还请大家多多讨论这个问题,谢谢。
 
写触发器的方法是行不通的,因为要修改的表是变化表,而在触发器中是不能被修改变化表(触发表)的。
 
qryUser.open()
qryUser.Sql.text='insert into user(userid.......'
....
qryUser.Post;
?????????????
一是用非数据敏感控件edit代替dbedit
二是query语句这样写
query.close;
query.sql.lear;
query.sql.add('insert....');
query.execsql;
提交就行了,不要用post方法
就不会出现两条记录问题
 
OK,谢谢大家,我回去试试,明天给分。
 
adoquery.locktype=ltBatchOptimistic


adoquery.UpdateBatch(arall)
 
结贴了,谢谢大家
 
后退
顶部