Chapter 6. C# API

Table of Contents


A separate Visual Studio solution is provided to build the Berkeley DB C# classes, the examples and the native support library.

The C# API requires version 2.0 of the .NET framework and expects that it has already been installed on your system. For the sake of discussion, we assume that the Berkeley DB source is in a directory called db-VERSION; for example, you downloaded a Berkeley DB archive, and you did not change the top-level directory name. The files related to C# are in four subdirectories of db-VERSION: csharp (the C# source files), libdb_csharp (the C++ files that provide the "glue" between C# and Berkeley DB,) examples_csharp (containing all example code) and test\scr037 (containing NUnit tests for the API).

Building the C# API produces a managed assembly libdb_dotnetVERSION.dll, containing the API, and two native libraries: libdb_csharpVERSION.dll and libdbVERSION.dll. (For all three files, VERSION is [MAJOR][MINOR], i.e. for version 4.8 the managed assembly is libdb_dotnet48.dll.) Following the existing convention, native libraries are placed in either db-VERSION\build_windows\Win32or db-VERSION\build_windows\x64, depending upon the platform being targeted. In all cases, the managed assembly will be placed in db-VERSION\build_windows\AnyCPU.

Because the C# API uses P/Invoke, for your application to use Berkeley DB successfully, the .NET framework needs to be able to locate the native libaries. This means the native libraries need to either be copied to your application's directory, the Windows or System directory, or the location of the libraries needs to be added to the PATH environment variable. See the MSDN documentation of the DllImport attribute and Dynamic-Link Library Search Order for further information.

If you get the following exception when you run, the .NET platform probably is unable to locate the native libraries:


To ensure that everything is running correctly, you may want to try a simple test from the example programs in the db-VERSION\examples_csharp directory.

For example, the ex_access sample program will prompt for text input lines, which are then stored in a Btree database named access.db. It is designed to be run from either the db-VERSION\build_windows\Debug or db-VERSION\build_windows\Release directory. Try giving it a few lines of input text and then a blank line. Before it exits, you should see a list of the lines you entered display with data items. This is a simple check to make sure the fundamental configuration is working correctly.


The Berkeley DB C# API has been tested with the Microsoft .NET Platform (version 2.0) on Windows.