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

时间:2011-07-15 08:48:36

标签: c# connection-string

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

Data Source=|DataDirectory|IntegrationTests.sdf

如何确定文件是否存在?

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

3 个答案:

答案 0 :(得分:0)

也许使用SqlConnectionStringBuilder类将您的连接字符串解析为其组件:

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

在这种情况下,您可以使用DataSource属性:

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

答案 1 :(得分:0)

对于Sql Server Express版,似乎使用了| DataDirectory |如果文件不存在,则自动触发检查文件是否存在甚至创建。不确定Sql Server CE是否也是如此。 这是我能找到的唯一(旧)参考:http://msdn.microsoft.com/en-us/library/aa478948.aspx

如果要解析| DataDirectory |根据您的实际路径并自行进行检查,您可以使用:

AppDomain.CurrentDomain.getData(“DataDirectory”);

答案 2 :(得分:0)

这是正确的答案: 使用此正则表达式来解析连接字符串:

@"Data\sSource(\s)*=(\s)*(?<DataSourceName>([^;]*))"

接受的答案对某些有效的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);