> For the complete documentation index, see [llms.txt](https://docs.wisej.com/ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.wisej.com/ai/components/api/tools/wisej.ai.tools.databasetools.md).

# DatabaseTools

Namespace: **Wisej.AI.Tools**

Assembly: **Wisej.AI** (3.5.0.0)

* [ToolsContainer](/ai/components/api/tools/wisej.ai.tools.toolscontainer.md)
  * [DatabaseTools](/ai/components/api/tools/wisej.ai.tools.databasetools.md)

Represents a set of tools for interacting with a database, including schema initialization and SQL execution.

{% tabs %}
{% tab title="C#" %}

```csharp
public class DatabaseTools : ToolsContainer
```

{% endtab %}

{% tab title="VB.NET" %}

```visual-basic
Public Class DatabaseTools
    Inherits ToolsContainer
```

{% endtab %}
{% endtabs %}

This class provides functionalities to manage database connections, retrieve schema information, and execute SQL queries. It utilizes the [ITokenizerService](/ai/components/api/services/itokenizerservice.md) to truncate the generated context to fit within the [MaxContextTokens](#maxcontexttokens) value.\
By overriding the virtual properties in this class, you can provide predefined values for connection, schema, columns, and server type. This allows for greater flexibility and customization in how the class is used.

## Constructors

### ![](/files/ptrKjmmRoQB76pvrIqh0) DatabaseTools(connection, columns, schema, serverType)

Initializes a new instance of the [DatabaseTools](/ai/components/api/tools/wisej.ai.tools.databasetools.md) class with optional parameters.

| Name           | Type                                                                                  | Description                                                      |
| -------------- | ------------------------------------------------------------------------------------- | ---------------------------------------------------------------- |
| **connection** | [DbConnection](https://docs.microsoft.com/dotnet/api/system.data.common.dbconnection) | The database connection to use. Default is `null`.               |
| **columns**    | [DataTable](https://docs.microsoft.com/dotnet/api/system.data.datatable)              | The data table containing column information. Default is `null`. |
| **schema**     | [String](https://docs.microsoft.com/dotnet/api/system.string)                         | The schema of the database. Default is `null`.                   |
| **serverType** | [String](https://docs.microsoft.com/dotnet/api/system.string)                         | The type of the database server. Default is `null`.              |

You can provide the schema in three ways:

* Provide the *columns* data table, which can be retrieved using `connection.GetSchema("Columns")`.
* Provide the *schema* string, formatted in a way that is understandable by the LLM.
* If both parameters are null, you can override either the [Columns](#columns) or the [Schema](#schema) properties.

## Properties

### ![](/files/ptrKjmmRoQB76pvrIqh0) Columns

[DataTable](https://docs.microsoft.com/dotnet/api/system.data.datatable): Gets the data table containing column information. (Default: `null`)

### ![](/files/ptrKjmmRoQB76pvrIqh0) Connection

[DbConnection](https://docs.microsoft.com/dotnet/api/system.data.common.dbconnection): Gets the database connection. (Default: `null`)

### ![](/files/ptrKjmmRoQB76pvrIqh0) MaxContextTokens

[Int32](https://docs.microsoft.com/dotnet/api/system.int32): Gets or sets the maximum number of context tokens. (Default: `4096`)

### ![](/files/ptrKjmmRoQB76pvrIqh0) Schema

[String](https://docs.microsoft.com/dotnet/api/system.string): Gets the schema of the database. (Default: `null`)

### ![](/files/ptrKjmmRoQB76pvrIqh0) ServerType

[String](https://docs.microsoft.com/dotnet/api/system.string): Gets the type of the database server.

### ![](/files/KL0Ik37djZRr8a07Wopd) TokenizerService

[ITokenizerService](/ai/components/api/services/itokenizerservice.md): Gets or sets the tokenizer service used for truncating context tokens.

## Methods

### ![](/files/KL0Ik37djZRr8a07Wopd) BuildDatabaseSchema(columns)

Builds a database schema representation from the provided columns.

| Parameter   | Type                                                                     | Description                                   |
| ----------- | ------------------------------------------------------------------------ | --------------------------------------------- |
| **columns** | [DataTable](https://docs.microsoft.com/dotnet/api/system.data.datatable) | The data table containing column information. |

**Returns:** [String](https://docs.microsoft.com/dotnet/api/system.string). A string representation of the database schema.

If the columns parameter is null, the method returns "Missing columns schema".

### ![](/files/KL0Ik37djZRr8a07Wopd) GetSQL(message)

Returns the JSON string returned in the *message* by stripping the enclosing markers (`sql and` ) if present.

| Parameter   | Type                                                                        | Description                                               |
| ----------- | --------------------------------------------------------------------------- | --------------------------------------------------------- |
| **message** | [Message](/ai/components/api/smartsession/wisej.ai.smartsession.message.md) | Message with the response text that may be a JSON string. |

**Returns:** [String](https://docs.microsoft.com/dotnet/api/system.string). JSON string.

### ![](/files/KL0Ik37djZRr8a07Wopd) select(sql)

Executes a SQL select query and returns the result as a string.

| Parameter | Type                                                          | Description               |
| --------- | ------------------------------------------------------------- | ------------------------- |
| **sql**   | [String](https://docs.microsoft.com/dotnet/api/system.string) | The SQL query to execute. |

**Returns:** [String](https://docs.microsoft.com/dotnet/api/system.string). The result of the SQL query as a string.

## Implements

| Name                                                                          | Description                                                                                           |
| ----------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| [IToolsContainer](/ai/components/api/tools/wisej.ai.tools.itoolscontainer.md) | Represents a container for tools, providing access to a hub, adapter, and a collection of parameters. |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.wisej.com/ai/components/api/tools/wisej.ai.tools.databasetools.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
