2

I have class application that uses ado.net to connect to Sqlite database. The application uses the db to store some data and the db may be changed at run time. The user may make backups of the db and change the location, but in this case i need to know how to change the connection string. I have tried this code but it didn't work:

string conn =
    @"metadata=res://*/KzDm.csdl|res://*/KzDm.ssdl|res://*/KzDm.msl;" +
    @"provider=System.Data.SQLite;" +
    @"provider connection string=" +
    @""" +@"Data Source=" +
    @"F:\My Own programs\KrarZara2\KZ\KZ\Kzdb.s3db" +
    @""";

Entities ent = new
Entities(conn);

this error "Keyword not supported: 'data source'." happen at this line

 public Entities(string connectionString) : base(connectionString, "Entities")

3 Answers 3

2

i write that and it worked with me

EntityConnectionStringBuilder conn = new EntityConnectionStringBuilder();
        conn.Metadata = @"res://*/KzDm.csdl|res://*/KzDm.ssdl|res://*/KzDm.msl";
        conn.Provider = "System.Data.SQLite";
        conn.ProviderConnectionString = @"data source=F:\My Own programs\KrarZara2\KZ\KZ\KrarDS.krar;Version=3;";
        EntityConnection entity = new EntityConnection(conn.ConnectionString);
        using (DmEnt ent = new DmEnt(entity))
        {
            var parcel = ent.Parcels.SingleOrDefault(d => d.id == 1);
            var pparcc = ent.Parcels.Select(d => d.id == 2);
            Parcel r = new Parcel();
            r.ParcelNumber = "11ju";
            r.Area = 8787;

            ent.AddToParcels(r);
            ent.SaveChanges();
        }

Dm ent is the entity model in edmx ado.net

Sign up to request clarification or add additional context in comments.

Comments

0

wrong line order, should be:

    @""" +
    @"Data Source=" +

1 Comment

Now he give me another error Keyword not supported: 'data source'. i'll change the question
0

I'd actually surprised that connection string works at all. In addition, it would be simpler to use string.Format to build this connection string:

var filename = @"F:\My Own programs\KrarZara2\KZ\KZ\Kzdb.s3db";
var connString = string.Format("Data Source={0};UseUTF16Encoding=True;", filename );

using( var conn = new SQLiteConnection( connString ) )
{
    ...
}

First, you would replace UseUTF16Encoding with the proper value for your setup. Second, note that the file path in the connection string is not surrounded by quotation marks.


If you are looking for a means to swap Sqlite data files at runtime you might look at this blog entry:

SQLite and Entity Framework 4

A summary of the solution is to parse the Entity framework connection string, change the data file and then reset it:

public static string RedirectedEntityFrameworkConnectionString(string originalConnectionString, string databaseFile, string password)
{
    // Parse the Entity Framework connection string.
    var connectionStringBuilder = new EntityConnectionStringBuilder(originalConnectionString);
    if (connectionStringBuilder.Provider != "System.Data.SQLite")
    {
        throw new ArgumentException("Entity Framework connection string does not use System.Data.SQLite provider.");
    }

    // Parse the underlying provider (SQLite) connection string.
    var providerConnectionStringBuilder = new SQLiteConnectionStringBuilder(connectionStringBuilder.ProviderConnectionString);

    // Redirect to the specified database file, and apply encryption.
    providerConnectionStringBuilder.DataSource = databaseFile;
    providerConnectionStringBuilder.Password = password;

    // Rebuild the Entity Framework connection string.
    connectionStringBuilder.ProviderConnectionString = providerConnectionStringBuilder.ConnectionString;
    return connectionStringBuilder.ConnectionString;
}

To use, you would do something like:

const string OriginalConnectionString = "..."; // (Copy out of app.config)
var connectionString = RedirectedEntityFrameworkConnectionString(OriginalConnectionString, myFileName, null);
using (var context = new MyEntities(connectionString))
{
    ...
}

4 Comments

the same erro but at this point var connectionStringBuilder = new EntityConnectionStringBuilder(originalConnectionString);
@Khalid Omar - Have you verified that you can use your connection string manually? I.e., whip up a small project and plug in your connection string to connect to your db?
yes i used it manually and it is ok if the old connection string that in app.config the application goes ok when i change the connstr the proplem comes
@Khalid Omar - Did you try the code from the link I added above?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.