in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

How to use a relative database path in the connectionString

Last post 04-19-2009 9:28 AM by Schuck. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 03-02-2009 1:34 AM

    How to use a relative database path in the connectionString

     When you add a edmx file in a project Visual Studio add a new connectionstring.

    I have seen that the databse path is hard coded. I have tried a relative path but it doens't work.

    Some one has an idea for this issue ?

    He re is an  excerpt:

    connectionString="metadata=res://*;provider=System.Data.SQLite;provider connection string="data source=C:\Data\Side_1\DEV\DOTNET\_WEB\Distribution_v3\App_Data\distribution.db3""

     

    regards,

    Sam 

    Sam
  • 03-03-2009 11:10 PM In reply to

    • kogz
    • Not Ranked
    • Joined on 03-04-2009
    • Posts 1

    Re: How to use a relative database path in the connectionString

     You can create a connection with that connection string, and possibly run something on it, for example:

     

    string databaseFile = "stuff.sqlite";

     Entities ent = new Entities(String.Format("metadata=res://*;provider=System.Data.SQLite;provider connection string="data source={0}"", databaseFile));

     

    .. or something.

  • 03-11-2009 4:22 PM In reply to

    Re: How to use a relative database path in the connectionString

     The following works fine for SQLite (for Sql Server I have not tested) :

      public static string GetEntityConnection() {

            string metadata;


            // Initialize the connection string builder for the underlying provider.
            SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
            //Specific to Sql Server
            if (PROVIDER_NAME.ToUpper() == "System.Data.SqlClient".ToUpper()) {
                // Set the properties for the data source.
                sqlBuilder.DataSource = distributionDAL.DATASOURCE;
                sqlBuilder.InitialCatalog = distributionDAL.DATABASE_NAME;
                sqlBuilder.IntegratedSecurity = true;
                metadata = @"res://*/AdventureWorksModel.csdl|
                                res://*/AdventureWorksModel.ssdl|
                                res://*/AdventureWorksModel.msl";
            }
            else {/*Specific to SQLite*/
                if (!File.Exists(distributionDAL.DATABASE_PATH)) {
                    throw new Exception("The database file doesn't exists in [" + distributionDAL.DATABASE_PATH + "]");
                }
                // Set the properties for the data source.
                sqlBuilder.DataSource = distributionDAL.DATABASE_PATH;
                metadata = @"res://*";
            }
            // Initialize the EntityConnectionStringBuilder.
            EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
            //Set the provider name.
            entityBuilder.Provider = distributionDAL.PROVIDER_NAME;
            // Set the provider-specific connection string.
            entityBuilder.ProviderConnectionString = sqlBuilder.ToString(); ;
            // Set the Metadata location.
            entityBuilder.Metadata = metadata;

            return entityBuilder.ToString();
        }

     

    Sam 

    Sam
  • 04-19-2009 9:28 AM In reply to

    Re: How to use a relative database path in the connectionString

    If, for example, your database is located in the same directory as you application, your data source part of the connection string should look like this:

     data source=.\distribution.db3"

    This worked for me.

    As i added the ef model to my application, i clicked the "New Connection" button, and entered (without quotes) ".\database_name.db" in to the database field. I had no problems so far.

Page 1 of 1 (4 items)
Powered by Community Server (Commercial Edition), by Telligent Systems