首页 / 浏览问题 / 组件GIS / 问题详情
iObject10i 属性字段写入总是不成功。
13EXP 2020年07月16日

我自己写了一


void UdbWriteDemo()
{
	//加载驱动器
	OgdcProviderManager::LoadAllProvider();
	UGDataSource* pDS = UGDataSourceManager::CreateDataSource(UGC::UGEngineType::UDB);
	if (!pDS) return;
	OgdcString strUdbPath = _U("E:\\data\\AAAAA.udb");
	OgdcString strAilasName = _U("AAAAA");
	OgdcString StrPassWord = _U("");
	UGDsConnection &m_connection = pDS->GetConnectionInfo();
	m_connection.m_nType = m_nEngineType;
	m_connection.m_strAlias = (OgdcString)strAilasName;
	m_connection.m_strServer = (OgdcString)strUdbPath;
	m_connection.m_strDatabase = (OgdcString)_U("");
	m_connection.m_strUser = (OgdcString)_U("");
	m_connection.m_strPassword = (OgdcString)StrPassWord;
	//创建数据源
	if (!pDS->Create())
	{
		delete pDS;
		pDS = nullptr;
		AfxMessageBox(IDS_CREATE_DATASOURCE_UDB_FAILED);
		return ;
	}

	//创建Vector dataset
	UGDatasetVectorInfo VectorInfo;
	VectorInfo.m_strName = _U("AAAAA");
	VectorInfo.m_nType = (UGDataset::DatasetType::PointZ);
	UGDatasetVector* pVectorDataSet = pDS->CreateDatasetVector(VectorInfo);
	if (pVectorDataSet == nullptr) return;
	//保持打开
	if (!pVectorDataSet->IsOpen())
	{
		pVectorDataSet->Open();
		if (!pVectorDataSet->IsOpen())
		{
			return ;
		}
	}

	//获取数据记录游标
	UGQueryDef pQuery;
	UGRecordset* pRecordset = nullptr;
	pRecordset = pVectorDataSet->Query(pQuery);
	if (pRecordset == nullptr) return;

	//属性字段创建
	OgdcFieldInfos filedInfos;
	filedInfos.AddField(_U("ID"), (OgdcFieldInfo::FieldType::INT32), 0);
	filedInfos.AddField(_U("NAME"), (OgdcFieldInfo::FieldType::NText), 128);
	if (!pVectorDataSet->CreateFields(filedInfos)) return ;

	UGGeoPoint3D geoPoint;
	for (int i = 0; i < 4; i++)
	{
		geoPoint.Make(100.0 + i * 10, 100, 0);
		pRecordset->AddNew(&geoPoint);
		pRecordset->Edit();
		pRecordset->SetFieldValue(UGString(_U("ID")), (UGVariant)(i + 1));
		pRecordset->SetFieldValue(UGString(_U("NAME")), UGString(_U("AAAAA")));
		pRecordset->Update();
	}
	pRecordset->Close();
	pVectorDataSet->Close();
	pDS->Close();

}

个写udb的例子程序,但是写入属性时候总是设置失败,不知道什么原因。

请大神帮忙看看。

1个回答

你先定位一下问题,整个代码主要实现了下面几个内容:

1:创建数据源,判断是否创建成功?

2:创建数据集,判断是否创建成功?

3:给上一步创建成功的数据集新建了两个字段,判断是否成功?

4:往数据集里面新增了4个点对象,并且设置了对应的字段,确定第一点,点是否添加进去了?如果添加成功了点,只是属性字段设置不成功吗?

在pRecordset->AddNew(&geoPoint);后,加一句pRecordset->MoveLast(),然后在进行pRecordset->Edit();后面的操作
4,620EXP 2020年07月16日

是的,点都加进去了,出了属性没进去,都是空白的。

调试发现,就是设置SetFieldValue失败了

在pRecordset->AddNew(&geoPoint);后,加一句pRecordset->MoveLast(),然后在进行pRecordset->Edit();后面的操作,试试这个

