VistaDB 6
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.vdb6";
    connStringBuilder.Password = "SuperSecretPassword";

    using (var connection = new VistaDBConnection(connStringBuilder.ConnectionString))
    {
        connection.Open();
    }
}

Using A Code Literal for a Connection String

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

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

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.vdb6'";

//OR..

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

//The @ symbol at the front of a string in C# means to take the entire string as a literal.

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.vdb6'";
            using( VistaDBConnection dbConn = new VistaDBConnection(connectionString) )
            {
                dbConn.Open();
                VistaDBCommand command = dbConn.CreateCommand();
                command.CommandText = "SELECT * FROM SAMPLETABLE";
                using (VistaDBDataReader dr = command.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        // Do something
                    }
                }
           }
        }
    }
}


See Also