各位高手:
本人用DELPHI不熟。目前在做这样一个工作,从A数据库导数据到B数据。使用ODBC连接SYBASE,源头使用TADOQUERY检索,目的同样使用TADOQUERY插入数据。代码片断如下://检索源头数据 a_src_query.SQL.Text:= l_select; try
a_src_query.Open(); //开始目的事务 a_dst_query.Connection.BeginTrans();
while Not a_src_query.Eof do
Begin
//此函数作用为根据数据类型忘目的的QUERY中插入语句。使用参数插入方法。 GetSybPreInsertSql(a_tableinfo,a_src_query.Fields,a_dst_query); try
a_dst_query.ExecSQL();
except on
E: Exception do
Begin
a_dst_query.Connection.RollbackTrans;
Result:= -1;
Exit;
end;
end;
//跳下一条记录
a_src_query.Next();
end; a_dst_query.Connection.CommitTrans();
a_src_query.Close();
except
on E: Exception do
Begin
Result:= -1;
a_dst_query.Connection.RollbackTrans;
Exit;
end;
end;但在执行过程发现执行到 a_dst_query.ExecSQL() 语句报出以下错误:
Exception class EOleException with message '在事务中,不能存在多个使用此游标类型的记录集。请更改游标类型、提交事务,或关闭其中某个记录集。'. 新手不明原因所在。请高手指教。
但如果我使用组成SQL语句的方法,就是人工生成(insert into A values('a'))类似语句,则不会出现错误。
本人用DELPHI不熟。目前在做这样一个工作,从A数据库导数据到B数据。使用ODBC连接SYBASE,源头使用TADOQUERY检索,目的同样使用TADOQUERY插入数据。代码片断如下://检索源头数据 a_src_query.SQL.Text:= l_select; try
a_src_query.Open(); //开始目的事务 a_dst_query.Connection.BeginTrans();
while Not a_src_query.Eof do
Begin
//此函数作用为根据数据类型忘目的的QUERY中插入语句。使用参数插入方法。 GetSybPreInsertSql(a_tableinfo,a_src_query.Fields,a_dst_query); try
a_dst_query.ExecSQL();
except on
E: Exception do
Begin
a_dst_query.Connection.RollbackTrans;
Result:= -1;
Exit;
end;
end;
//跳下一条记录
a_src_query.Next();
end; a_dst_query.Connection.CommitTrans();
a_src_query.Close();
except
on E: Exception do
Begin
Result:= -1;
a_dst_query.Connection.RollbackTrans;
Exit;
end;
end;但在执行过程发现执行到 a_dst_query.ExecSQL() 语句报出以下错误:
Exception class EOleException with message '在事务中,不能存在多个使用此游标类型的记录集。请更改游标类型、提交事务,或关闭其中某个记录集。'. 新手不明原因所在。请高手指教。
但如果我使用组成SQL语句的方法,就是人工生成(insert into A values('a'))类似语句,则不会出现错误。
l_return:= ' Insert into ' + a_tableinfo.owner + '.' + a_tableinfo.name ; l_col:= '(';
//取列名,定义变量
for I := 0 to a_fields.Count - 1 do
begin
if i= a_fields.Count - 1 then
l_col:= l_col + ':'+ trim(a_fields.Fields[i].FieldName)
else
l_col:= l_col + ':' + trim(a_fields.Fields[i].FieldName) + ',';
end; l_col := l_col + ')'; l_return:= l_return + ' values ' + l_col; //取参数数值
i:=0;
a_query.Parameters.Clear();
a_query.SQL.Text:= l_return; for I := 0 to a_fields.Count - 1 do
Begin
if (a_fields.Fields[i].DataType = ftWord) or (a_fields.Fields[i].DataType= ftSmallint) or (a_fields.Fields[i].DataType = ftInteger) or (a_fields.Fields[i].DataType= ftLargeint) then
begin
if a_fields.Fields[i].Value = null then
Begin
a_query.Parameters.ParamByName(a_fields.Fields[i].FieldName).Value:= 0
end
Else
Begin
a_query.Parameters.ParamByName(a_fields.Fields[i].FieldName).Value:= a_fields.Fields[i].Value;
end;
end
Else if a_fields.Fields[i].DataType = ftDateTime then
begin
if a_fields.Fields[i].Value = null then
a_query.Parameters.ParamByName(a_fields.Fields[i].FieldName).Value:= strtodate('1900-01-01')
else
a_query.Parameters.ParamByName(a_fields.Fields[i].FieldName).Value:= a_fields.Fields[i].Value;
end
else if a_fields.Fields[i].DataType = ftDate then
begin
if a_fields.Fields[i].Value = null then
a_query.Parameters.ParamByName(a_fields.Fields[i].FieldName).Value:= strtodate('1900-01-01')
else
a_query.Parameters.ParamByName(a_fields.Fields[i].FieldName).Value:= a_fields.Fields[i].Value;
end
else if (a_fields.Fields[i].DataType = ftFloat) or (a_fields.Fields[i].DataType =ftCurrency) or (a_fields.Fields[i].DataType = ftBCD) then
begin
if a_fields.Fields[i].Value = null then
a_query.Parameters.ParamByName(a_fields.Fields[i].FieldName).Value:= 0
else
a_query.Parameters.ParamByName(a_fields.Fields[i].FieldName).Value:=a_fields.Fields[i].Value;
end
Else
Begin
if a_fields.Fields[i].Value = null then
a_query.Parameters.ParamByName(a_fields.Fields[i].FieldName).Value:= ''
else
a_query.Parameters.ParamByName(a_fields.Fields[i].FieldName).Value:=a_fields.Fields[i].Value;
end;
end;
Result:= true;