Hello,

Today we learn how to configure connections and execute SQL queries.

Now, please, open the command prompt (Win-R, cmd, Enter) and type: gsqlcmd userconfig

This command starts a visual editor of connection strings, gConnectionManager:

You may see the configuration file path in the status bar.

Click the ... button in the last column. You will see a list of available providers:

Select, for example, SqlClient Data Provider to connect to Microsoft SQL Server and click Next.

In this step, specify a server, database, and your credentials to connect.

You may click the Examples link to get connection examples.

Select your existing database and click Finish.

You will see a new connection string:

Rename the string. In this example, it has a name as the connected database, SkypeReports.

Click the Save button and close the wizard.

Now you may check available connections. Type: gsqlcmd connections

In this example, you may see a created connection string in the user configuration file.

There are three configuration files available: AppConfig, UserConfig, and LocalConfig.

Use gsqlcmd localconfig to edit connection strings specific to a folder.

Now, let's execute the query against the created connection.

Type, for example: gsqlcmd SkypeReports "SELECT 'Hello, World!'"

You will see the result:

Let's export data to CSV using the /ascsv option.

Type, for example: gsqlcmd SkypeReports "SELECT 1 id, 'Hello, World!' name" /ascsv

The common structure of the default execute command:

gsqlcmd [execute] <connection name> <query>|<input file> [<output file>] [options]

For example, a batch file may contain the following command:

gsqlcmd execute SkypeReports input.sql output.csv /ascsv

Today you have learned how to create named connections and execute SQL queries.

You may use the same technique for any supported database platform.

You may easily export data to CSV. Just add the /ascsv option.

Best regards,

Sergey Vaselenko
CEO | Gartle Technology Corporation

This website is using cookies. By continuing to browse, you give us your consent to our use of cookies as explained in our Cookie Policy.