如何为 mdf 文件创建连接

How to create connection for mdf files

我正在使用本地数据库创建 Windows 应用程序。

现在我需要在 C# 中为该本地数据库定义一个连接字符串。

Winforms C# 中本地 .mdf 文件的连接字符串是什么?

使用SqlServeCe作为命名空间,然后给出如下编码

     using System.Data.SqlServerCe;

      SqlCeConnection conn = new SqlCeConnection(@"Data Source=D:\PROJECT\data\db.mdf;");

执行以下步骤:

  • 在您的 Server Explorer window 中右键单击数据库 您已创建 select Properties.

  • 中的Properties复制window连接字符串的值 属性 并将其粘贴到您的应用程序中。

应该是这样的:

Data Source=(localdb)\v11.0;Initial Catalog=yourDB;Integrated Security=True

只需添加一个额外的 \ 即可在 c# 中工作:

string address = "Data Source=(localdb)\v11.0;Initial Catalog=yourDB;Integrated Security=True";

你也可以看看 The Connection Strings Reference.

给你

<add name="YourDataBase" connectionstring="Data Source=.\SQLEXPRESS;  AttachDbFilename=Path\YourDataFile.mdf; Integrated Security=True; Connect Timeout=30; User Instance=True" /> 

您也可以访问 connectionstring.Com 了解更多信息。