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#

Establish the connection

When you want to start an interaction with the storage on a server, you'll pretty much always use the functions detailed on this page. We put them into our 'init' function.
Inside the main function, we will add the call to init() with the necessary error handling.
Tutorial.cpp – main function
Tutorial.cs – main function
WDomain* pDomain;

if(S_OK != init(pDomain, strServerAddress, usServerPort, guidDomainId, ulStorageId))
{
	printf("init failed\n");
	return -1;
}
WDomain pDomain;

if(0 != init(out pDomain, strServerAddress, usServerPort, guidDomainId, ulStorageId))
{
	Console.WriteLine("init failed");
	return;
}
We always start with a call to InitializeThreadInitializeThread. This will load and initialize the libraries needed to work with DataFS and DataFSAccess.
init.cpp
init.cs
#include "pch.h"

HRESULT init(WDomain*& pDomain, const wchar_t* strServerAddress, UINT16 usServerPort, const GUID& guidDomainId, UINT32 ulStorageId)
{
	// load libraries and initialize thread
	InitializeThread();
public static int init(out WDomain pDomain, string strServerAddress, UInt16 usServerPort, Guid guidDomainId, UInt32 ulStorageId)
{
	// load libraries and initialize thread
	ThreadInit.InitializeThread();
Next, we will createcreate the domain object and initializeinitialize it.
	// build domain object
	pDomain = Domain_Create();

	pDomain->Initialize(&guidDomainId);
	// build domain object
	pDomain = WDomain.Create();

	pDomain.Initialize(guidDomainId);
After that, we establish the connectionestablish the connection. As you can see, the third parameter is NULLnull because, for this tutorial, our connection is unencrypted.
	// connect to server
	pDomain->Connect(strServerAddress, usServerPort, NULL);
	// connect to server
	pDomain.Connect(strServerAddress, usServerPort, null);
Now, we are ready to connect to the storageconnect to the storage with "QueryStorage". The second parameter is set to false because don't want to register an event clientevent client for now.
	// connect to storage
	pDomain->QueryStorage(ulStorageId, false);
	// connect to storage
	pDomain.QueryStorage(ulStorageId, false, null);
Finally, we are ready to call "Bind", which initialized the classes defined in DADL with data from the domain's schema. See this pagethis page for further information.
	// bind to schema
	AccessDefinition::Bind(pDomain);

	return S_OK;
}
	// bind to schema
	AccessDefinition.Bind(pDomain);

	return 0;
}
After that, we're technically ready to work with our data, but none exists yet. We'll look at how to uninitialize the connection first of all.

Final code

Our whole 'init' function with correct error handling would look something like this:
init.cpp
init.cs
#include "pch.h"

HRESULT init(WDomain*& pDomain, const wchar_t* strServerAddress, UINT16 usServerPort, const GUID& guidDomainId, UINT32 ulStorageId)
{
	HRESULT hRes;

	// load libraries and initialize thread
	InitializeThread();

	// build domain object
	pDomain = Domain_Create();

	if(FAILED(hRes = pDomain->Initialize(&guidDomainId)))
	{
		wprintf(L"Initialize failed (0x%x)\n", hRes);
		Domain_Destroy(pDomain);
		UninitializeThread();
		return E_FAIL;
	}

	// connect to server
	if(FAILED(hRes = pDomain->Connect(strServerAddress, usServerPort, NULL)))
	{
		wprintf(L"Connect failed (0x%x)\n", hRes);
		pDomain->Uninitialize();
		Domain_Destroy(pDomain);
		UninitializeThread();
		return E_FAIL;
	}

	// connect to storage
	if(FAILED(hRes = pDomain->QueryStorage(ulStorageId, false)))
	{
		wprintf(L"QueryStorage failed (0x%x)\n", hRes);
		pDomain->DisconnectAll();
		pDomain->Uninitialize();
		Domain_Destroy(pDomain);
		UninitializeThread();
		return E_FAIL;
	}

	// bind to schema
	if(FAILED(hRes = AccessDefinition::Bind(pDomain)))
	{
		wprintf(L"Bind failed (0x%x)\n", hRes);
		pDomain->ReleaseStorage(ulStorageId);
		pDomain->DisconnectAll();
		pDomain->Uninitialize();
		Domain_Destroy(pDomain);
		UninitializeThread();
		return E_FAIL;
	}

	return S_OK;
}
using System;
using DataFoundationAccess;

namespace TutorialCs
{
partial class Tutorial
{
	public static int init(out WDomain pDomain, string strServerAddress, UInt16 usServerPort, Guid guidDomainId, UInt32 ulStorageId)
	{
		int hRes;

		// load libraries and initialize thread
		ThreadInit.InitializeThread();

		// build domain object
		pDomain = WDomain.Create();

		if(0 > (hRes = pDomain.Initialize(guidDomainId)))
		{
			Console.WriteLine("Initialize failed ({0:x})", hRes);
			WDomain.Destroy(pDomain);
			ThreadInit.UninitializeThread();
			return -1;
		}

		// connect to server
		if(0 > (hRes = pDomain.Connect(strServerAddress, usServerPort, null)))
		{
			Console.WriteLine("Connect failed ({0:x})", hRes);
			pDomain.Uninitialize();
			WDomain.Destroy(pDomain);
			ThreadInit.UninitializeThread();
			return -1;
		}

		// connect to storage
		if(0 > (hRes = pDomain.QueryStorage(ulStorageId, false, null)))
		{
			Console.WriteLine("QueryStorage failed ({0:x})", hRes);
			pDomain.DisconnectAll();
			pDomain.Uninitialize();
			WDomain.Destroy(pDomain);
			ThreadInit.UninitializeThread();
			return -1;
		}

		// bind to schema
		if(0 > (hRes = AccessDefinition.Bind(pDomain)))
		{
			Console.WriteLine("Bind failed ({0:x})", hRes);
			pDomain.ReleaseStorage(ulStorageId);
			pDomain.DisconnectAll();
			pDomain.Uninitialize();
			WDomain.Destroy(pDomain);
			ThreadInit.UninitializeThread();
			return -1;
		}

		return 0;
	}
}
}
© 2022 Mobiland AG