在线求助:将一张表中内容搬到另一个数据库中某张表,ODBC中报告出错,错在哪里?

时间:2022-12-11 17:16:26
将一张表中内容搬到另一个数据库中某张表
m_pfset指源表的CRecordSet对象指针
m_ptset指目的表的CRecordSet对象指针

在MyView.cpp中代码如下:
m_pfset = &GetDocument()->m_fset;
m_ptset = &GetDocument()->m_tset;
if (m_pfset->IsOpen())
m_pfset->Close();
if (m_ptset->IsOpen())
m_ptset->Close();
m_pfset->Open();
m_ptset->Open();

m_ptset->m_pDatabase->BeginTrans();
if(m_pfset->IsBOF())
{
return;
}
m_pfset->MoveFirst();
while(!m_pfset->IsEOF()){
m_ptset->AddNew();
m_ptset->m_T = m_pfset->m_T;//CTime对象
m_ptset->m_V = m_pfset->m_V;//double型
m_ptset->m_F = m_pfset->m_F;//int型
m_ptset->Update();
m_pfset->MoveNext();
}
m_ptset->m_pDatabase->CommitTrans();

调试中,出现错误对话框“Unhandled exception in db5.exe(MFC42D.DLL):0xC0000005:Access Violation”

10 个解决方案

#1


是不是空值引起的?

#2


不过,如果不加上BeginTrans()和CommitTrans(),运行时,除了第一次会出错外,以后都能运行正常。加上事务后,每次都会出错。

请大家指点!

#3


我的源表上是有数据的,目的表上没有任何数据

#4


是不是有其他程序打开了你的数据表?

另外,建议Open()时,请判断一下返回值或者捕获异常

if(! m_pfset->Open(CRecordset::dynaset ) )
{return;}
if(! m_ptset->Open(CRecordset::dynaset ))
{return;}

#5


我把devzhao老兄的代码加到程序里,出现对话框“记录集是只读”,没有出错信息了,请问为什么?我应该怎么做?

#6


A dynaset stays synchronized with data updates made by other users. A snapshot is a static view of the data. Each form represents a set of records fixed at the time the recordset is opened, but when you scroll to a record in a dynaset, it reflects changes subsequently made to the record, either by other users or by other recordsets in your application.
To work with either kind of recordset, you typically derive an application-specific recordset class from CRecordset. Recordsets select records from a data source, and you can then: 

Scroll through the records. 
Update the records and specify a locking mode. 
Filter the recordset to constrain which records it selects from those available on the data source. 
Sort the recordset. 
Parameterize the recordset to customize its selection with information not known until run time

如果数据源是只读的,则记录集也将是只读的
看看是否在别的正在用Recordset时已经Lock了

#7


不过我建议你用SQL语句很容易复制表的:
 说明:复制表(只复制结构,源表名:a 新表名:b)
SQL: select * into b from a where 1<>1

说明:拷贝表(拷贝数据,源表名:a 目标表名:b)
insert into b(a, b, c) select d,e,f from a;

#8


我的源数据是本地的Sybase数据库,目的地是远程的SQL Server数据库,这样也能用SQL实现吗?
我用Delphi能做,不过VC现在还不顺手。

我知道我的目的地数据库是只读的,所以程序会出错,可是怎么会是只读的呢?有什么办法能改变其只读属性?

#9


我用快照方式打开目的地数据库,只要不用事务机制,就能插入数据记录,只是有时会报错。
一旦采用事务机制,就一直报错。

用动态方式打开目的数据库,报告只读,不能插数据,不过没有那种出错信息了。

#10


我打算改用SQL语句实现记录的转移,不过问题是两个不同的数据库中,如何实现时间字段的复制(CTime对象),怎么组合到SQL语句中?

谢谢!

#1


是不是空值引起的?

#2


不过,如果不加上BeginTrans()和CommitTrans(),运行时,除了第一次会出错外,以后都能运行正常。加上事务后,每次都会出错。

请大家指点!

#3


我的源表上是有数据的,目的表上没有任何数据

#4


是不是有其他程序打开了你的数据表?

另外,建议Open()时,请判断一下返回值或者捕获异常

if(! m_pfset->Open(CRecordset::dynaset ) )
{return;}
if(! m_ptset->Open(CRecordset::dynaset ))
{return;}

#5


我把devzhao老兄的代码加到程序里,出现对话框“记录集是只读”,没有出错信息了,请问为什么?我应该怎么做?

#6


A dynaset stays synchronized with data updates made by other users. A snapshot is a static view of the data. Each form represents a set of records fixed at the time the recordset is opened, but when you scroll to a record in a dynaset, it reflects changes subsequently made to the record, either by other users or by other recordsets in your application.
To work with either kind of recordset, you typically derive an application-specific recordset class from CRecordset. Recordsets select records from a data source, and you can then: 

Scroll through the records. 
Update the records and specify a locking mode. 
Filter the recordset to constrain which records it selects from those available on the data source. 
Sort the recordset. 
Parameterize the recordset to customize its selection with information not known until run time

如果数据源是只读的,则记录集也将是只读的
看看是否在别的正在用Recordset时已经Lock了

#7


不过我建议你用SQL语句很容易复制表的:
 说明:复制表(只复制结构,源表名:a 新表名:b)
SQL: select * into b from a where 1<>1

说明:拷贝表(拷贝数据,源表名:a 目标表名:b)
insert into b(a, b, c) select d,e,f from a;

#8


我的源数据是本地的Sybase数据库,目的地是远程的SQL Server数据库,这样也能用SQL实现吗?
我用Delphi能做,不过VC现在还不顺手。

我知道我的目的地数据库是只读的,所以程序会出错,可是怎么会是只读的呢?有什么办法能改变其只读属性?

#9


我用快照方式打开目的地数据库,只要不用事务机制,就能插入数据记录,只是有时会报错。
一旦采用事务机制,就一直报错。

用动态方式打开目的数据库,报告只读,不能插数据,不过没有那种出错信息了。

#10


我打算改用SQL语句实现记录的转移,不过问题是两个不同的数据库中,如何实现时间字段的复制(CTime对象),怎么组合到SQL语句中?

谢谢!