C# - 从连接字符串获取文件路径
C# 中是否有现有方法可以从表示 ConnectionString
的字符串中提取文件路径到 SqlCE
.sdf 文件?我想在初始化时检查文件是否存在,如果文件被修改则备份它。
连接字符串示例:
strConn = "Data Source=|DataDirectory|\dbAlias.sdf";
Is there an existing method in C# to extract the file path from a string that represents a ConnectionString
to a SqlCE
.sdf file? I want to check if the file exists at initialization and back it up if the file has been modified.
Sample connection string:
strConn = "Data Source=|DataDirectory|\dbAlias.sdf";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 SqlCeConnectionStringBuilder 类来解析现有的 Sql Compact 连接字符串。
You can use SqlCeConnectionStringBuilder class to parse existing Sql Compact connection string.
也许有点晚了,但我在解决同样的问题时遇到了这个问题。您可以使用
AppDomain.CurrentDomain.GetData("DataDirectory")
查找|DataDirectory|
文件夹的位置。所以你的连接字符串可以这样翻译:A bit late perhaps, but I came across this question wile struggling with the same problem. You can find the location of the
|DataDirectory|
folder withAppDomain.CurrentDomain.GetData("DataDirectory")
. So your connectionstring can be translated like this:您可以只创建连接并从中获取数据源作为属性:
You could just create the connection and get the data source from it as a property:
对于 LocalDB 和 SqlConnection(不是 CE):
For LocalDB and SqlConnection (not CE):