Configuring SqlCli
To connect and interact with a SQL database, an SqlCli object must be configured with two key pieces of information:
- The driver (which database to connect to)
- The connection string (how to connect to the database)
Set the driver using the Driver() method and the connection string using the ConnString() method.
A list of supported drivers is available. Below are details and examples for each driver, including how to build a suitable connection string.
Driver: n1ql (Couchbase)
- Standalone:
hostname:port(e.g.,localhost:8093) - Cluster: Full URL (e.g.,
http://localhost:9000/)
var cli = new SqlCli();
cli.Driver("n1ql");
cli.ConnString("localhost:8093");
if (cli.Connect()) {
// Perform SQL tasks...
cli.Close();
}Driver: firebirdsql (FirebirdSQL)
Format: user:password@servername/foo/bar.fdb
var cli = new SqlCli();
cli.Driver("firebirdsql");
cli.ConnString("user:password@servername/foo/bar.fdb");
if (cli.Connect()) {
// Perform SQL tasks...
cli.Close();
}Driver: bigquery (Google BigQuery)
Format: bigquery://projectid/location/dataset
var cli = new SqlCli();
cli.Driver("bigquery");
cli.ConnString("bigquery://projectid/location/dataset");
if (cli.Connect()) {
// Perform SQL tasks...
cli.Close();
}Driver: mssql (MS SQL Server)
Format: sqlserver://username:password@host/instance?param1=value¶m2=value
- For SQLExpress:
sqlserver://sa@localhost/SQLExpress?database=master&connection+timeout=30 - For custom port:
sqlserver://sa:mypass@localhost:1234?database=master&connection+timeout=30
NOTE
URL-encode all parts of the connection string as it must be a valid URL.
var cli = new SqlCli();
cli.Driver("mssql");
cli.ConnString("sqlserver://sa:mypass@localhost?database=master&connection+timeout=30");
if (cli.Connect()) {
// Perform SQL tasks...
cli.Close();
}Driver: mysql (MySQL/MariaDB)
Format: user:password@(hostname_or_ipaddr:port)/dbname
var cli = new SqlCli();
cli.Driver("mysql");
cli.ConnString("user:pass@(localhost:3306)/testdb");
if (cli.Connect()) {
// Perform SQL tasks...
cli.Close();
}Driver: oracle (Oracle)
Format: user/pass@hostname_or_ipaddr:port/database
var cli = new SqlCli();
cli.Driver("oracle");
cli.ConnString("user/pass@localhost:1521/mydb");
if (cli.Connect()) {
// Perform SQL tasks...
cli.Close();
}Driver: pg (Postgres)
Format: host=... port=... user=... password=... dbname=... sslmode=...
host: Hostname or IP addressport: Port (default 5432)user: Database usernamepassword: Database passworddbname: Database namesslmode:enableordisable
var cli = new SqlCli();
cli.Driver("pg");
cli.ConnString("host=localhost port=5432 user=your_db_user password=your_db_password dbname=db_name sslmode=disable");
if (cli.Connect()) {
// Perform SQL tasks...
cli.Close();
}Driver: sqlite (SQLite)
- In-memory:
:memory: - File-based: Fully qualified path to the database file
// In-memory SQLite DB
var cli = new SqlCli();
cli.Driver("sqlite");
cli.ConnString(":memory:");
if (cli.Connect()) {
// Perform SQL tasks...
cli.Close();
}
// File-based SQLite DB
var cli = new SqlCli();
cli.Driver("sqlite");
cli.ConnString("/home/someuser/mydata.sqlite");
if (cli.Connect()) {
// Perform SQL tasks...
cli.Close();
}