在运行时更改类应用程序中的连接字符串

时间:2011-05-08 16:05:56

标签: c# sqlite ado.net

我有使用ado.net连接到Sqlite数据库的类应用程序。应用程序使用db来存储一些数据,并且可以在运行时更改db。用户可以备份数据库并更改位置,但在这种情况下,我需要知道如何更改连接字符串。 我尝试过这段代码但是没有用:

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);

此错误“不支持关键字:'数据源'。” 发生在这一行

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

3 个答案:

答案 0 :(得分:1)

我写这个并且它与我一起工作

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是edmx ado.net中的实体模型

答案 1 :(得分:0)

错误的行顺序,应该是:

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

答案 2 :(得分:0)

我真的很惊讶连接字符串的工作原理。此外,使用string.Format构建此连接字符串会更简单:

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 ) )
{
    ...
}

首先,您将使用适当的设置值替换UseUTF16Encoding。其次,请注意连接字符串中的文件路径未被引号括起。


如果您正在寻找在运行时交换Sqlite数据文件的方法,您可以查看此博客条目:

SQLite and Entity Framework 4

解决方案的摘要是解析Entity框架连接字符串,更改数据文件然后重置它:

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;
}

要使用,您可以执行以下操作:

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