C# 错误26的解决方案-定位指定的服务器/实例时出错?
我已经为我的大学项目发布了我的网站,但它正在显示C# 错误26的解决方案-定位指定的服务器/实例时出错?,c#,asp.net,sql-server,connection-string,C#,Asp.net,Sql Server,Connection String,我已经为我的大学项目发布了我的网站,但它正在显示 Error 26 - Error Locating Server/Instance Specified. Other things are working properly. 您可以访问网站了解详细错误 我的项目是Visual Studio 2010的C#ASP.NET 4.0和内置的Sql Server 2008 我在每个页面中都使用此连接字符串 SqlConnection con = new SqlConnection("data sour
Error 26 - Error Locating Server/Instance Specified. Other things are working properly.
您可以访问网站了解详细错误
我的项目是Visual Studio 2010的C#ASP.NET 4.0
和内置的Sql Server 2008
我在每个页面中都使用此连接字符串
SqlConnection con = new SqlConnection("data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true");
SqlCommand cmd = new SqlCommand();
我知道我可以把它放在Web.config
中,但我不知道如何在aspx页面中使用它进行查询
我的问题是:为了让我的网站正常运行,我应该对我的连接字符串做什么更改
其他详细信息:我从中获取了我的Web托管计划,它支持Sql Server 2008。请尝试解决这段代码。如果使用Sql身份验证,则应在get连接字符串中提供用户名和密码。
private static void OpenSqlConnection()
{
string connectionString = GetConnectionString();
using (SqlConnection connection = new SqlConnection())
{
connection.ConnectionString = connectionString;
connection.Open();
Console.WriteLine("State: {0}", connection.State);
Console.WriteLine("ConnectionString: {0}",
connection.ConnectionString);
}
}
static private string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
return "Data Source=MSSQL1;Initial Catalog=AdventureWorks;"
+ "Integrated Security=true;";
}
我不熟悉.Net。我只是想知道,为了让我的网站正常运行,我应该在连接字符串中做些什么更改?为什么要使用AttachDBFilename=| DataDirectory | aspnetdb.mdf。如果sql实例中存在数据库,为什么不使用数据库名称呢。数据源=您的服务器名称;初始目录=您的数据库名称;UserId=您的_用户名;Password=你的密码因为我的托管服务提供商告诉我这样做,你能给我一个连接字符串的示例吗?也许可以向你的托管服务提供商询问连接字符串的示例。如果您在共享主机上,数据库服务器通常位于单独的计算机上。