在运行时更改类应用程序中的连接字符串?
我有使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我写的,它对我有用
Dm ent 是 edmx ado.net 中的实体模型
i write that and it worked with me
Dm ent is the entity model in edmx ado.net
行序错误,应该是:
wrong line order, should be:
事实上,我很惊讶连接字符串居然能起作用。此外,使用 string.Format 构建此连接字符串会更简单:
首先,您可以将 UseUTF16Encoding 替换为适合您的设置的值。其次,请注意连接字符串中的文件路径没有用引号引起来。
如果您正在寻找一种在运行时交换 Sqlite 数据文件的方法,您可以查看此博客条目:
SQLite 和实体框架 4
解决方案摘要是解析实体框架连接字符串,更改数据文件,然后重置它:
要使用,您需要执行以下操作:
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:
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:
To use, you would do something like: