简体   繁体   English

使用Web.config ASP.NET连接到mdf数据库

[英]Connecting to mdf database with Web.config ASP.NET

I tried many things and saw a lot of information on the internet. 我尝试了很多事情,并在互联网上看到了很多信息。 It just doesn't work and I want to know what am I doing wrong. 只是不起作用,我想知道我在做什么错。

I created a .mdf database in my App_Data folder and trying to connect it through Web.config. 我在App_Data文件夹中创建了一个.mdf数据库,并尝试通过Web.config连接它。

my <connectionString /> looks like this: 我的<connectionString />看起来像这样:

<add name="Datab1" connectionString="Data Source=.\SQLExpress;AttachDbFilename=|DataDirectory|Datab1.mdf;Database=Datab1; Trusted_Connection=Yes;"  />

And I also tried this: 我也尝试过这样:

<add name="Datab1" connectionString="Data Source=.\SQLEXPRESS;
                      AttachDbFilename=E:\Secret\Secret\App_Data\Datab1.mdf;
                      Integrated Security=True;
                      User Instance=True"/>

The providerName="System.Data.Client" /> gives me an error so I removed it. providerName="System.Data.Client" />给我一个错误,所以我将其删除。

What am I doing wrong here? 我在这里做错了什么?

I finally fixed it by going to Server Explorer > Connect to Database > Data source to "Microsoft SQL Server Database File (SqlClient)" and Db file name browse to the .mdf file you want to use. 最后,我通过以下方法修复了该问题:转到“服务器资源管理器”>“连接到数据库”>“ Microsoft SQL Server数据库文件(SqlClient)”的数据源,然后将Db文件名浏览到要使用的.mdf文件。 Use Windows Authentication. 使用Windows身份验证。

in the Web.config use this string: 在Web.config中使用以下字符串:

<add name="Datab1" connectionString="Data Source=(localdb)\v11.0;AttachDbFileName=path\to\folder\of\database\Datab1.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>

And replace Datab1 with your own database name of course. 当然,用您自己的数据库名称替换Datab1。 This worked for me so this question is solved! 这对我有用,因此这个问题得以解决!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM