GitHub    Download    Forum
Overview
Tutorial
Database setup
Preparing the project
Establish the connection
Shut down the connection
DTDL types
Creating the schema
DADL classes
Entry point object
Write and read attributes
Write and read lists
Extending the schema
Modified entry point object
Write and read objects
Write and read object lists
C++ API
C# API
DTDL
DADL
Setup
C++
C#

Write and read attributes

Finally, we can write and read our attributes! To do so, we can use the functions generated by DADL. In the case of the "Description" attribute, it was "get", "set" and "remove".
const wchar_t* strCurrentDescription;
pSupplies->GetDescription(&strCurrentDescription);
pSupplies->SetDescription(L"new description");
pSupplies->RemoveDescription();
string strCurrentDescription;
pSupplies.GetDescription(out strCurrentDescription);
pSupplies.SetDescription("new description");
pSupplies.RemoveDescription();
Our 'writeAttributes' and 'readAttributes' functions might look like this in the next code segments.
writeAttributes.cpp
writeAttributes.cs
#include "pch.h"

void writeAttributes(WSupplies* pSupplies)
{
	// Description
	wprintf(L"Supplies description:\n");
	std::wstring input;
	std::getline(std::wcin, input);
	pSupplies->SetDescription(input.c_str());

	// LastUpdated
	UINT64 ullCurrentTime;
	::GetSystemTimeAsFileTime((FILETIME *)&ullCurrentTime);
	pSupplies->SetLastUpdated(ullCurrentTime);
	wprintf(L"Time was automatically set to current time.\n");

	// Store
	pSupplies->Store();

	// Execute
	HRESULT hRes;
	if(FAILED(hRes = pSupplies->GetDomain()->Execute(Transaction::Store)))
	{
		wprintf(L"Domain failed to execute the transaction (0x%x)\n", hRes);
	}
}
using System;
using DataFoundationAccess;

namespace TutorialCs
{
partial class Tutorial
{
	public static void writeAttributes(WSupplies pSupplies)
	{
		// Description
		string input;
		Console.WriteLine("Supplies description:");
		input = Console.ReadLine();
		pSupplies.SetDescription(input);

		// LastUpdated
		pSupplies.SetLastUpdated(DateTime.UtcNow);
		Console.WriteLine("Time was automatically set to current time.");

		// Store
		pSupplies.Store(Transaction.Store);

		// Execute
		int hRes;
		if(0 > (hRes = pSupplies.GetDomain().Execute(Transaction.Store)))
		{
			Console.WriteLine("Domain failed to execute the transaction (0x{0:x})", hRes);
		}
	}
}
}

readAttributes.cpp
readAttributes.cs
#include "pch.h"

void readAttributes(WSupplies* pSupplies)
{
	HRESULT hRes;

	// Description
	const wchar_t* strDescription;
	if(S_OK == (hRes = pSupplies->GetDescription(&strDescription)))
		wprintf(L"Supplies description: %s\n", strDescription);
	else
		wprintf(L"Cannot read supplies description (0x%x)\n", hRes);

	// LastUpdated 
	UINT64 ullCurrentTime;
	if(S_OK == (hRes = pSupplies->GetLastUpdated(&ullCurrentTime)))
	{
		SYSTEMTIME lastTime;
		::FileTimeToSystemTime((FILETIME*)&ullCurrentTime, &lastTime);
		wprintf(L"Time last updated: %02i.%02i.%04i %02i:%02i (UTC)\n", lastTime.wDay, lastTime.wMonth, lastTime.wYear, lastTime.wHour, lastTime.wMinute);
	}
	else
		wprintf(L"Cannot read supplies time (0x%x)\n", hRes);
}
using System;
using DataFoundationAccess;

namespace TutorialCs
{
partial class Tutorial
{
	public static void readAttributes(WSupplies pSupplies)
	{
		int hRes;

		// Description
		string strDescription;
		if(0 == (hRes = pSupplies.GetDescription(out strDescription)))
			Console.WriteLine("Supplies description: {0}", strDescription);
		else
			Console.WriteLine("Cannot read supplies description (0x{0:x})", hRes);

		// LastUpdated 
		DateTime dtCurrentTime;
		if(0 == (hRes = pSupplies.GetLastUpdated(out dtCurrentTime)))
			Console.WriteLine("Time last updated: {0} (UTC)", dtCurrentTime.ToLongTimeString());
		else
			Console.WriteLine("Cannot read supplies time (0x{0:x})", hRes);
	}
}
}
© 2022 Mobiland AG