C# 根据连接字符串确定是否存在SQL Server CE 4.0数据库文件

C# 根据连接字符串确定是否存在SQL Server CE 4.0数据库文件,c#,connection-string,C#,Connection String,给定以下连接字符串(对于SQL Server CE 4.0) 数据源=|数据目录| IntegrationTests.sdf 如何确定该文件是否存在 (我知道我可以在调用File.Exists()时硬编码路径,但我不想这样做。如果我决定更改连接字符串怎么办?也许可以使用该类将连接字符串解析为其组件: class Program { static void Main() { // Create a new SqlConnectionStringBuilder and

给定以下连接字符串(对于SQL Server CE 4.0)

数据源=|数据目录| IntegrationTests.sdf 如何确定该文件是否存在

(我知道我可以在调用
File.Exists()
时硬编码路径,但我不想这样做。如果我决定更改连接字符串怎么办?

也许可以使用该类将连接字符串解析为其组件:

class Program
{
    static void Main()
    {
        // Create a new SqlConnectionStringBuilder and
        // initialize it with a few name/value pairs.
        SqlConnectionStringBuilder builder =
            new SqlConnectionStringBuilder(GetConnectionString());

        // The input connection string used the 
        // Server key, but the new connection string uses
        // the well-known Data Source key instead.
        Console.WriteLine(builder.ConnectionString);

        // Pass the SqlConnectionStringBuilder an existing 
        // connection string, and you can retrieve and
        // modify any of the elements.
        builder.ConnectionString = "server=(local);user id=ab;" +
            "password= a!Pass113;initial catalog=AdventureWorks";

        // Now that the connection string has been parsed,
        // you can work with individual items.
        Console.WriteLine(builder.Password);
        builder.Password = "new@1Password";
        builder.AsynchronousProcessing = true;

        // You can refer to connection keys using strings, 
        // as well. When you use this technique (the default
        // Item property in Visual Basic, or the indexer in C#),
        // you can specify any synonym for the connection string key
        // name.
        builder["Server"] = ".";
        builder["Connect Timeout"] = 1000;
        builder["Trusted_Connection"] = true;
        Console.WriteLine(builder.ConnectionString);

        Console.WriteLine("Press Enter to finish.");
        Console.ReadLine();
    }

    private static string GetConnectionString()
    {
        // To avoid storing the connection string in your code,
        // you can retrieve it from a configuration file. 
        return "Server=(local);Integrated Security=SSPI;" +
            "Initial Catalog=AdventureWorks";
    }
}
在这种情况下,您可以使用
数据源
属性:

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder("Data Source=|DataDirectory|IntegrationTests.sdf");
Console.WriteLine(builder.DataSource);

对于Sql Server Express edition,使用| DataDirectory |似乎会自动触发对文件存在性的检查,如果文件不存在,甚至会自动创建文件。不确定Sql Server CE是否也是如此。 以下是我能找到的唯一(旧)参考资料:

但是,如果您想将| DataDirectory |解析为实际路径并自己进行检查,您可以使用:

AppDomain.CurrentDomain.getData(“DataDirectory”);

这将是正确的答案: 使用此正则表达式分析connectionstring:

@"Data\sSource(\s)*=(\s)*(?<DataSourceName>([^;]*))"
@“数据源(\s)*=(\s)*(?([^;]*)”
接受的答案对于某些有效的sql ce连接字符串无效,因为它将连接字符串生成器用于非ce sql连接

下面是提取它的C代码:

    Regex regex = new Regex(@"Data\sSource(\s)*=(\s)*(?<DataSourceName>([^;]*))");
    string file = regex.Matches(TheConnectionString).Cast<Match>().Select(x => x.Groups["DataSourceName"].Captures[0].ToString().Trim('\'')).FirstOrDefault();
    return File.Exists(file);
Regex Regex=new Regex(@“数据源(\s)*=(\s)*=(?([^;]*))”;
字符串文件=regex.Matches(连接字符串).Cast().Select(x=>x.Groups[“DataSourceName”]”。捕获[0]。ToString().Trim('\'')。FirstOrDefault();
返回文件。存在(文件);

结果证明我无论如何都不能使用SQL Server CE,因为嵌套事务不受支持(而且我无法控制处理事务的代码)。。。但这肯定能完成任务=)这行不通,请参阅我的答案,以获得更可靠的解决方案。有人不喜欢这个答案,可能是因为问题与一般情况无关。但是,不要被愚弄了:这仍然是一般情况下的正确答案。
    Regex regex = new Regex(@"Data\sSource(\s)*=(\s)*(?<DataSourceName>([^;]*))");
    string file = regex.Matches(TheConnectionString).Cast<Match>().Select(x => x.Groups["DataSourceName"].Captures[0].ToString().Trim('\'')).FirstOrDefault();
    return File.Exists(file);