Using Database Connections with gsqlcmd
gsqlcmd
requires a database connection as the first positional parameter in database-related modes.
You can specify the connection using:
- Database connection strings
- Named connections
- File names
Connection String Example
Here's how to use a connection string:
gsqlcmd exec "System.Data.OleDb;Provider=MSOLEDBSQL;Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=SSPI" "select name from sys.databases"
You can also pass connection strings via environment variables. For example:
set connection="Provider=MSOLEDBSQL;Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=SSPI" gsqlcmd %connection% "select name from sys.databases"
Named Connection Example
To use a named connection, you can do the following:
gsqlcmd exec master "select name from sys.databases"
File Name Example
You can also connect using a file name:
gsqlcmd exec test.xlsx "SELECT * FROM [sheet1$]"
For more details, see the connection parameter description.
Creating Named Connections
You can easily create named connections using the edit-connections command:
gsqlcmd edit-connections /local
Use the /local, /user, or /app option to edit connections in the specified configuration file.
This command launches gConnectionManager, which allows you to create, edit, copy, and delete connection strings in a visual interface:
Listing Connections
To list defined connection string names, use the show-connections mode:
gsqlcmd show-connections
You can filter connections by specifying the configuration file with the /local, /user, or /app option. For example:
gsqlcmd show-connections /user
Filtering by Platform
You can also filter connections by platform using the following options:
For example:
gsqlcmd show-connections /app /oracle
Showing Connection Properties
To view the properties of a specific connection, use the show-connection mode:
gsqlcmd show-connection master
You can query specific connection properties like this:
gsqlcmd show-connection test.xlsx connectionString
Showing Available Data Providers
To list available data providers, use the show-providers mode:
gsqlcmd show-providers /excel
Note that Windows has separate 32-bit and 64-bit data providers. Use gsqlcmd32.exe
to work with 32-bit providers and connection strings on 64-bit Windows. For example:
gsqlcmd32 show-providers /excel