Installing - .NET
There are two primary methods by which you can install the DataTables .NET libraries:
- From NuGet
- Direct inclusion
NuGet installation
A NuGet package is available that contains the DataTables .NET libraries to allow easy installation. If you are using a UI (e.g. in Visual Studio) to manage packages, search for DataTables-Editor-Server and install the matching package.
On the command line you can use:
dotnet add package DataTables-Editor-Server
Direct inclusion
Download the Editor .NET demo app from the Editor download page, extract the zip file and then copy the DLLs to your project, adding a reference to have the DLL imported.
Namespace
The .NET libraries for DataTables are available under the DataTables namespace - i.e.:
using DataTables;
Make sure you include this namespace when using the libraries!
Database connection
Once the library has been loaded into your project, the next step is to set up the database connection so the libraries can interact with the database server, reading and modified data as required. This is done through the Database class in the DataTables namespace, which is a database abstraction layer, so it can interface with multiple different database types.
You can create a Database class instance using the following code, which is typically created in each controller. This class uses ADO.NET connection pooling to efficiently connect to your database, while ensuring that each Ajax request is independent of other requests:
var settings = Properties.Settings.Default;
using (var db = new Database(settings.DbType, settings.DbConnection))
{
...
}
A using() statement is used to ensure that the database connection is correctly closed and disposed of after the controller has finished using it.
The database constructor accepts two parameters:
DbType- This defines the database type you are connecting to and can be one of:azure- Microsoft Azuremysql- MySQLpostgres- PostgreSQLsqlite- SQLitesqlserver- SQL Serversqlserverce- SQL Server CE (for local file databases)
DbConnection- This is the database connection string to connect to the SQL server. The exact format of the connection string depends upon the server being used, but typically it includes the server host name / address, user name and password. The Connection Strings web-site can be useful if you aren't sure of what connection string to use for your server.
It is worth noting that the Database class has a number of overrides available to allow initialisation with different configurations (refer to the Editor .NET API reference documentation for full details):
new Database(string, string)new Database(string, DbConnectionStringBuilder)new Database(string, DbConnection)
In the above code, you'll notice that two application properties are used. These are defined in the project's settings and can be reused throughout your application, referenced and managed from a singe location, should you need to change them later. To modify these properties, double click the Settings.settings file in the application Properties option (in the Solution Explorer of Visual Studio). You can store these details else where, as required by your project.
.NET paradigms
There a couple of key programming concepts that the DataTables .NET libraries make use of and it is important to understand them in this context. If you are already comfortable with the ideas of chaining and namespaces in .NET skip this section, otherwise read on for a summary of these .NET features, as they are used above and also in the Editor examples.
Chaining
Like the Editor Javascript API the .NET Editor API is fully chainable, allowing potentially complex code to be expressed succinctly. The majority of the methods that Editor and its child classes provide are chainable - i.e. they return the instance that you are working with. Consider for example:
var editor = new Editor( Db, 'staff' );
editor.Fields( ... );
editor.Process( ... );
DtResponse data = editor.Data();
Using chained style, the above code block can be rewritten as:
DtResponse data = new Editor( Db, 'staff' )
.Fields( ... )
.Process( ... )
.Data();
Using a chained style of coding is not required, although you will see it used in this documentation and the examples Editor comes with.
Namespaces
As noted above, the .NET libraries for DataTables are available under the DataTables namespace. This is to ensure separation between different libraries which might provide classes with the same name (e.g. DataTable and Editor are not uncommon names!).
using DataTables;
Make sure you include this namespace when using the libraries!
Sources
If you are interested in building the .NET libraries yourself, you can find the source on Github. The DataTables .NET server-side source is released under the MIT license.