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

发布于 2024-11-19 23:14:25 字数 206 浏览 3 评论 0原文

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

Data Source=|DataDirectory|IntegrationTests.sdf

我如何确定该文件是否存在?

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

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

Data Source=|DataDirectory|IntegrationTests.sdf

how do I 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?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小忆控 2024-11-26 23:14:25

也许使用 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);

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);
爱人如己 2024-11-26 23:14:25

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

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

AppDomain.CurrentDomain.getData(“DataDirectory”);

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”);
濫情▎り 2024-11-26 23:14:25

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

@"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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文