开发者

Figure out if a SQL Server CE 4.0 database file exists based on connection string

Given the following connection string (for SQL Server CE 4.0)

Data Source=|DataDirectory|IntegrationTests.sdf

how do I开发者_JAVA百科 figure out if the file exists?

(I know I can hard-code the path in a call to File.Exists(), but I don't want to. What if I decide to change the connection string?)


Perhaps use the SqlConnectionStringBuilder Class to parse your connection string into its component pieces:

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

In this case you can use the DataSource property:

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


It seems that for Sql Server Express edition, the use of |DataDirectory| automatically triggered the check for file existence and even its creation if it does not exist. Not sure if the same holds true for Sql Server CE. Here's the only (old) reference I could find: http://msdn.microsoft.com/en-us/library/aa478948.aspx

If you want to resolve |DataDirectory| to your actual path and do the checking by yourself, however, you could use:

AppDomain.CurrentDomain.getData(“DataDirectory”);


This would be the correct answer: Use this regular expression to parse the connectionstring:

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

The accepted answer will not work for some valid sql ce connection strings, as it uses the connection string builder for non-ce sql connections.

Here is the C# code to extract it:

    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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