VistaDB 5
Gibraltar VistaDB > Getting Started > Using VistaDB with Entity Framework > Connection Strings > Using VistaDB with ADO.NET - Using Connection Strings in Code
Using VistaDB with ADO.NET - Using Connection Strings in Code

Creating a Connection String in Code

You don't have to put a literal connection string in your application - you can use the VistaDB Connection String Builder class to build up the connection string from the specific options you want.  This can be particularly convenient when working with encrypted databases or where a user can provide their own path to the database so you don't have to worry about combining the various elements correctly with escape codes.

public void CreateConnectionString()
    VistaDBConnectionStringBuilder connStringBuilder = new VistaDBConnectionStringBuilder();
    connStringBuilder.DataSource = "MyDatabase.vdb5";
    connStringBuilder.Password = "SuperSecretPassword";

    using (var connection = new VistaDBConnection(connStringBuilder.ConnectionString))
Public Sub CreateConnectionString()
     Dim connStringBuilder As New VistaDBConnectionStringBuilder()
     connStringBuilder.DataSource = "MyDatabase.vdb5"
     connStringBuilder.Password = "SuperSecretPassword"

     Using connection = New VistaDBConnection(connStringBuilder.ConnectionString)
     End Using
End Sub

Using A Code Literal for a Connection String

Let's say we want to load c:\temp\database.vdb5 in our application.

data source='c:\temp\database.vdb5' 

When specifying a path in your application you typically need to tell the compiler to not threat each backslash (\) as the start of a control character sequence but instead as a literal backslash.  This is done by adding a second backslash, like show in the following examples:

string myconnectionstring = "data source='c:\\temp\\database.vdb5'"; 


string myconnectionstring = @"data source='c:\temp\database.vdb'"; 

//The @ symbol at the front of a string in C# means to take the entire string as a literal. 
Dim myconnection as string myconnection = "data source='c:\\temp\\database.vdb5'" 

Connecting In Code Example

And the complete code with the connection object looks like this:

using System;
using System.Text;
using VistaDB.Provider;
namespace TestSQLPools
    class Program
        static void Main(string[] args)
            string connectionString = "data source='c:\\temp\\database.vdb5'";
            using( VistaDBConnection dbConn = new VistaDBConnection(connectionString) )
                VistaDBCommand command = dbConn.CreateCommand();
                command.CommandText = "SELECT * FROM SAMPLETABLE";
                using (VistaDBDataReader dr = command.ExecuteReader())
                    while (dr.Read())
                        // Do something
Imports VistaDB.Provider

Module Module1

    Sub Main()

        Dim connectionString As String
        connectionString = "data source='c:\\temp\\database.vdb5'"

        ' Using is a shortcut to tear down the object when it falls out of scope
        Using dbConn As VistaDBConnection = New VistaDBConnection(connectionString)


            Dim command As VistaDBCommand = dbConn.CreateCommand()
            command.CommandText = "SELECT * FROM SAMPLETABLE"

            Dim dr As VistaDBDataReader = command.ExecuteReader()

            While dr.Read
                'DO SOMETHING
            End While
        End Using

    End Sub

End Module

See Also