Connect Application to Microsoft SQL Databases
The C3 Agentic AI Platform has a built-in connector for integrating with Microsoft SQL (MSSQL) databases.
To connect to MSSQL from your application:
- Add a SqlSourceSystem modeling the MSSQL source system to your package.
- Configure the JdbcCredentials authorizing the connection to the external MSSQL table.
- Add a SqlSourceCollection modeling the target MSSQL table to your package.
- Create an External Type modeling the schema of the external MSSQL table.
The following sections include detailed instructions for configuring the connection.
For more information, see the MSSQL documentation.
Model the source system
Create a SqlSourceSystem and set the name field as the identifier for the external database system.
For example, you can add the following MSSQLSourceSystem.json to the \metadata\SqlSourceSystem directory of your package:
{
"name": "MSSQLSourceSystem"
}Configure the credential used to authorize the JDBC connection
Create a JdbcCredentials Type instance to configure the connection to the external MSSQL table, passing the following fields to the JdbcCredentials.fromServerEndpoint() method:
serverEndpoint— MSSQL server hostnameport— MSSQL server portdatastoreType— Specifies that the JdbcCredentials authorizes a connection to MSSQLdatabase— The name of the MSSQL database that you are connecting tousername— The account with authorization to access the MSSQL database tablepassword— The password for this account
For example, run the following from console to configure the JdbcCredentials:
var creds = JdbcCredentials.fromServerEndpoint("HOST",
PORT,
DatastoreType.MSSQL,
"SOME_DB",
"SCHEMANAME",
"some_username",
"some_password");
JdbcStore.forName("MSSQLSourceSystem").setCredentials(creds, ConfigOverride.APP);
JdbcStore.forName("MSSQLSourceSystem").setExternal(ConfigOverride.APP);Model the table containing the data
To model the external MSSQL table in your application, create a SqlSourceCollection and set the following fields:
name— Identifier for the MSSQL tablesource— Name of the External Type that models the schema of the external MSSQL tablesourceSystem— Name of the MSSQL SqlSourceSystem
For example, to model a table called insurance_risk, you can add the following MSSQLTable.json to the \metadata\SqlSourceCollection directory of your package:
{
"name" : "InsuranceDataMSSQL",
"source" : "InsuranceDataMSSQL",
"sourceSystem" : {
"type" : "SqlSourceSystem",
"name" : "MSSQLSourceSystem"
}
}Model the table schema
To model the schema of the MSSQL table in your application, create an External Entity Type with a schema name that matches the name of the table in the MSSQL table, as it is case-sensitive.
Start by adding the following InsuranceDataMSSQL.c3typ file to the \src directory of your package:
entity type InsuranceDataMSSQL mixes External, NoSystemCols schema name 'insurance_risk'You can use the inferSourceType() method to access the C3 AI data types of the table, which the C3 Agentic AI Platform infers from the source data types.
var schema = SqlSourceCollection.forName("InsuranceDataMSSQL").inferSourceType().declaredFieldTypes;
var myObject = {};
for (let i = 0; i < schema.length; i++) {
schemaName = schema[i].schemaName;
myObject[schemaName] = schema[i].valueType.name;
}
MSSQL data types are mapped to C3 AI data types according to the following table. See also PrimitiveType.
| MSSQL Data Types | C3 AI Data Types |
|---|---|
| bigint | bigint |
| int | int, int32 |
| smallint, tinyint | int16 |
| numeric | decimal |
| real | float |
| float | double |
| char, text, varchar, nchar, ntext, nvarchar | string |
| binary, varbinary | binary |
| date, smalldatetime, datetime, datetime2, time | datetime |
You can also access the source data types to validate the type inference:
SqlSourceCollection.forName("InsuranceDataMSSQL").connect().columns;
Complete the External Entity Type definition:
entity type InsuranceDataMSSQL mixes External, NoSystemCols schema name "insurance_risk" {
id: ~ schema name "Id"
firstName: string schema name "First_Name"
lastName: string schema name "Last_Name"
age: int schema name "Age"
income: int schema name "Income"
annualMileage: int schema name "Annual_Mileage"
creditScore: int schema name "Credit_Score"
debtToAssetRatio: double schema name "Debt_to_Asset_Ratio"
riskScore: int schema name "Risk_Score"
}Note that the id field is required. If your table does not have a column called id, you can change the schema name for the corresponding id field with the following annotation:
@db(dataTypeOverride="ID_FIELD_DATA_TYPE")
id: ~ schema name "ID_FIELD"Read data from the table
After completing the External Entity Type definition, you can validate the configuration by fetching the External Type data from the MSSQL table:
c3Grid(InsuranceDataMSSQL.fetch());