2017-02-28 48 views
0

我的老师给我发了一个数据库文件,我在SQL SERVER中恢复它。他还给我发了一个asp.net项目。他要求我通过添加连接字符串将数据库文件连接到项目,但我不知道在哪里添加它..我必须继续该项目,请帮助我。如何连接数据库文件到asp.net

+0

谷歌“.net连接到SQL服务器数据库”,我相信你会发现一些东西。 – Carra

+0

如果它的桌面应用程序或web.config文件(如果它是一个Web应用程序),则必须将其添加到您的app.config文件中。您也可以使用ado.net实体框架,在这种情况下,您将能够通过使用GUI连接到数据库,不需要手动在配置文件中添加任何内容。 –

+0

Badhon我在我的项目中有web.config文件,我的老师也要求我在那里添加字符串。首先,我认为可能是该文件中存在字符串查询,我需要做的只是将其替换为来自我的系统的连接字符串。但我无法找到任何东西。我看到的只是该web.config文件中的这段代码。 – Fary

回答

1

您需要将app.config/web.config文件添加到您的项目中,然后详细说明该文件中的连接字符串。

首先你有web.config文件。那么你需要添加一个节中,如果不存在所谓的

<appSettings> 
     <add key="DBConnection" value="data source=nameofserver;initial catalog=dnname ;integrated security=True;MultipleActiveResultSets=True;" /> 
</<appSettings> 

然后你就可以得到这个代码的关键,并连接到数据库

using(SqlConnection conn = new SqlConnection()) 
    { 
     // this connection string age can be saved to config file 
     conn.ConnectionString = // read this fron config ; 
     // using the code here to query the data 
    } 
+0

是的,我有web.config文件。你能告诉我如何在特定的数据库中添加字符串吗? – Fary

+0

我非常困惑:( – Fary

0

添加配置节点内的ConnectionStrings节点在Web.config文件。

<configuration> 
    <connectionStrings> 
    <add name="DatabaseConnection" providerName="System.Data.SqlClient" connectionString="Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;" /> 
    </connectionStrings> 
</configuration> 

用您的数据替换myServerAddressmyDataBase。此处的关键是完成connectionString属性。