创建好数据集后,添加一句:dataset->open()

刚刚试过了,还是SetFieldValue时候会失败。不知道是原因?有没有跟许可有关?我用的试用期90天许可。

跟许可没有关系,你有没有添加open方法
//保持打开
	if (!pVectorDataSet->IsOpen())
	{
		pVectorDataSet->Open();
		if (!pVectorDataSet->IsOpen())
		{
			return ;
		}
	}

我又重新加了判断;


void UdbWirteDemo()
{
	//加载驱动器
	OgdcProviderManager::LoadAllProvider();
	UGDataSource* pDS = UGDataSourceManager::CreateDataSource(UGC::UGEngineType::UDB);
	if (!pDS) return;
	OgdcString strUdbPath = _U("E:\\data\\\\AAAAA.udb");
	OgdcString strAilasName = _U("AAAAA");
	OgdcString StrPassWord = _U("");
	UGDsConnection &m_connection = pDS->GetConnectionInfo();
	m_connection.m_nType = m_nEngineType;
	m_connection.m_strAlias = (OgdcString)strAilasName;
	m_connection.m_strServer = (OgdcString)strUdbPath;
	m_connection.m_strDatabase = (OgdcString)_U("");
	m_connection.m_strUser = (OgdcString)_U("");
	m_connection.m_strPassword = (OgdcString)StrPassWord;
	//创建数据源
	if (!pDS->Create())
	{
		delete pDS;
		pDS = nullptr;
		AfxMessageBox(IDS_CREATE_DATASOURCE_UDB_FAILED);
		return ;
	}

	//创建Vector dataset
	UGDatasetVectorInfo VectorInfo;
	VectorInfo.m_strName = _U("AAAAA");
	VectorInfo.m_nType = (UGDataset::DatasetType::PointZ);
	UGDatasetVector* pVectorDataSet = pDS->CreateDatasetVector(VectorInfo);
	if (pVectorDataSet == nullptr) return;
	UGbool bOpenOK = pVectorDataSet->Open();
	//保持打开
	if (!pVectorDataSet->IsOpen())
	{
		pVectorDataSet->Open();
		if (!pVectorDataSet->IsOpen())
		{
			return ;
		}
	}

	//获取数据记录游标
	UGQueryDef pQuery;
	UGRecordset* pRecordset = nullptr;
	pRecordset = pVectorDataSet->Query(pQuery);
	if (pRecordset == nullptr) return;

	//属性字段创建
	OgdcFieldInfos filedInfos;
	filedInfos.AddField(_U("ID"), (OgdcFieldInfo::FieldType::INT32), 0);
	filedInfos.AddField(_U("NAME"), (OgdcFieldInfo::FieldType::NText), 128);
	if (!pVectorDataSet->CreateFields(filedInfos)) return ;

	UGGeoPoint3D geoPoint;
	UGbool bOK = false;
	int ID = 0;
	for (int i = 0; i < 4; i++)
	{
		geoPoint.Make(100.0 + i * 10, 100, 0);
		pRecordset->AddNew(&geoPoint);
		bOK = pRecordset->MoveLast();
		bOK = pRecordset->Edit();
		if (!pRecordset->SetFieldValue(UGString(_U("ID")), (UGVariant)(i + 1)))
		{
          //这里总是失败 属性设置不成功
			int a = 0;
		}
		if (!pRecordset->SetFieldValue(UGString(_U("NAME")), UGString(_U("AAAAA"))))
		{
			int a = 0;
		}
		ID = pRecordset->Update();
	}
	pRecordset->Close();
	pVectorDataSet->Close();
	pDS->Close();

}

单独添加一个功能按钮去设置字段能够成功吗?和前面的创建数据集创建字段开,重新去获取到ugrecordset,然后进行设置字段的操作。单独这么看代码是没有问题的

好,我试试。

果然,是需要重新获取一个数据集游标。那为啥直接用第一个游标不能直接写入属性呢?这是不是Iobject10i c++ 组件的bug啊?


