如何使用ASP.NET应用程序连接到SQL Server数据库 [英] How do I connect to SQL server database using ASP.NET application
本文介绍了如何使用ASP.NET应用程序连接到SQL Server数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法将我的asp.net应用程序连接到sql server数据库。
以下是我的web.config文件中的连接字符串的样子。
我的数据库名称是XcellIT
I have been unable to connect my asp.net application to a sql server database.
Here is how my connection string in my web.config file looks like.
My database name is XcellIT
<connectionStrings>
<add name="dbConnection" connectionString="Data Source=(localhost)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-XcellIT-20160613040036.mdf;Initial Catalog=aspnet-XcellIT-20160613040036;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
这是我要检查它是否在我的default.aspx中连接的地方。 cs文件。
This is where I will check to see if it is connected in my default.aspx.cs file.
protected void Page_Load(object sender, EventArgs e)
{
try
{
SqlConnection dbConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString);
if(dbConnection.State == ConnectionState.Open)
{
string message = "Database connected successfully";
Response.Redirect("Contact.aspx",true);
}
}
}
我尝试过:
我的谷歌搜索对我没有帮助。
What I have tried:
My google searches just does not help me.
推荐答案
这不是一个LocalDb连接字符串。 LocalDb仅作为文件工作,无法允许来自其他计算机的远程连接。您的连接看起来像是在尝试使用远程计算机上的LocalDb。
正确的连接字符串看起来应该更像这样:
That's not a LocalDb connection string. LocalDb only works as a file and has no facility to allow remote connections from other machines. Your connection looks like you're trying to use LocalDb from a remote machine.
The correct connection string should look more like this:
Server=(LocalDb)\v11.0;Integrated Security=true;AttachDbFileName=|DataDirectory|\aspnet-XcellIT-20160613040036.mdf;
这篇关于如何使用ASP.NET应用程序连接到SQL Server数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文