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

发布于 2024-11-06 06:38:36 字数 602 浏览 0 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

辞取 2024-11-13 06:38:36

我写的,它对我有用

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 中的实体模型

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

音盲 2024-11-13 06:38:36

行序错误,应该是:

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

wrong line order, should be:

    @""" +
    @"Data Source=" +
凤舞天涯 2024-11-13 06:38:36

事实上,我很惊讶连接字符串居然能起作用。此外,使用 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 和实体框架 4

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

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

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))
{
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文