void TestDemo()
{
	//加载驱动器
	OgdcProviderManager::LoadAllProvider();
	UGDataSource* pDS = UGDataSourceManager::CreateDataSource(UGC::UGEngineType::UDB);
	if (!pDS) return;
	OgdcString strUdbPath = _U("E:\\data\\AAAAA.udb");
	OgdcString strAilasName = _U("AAAAA");
	OgdcString StrPassWord = _U("");
	UGDsConnection &m_connection = pDS->GetConnectionInfo();
	m_connection.m_nType = m_nEngineType;
	m_connection.m_strAlias = (OgdcString)strAilasName;
	m_connection.m_strServer = (OgdcString)strUdbPath;
	m_connection.m_strDatabase = (OgdcString)_U("");
	m_connection.m_strUser = (OgdcString)_U("");
	m_connection.m_strPassword = (OgdcString)StrPassWord;
	//创建数据源
	if (!pDS->Create())
	{
		delete pDS;
		pDS = nullptr;
		AfxMessageBox(IDS_CREATE_DATASOURCE_UDB_FAILED);
		return ;
	}

	//创建Vector dataset
	UGDatasetVectorInfo VectorInfo;
	VectorInfo.m_strName = _U("AAAAA");
	VectorInfo.m_nType = (UGDataset::DatasetType::PointZ);
	UGDatasetVector* pVectorDataSet = pDS->CreateDatasetVector(VectorInfo);
	if (pVectorDataSet == nullptr) return;
	UGbool bOpenOK = pVectorDataSet->Open();
	//保持打开
	if (!pVectorDataSet->IsOpen())
	{
		pVectorDataSet->Open();
		if (!pVectorDataSet->IsOpen())
		{
			return ;
		}
	}

	//获取数据记录游标
	UGQueryDef pQuery;
	UGRecordset* pRecordset = nullptr;
	pRecordset = pVectorDataSet->Query(pQuery);
	if (pRecordset == nullptr) return;

	//属性字段创建
	OgdcFieldInfos filedInfos;
	filedInfos.AddField(_U("ID"), (OgdcFieldInfo::FieldType::INT32), 0);
	filedInfos.AddField(_U("NAME"), (OgdcFieldInfo::FieldType::NText), 128);
	if (!pVectorDataSet->CreateFields(filedInfos)) return ;

	//创建Geometry
	UGGeoPoint3D geoPoint;
	UGbool bOK = false;
	int ID = 0;
	int i = 0;
	for ( i = 0; i < 4; i++)
	{
		geoPoint.Make(100.0 + i * 10, 100, 0);
		pRecordset->AddNew(&geoPoint);
		bOK = pRecordset->MoveLast();
		bOK = pRecordset->Edit();
		//写入属性字段
		if (!pRecordset->SetINT32(UGString(_U("ID")), (i + 1)))
		{
			//写入失败 
			int a = 0;
		}
		if (!pRecordset->SetString(UGString(_U("NAME")), UGString(_U("AAAAA"))))
		{
			//写入失败
			int a = 0;
		}
		ID = pRecordset->Update();
	}
	pRecordset->Close();

	UGQueryDef pQuery1;
	UGRecordset* pRecordset1 = nullptr;
	pRecordset1 = pVectorDataSet->Query(pQuery1);
	if (pRecordset1 == nullptr) return;
	bOK = pRecordset1->MoveFirst();

	for ( i = 0; i < 4; i++)
	{
		pRecordset1->SeekID(i + 1);
		bOK = pRecordset1->Edit();
		//写入属性字段
		if (!pRecordset1->SetINT32(UGString(_U("ID")), (i + 10)))
		{
			//写入成功
			int a = 0;
		}
		if (!pRecordset1->SetString(UGString(_U("NAME")), UGString(_U("BBBBB"))))
		{
			//写入成功
			int a = 0;
		}
		ID = pRecordset1->Update();

	}
	pRecordset1->Close();
	pVectorDataSet->Close();
	pDS->Close();

}


...