Opening and closing the database connection (ADO.NET)

Before you can access data in Vertica through ADO.NET, you must create a connection to the database using the VerticaConnection class which is an implementation of System.Data.DbConnection.

Before you can access data in Vertica through ADO.NET, you must create a connection to the database using the VerticaConnection class which is an implementation of System.Data.DbConnection. The VerticaConnection class takes a single argument that contains the connection properties as a string. You can manually create a string of property keywords to use as the argument, or you can use the VerticaConnectionStringBuilder class to build a connection string for you.

To download the ADO.NET driver, go to the Client Drivers Downloads page.

This topic details the following:

  • Manually building a connection string and connecting to Vertica

  • Using VerticaConnectionStringBuilder to create the connection string and connecting to Vertica

  • Closing the connection

To manually create a connection string:

See ADO.NET connection properties for a list of available properties to use in your connection string. At a minimum, you need to specify the Host, Database, and User.

  1. For each property, provide a value and append the properties and values one after the other, separated by a semicolon. Assign this string to a variable. For example:

    String connectString = "DATABASE=VMart;HOST=v_vmart_node0001;USER=dbadmin";
    
  2. Build a Vertica connection object that specifies your connection string.

    VerticaConnection _conn = new VerticaConnection(connectString)
    
  3. Open the connection.

    _conn.Open();
    
  4. Create a command object and associate it with a connection. All VerticaCommand objects must be associated with a connection.

    VerticaCommand command = _conn.CreateCommand();
    

To use the VerticaConnectionStringBuilder class to create a connection string and open a connection:

  1. Create a new object of the VerticaConnectionStringBuilder class.

    VerticaConnectionStringBuilder builder = new VerticaConnectionStringBuilder();
    
  2. Update your VerticaConnectionStringBuilder object with property values. See ADO.NET connection properties for a list of available properties to use in your connection string. At a minimum, you need to specify the Host, Database, and User.

    builder.Host = "v_vmart_node0001";
    builder.Database = "VMart";
    builder.User = "dbadmin";
    
  3. Build a Vertica connection object that specifies your connection VerticaConnectionStringBuilder object as a string.

    VerticaConnection _conn = new VerticaConnection(builder.ToString());
    
  4. Open the connection.

    _conn.Open();
    
  5. Create a command object and associate it with a connection. All VerticaCommand objects must be associated with a connection.

    VerticaCommand command = _conn.CreateCommand;
    

To close the connection:

When you're finished with the database, close the connection. Failure to close the connection can deteriorate the performance and scalability of your application. It can also prevent other clients from obtaining locks.

 _conn.Close();

Example usage:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Vertica.Data.VerticaClient;
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            VerticaConnectionStringBuilder builder = new VerticaConnectionStringBuilder();
            builder.Host = "192.168.1.10";
            builder.Database = "VMart";
            builder.User = "dbadmin";
            VerticaConnection _conn = new VerticaConnection(builder.ToString());
            _conn.Open();
        //Perform some operations
            _conn.Close();
        }
    }
}