【发布时间】:2017-03-07 10:12:08
【问题描述】:
我正在使用 ASP.NET Framework 4.5 开发 Web 应用程序。 SQLSERVER 数据库已连接到项目。一切正常,我已经完成了这个项目。在发布项目之前,我将连接字符串更改为在线服务器数据库。该网站已成功发布。
为了对应用程序做一些小改动,我将连接字符串改回本地数据库。现在,当我运行应用程序时,出现以下错误:
“/”应用程序中的服务器错误。
找不到网络名称。
说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详情: System.ComponentModel.Win32Exception:找不到网络名称
来源错误:
Line 49:
Line 50: DataTable dt = new DataTable();
Line 51: conn.Open(); // Throws Exception Here
Line 52: string query = "SELECT Event.StartDate, Event.title, Artist.[Stage Name] FROM Event INNER JOIN Artist ON Event.ArtistID=Artist.ID WHERE StartDate >= GETDATE() ORDER BY CONVERT(DateTime, StartDate,101) DESC";
Line 53: SqlDataAdapter da = new SqlDataAdapter(query, conn);
源文件: c:\Users\Junaid\Desktop\FAST\PasoFino\Artist Management\ArtistManagementSystem\ArtistManagementSystem\Views\Portal\Dashboard.aspx.cs 行:51
堆栈跟踪:
[Win32Exception (0x80004005): The network name cannot be found]
[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)]
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling) +821
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +332
System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) +38
System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +699
System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +89
System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +426
System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +78
System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) +191
System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +154
System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +21
System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) +90
System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +217
System.Data.SqlClient.SqlConnection.Open() +96
ArtistManagementSystem.Views.Portal.Dashboard.showUpComingEvents() in c:\Users\Junaid\Desktop\FAST\PasoFino\Artist Management\ArtistManagementSystem\ArtistManagementSystem\Views\Portal\Dashboard.aspx.cs:51
ArtistManagementSystem.Views.Portal.Dashboard.Page_Load(Object sender, EventArgs e) in c:\Users\Junaid\Desktop\FAST\PasoFino\Artist Management\ArtistManagementSystem\ArtistManagementSystem\Views\Portal\Dashboard.aspx.cs:20
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +95
System.Web.UI.Control.LoadRecursive() +59
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +678
这是我的 Web.Config:
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<system.web>
<sessionState mode="InProc" timeout="20"></sessionState>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" maxRequestLength="1048576"/>
<pages>
<controls>
<add assembly="TimePicker" namespace="MKB.TimePicker" tagPrefix="cc1"/>
<add tagPrefix="ajaxtoolkit" namespace="AjaxControlToolkit" assembly="AjaxControlToolKit"/>
</controls>
</pages>
<!--<authentication mode="Forms">
<forms loginUrl="Views/Login/Login.aspx" defaultUrl="Views/Portal/Dashboard.aspx">
<credentials passwordFormat="Clear">
<user name="junaid" password="fastian123"/>
</credentials>
</forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization>-->
</system.web>
<connectionStrings>
<!--<add name="ArtistManagementSystemEntities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=REDDEVIL\SQLEXPRESS;initial catalog=ArtistManagementSystem;persist security info=True;user id=sa;password=fastian123;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
<add name="ArtistManagementSystemConnectionString" connectionString="Data Source=REDDEVIL/SQLEXPRESS;Initial Catalog=ArtistManagementSystem;Persist Security Info=True;User ID=sa;Password=fastian123;MultipleActiveResultSets=True;Application Name=EntityFramework"
providerName="System.Data.SqlClient" />-->
<add name="ArtistManagementSystem" connectionString="Data Source=REDDEVIL/SQLEXPRESS;Initial Catalog=ArtistManagementSystem;User ID=sa;Password=***********" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
</configuration>
这是我得到错误的地方:(Dashboard.aspx)
protected void showUpComingEvents()
{
//string myConnection = dbController.connectionString;
string myConnection = dbController.connectionString;
SqlConnection conn = new SqlConnection(myConnection);
DataTable dt = new DataTable();
conn.Open();
string query = "SELECT Event.StartDate, Event.title, Artist.[Stage Name] FROM Event INNER JOIN Artist ON Event.ArtistID=Artist.ID WHERE StartDate >= GETDATE() ORDER BY CONVERT(DateTime, StartDate,101) DESC";
SqlDataAdapter da = new SqlDataAdapter(query, conn);
da.Fill(dt);
conn.Close();
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
这是 dbController 类,它只有一个连接字符串。 (我知道这不是一个好方法,但这只是为了让事情正常进行)
dbController.cs:
public class dbController
{
//public static string connectionString = "Data Source=SQL5027.myASP.NET;Initial Catalog=DB_A0C908_nbrewer0210;User Id=DB_A0C908_nbrewer0210_admin;Password=fastian123";
public static string connectionString = "Data Source=REDDEVIL/SQLEXPRESS;Initial Catalog=ArtistManagementSystem;User ID=sa;Password=***********";
}
注意:我想提几点:
1) 我可以使用 SqlServer Management Studio 连接到数据库。
2) 在服务中,SQL Server (SQLEXPRESS) 和 SQL Server Browser 正在运行。
3) 在 SQL Server 配置管理器中,在客户端协议中,启用 TCP/IP 和命名管道。
【问题讨论】:
-
我认为你的连接字符串是错误的。你可能需要一个反斜杠:
Data Source=REDDEVIL\SQLEXPRESS;... -
如果数据库在本地机器上,那么你可以使用
Data Source=. -
@Marco \S 给出无法识别的转义序列错误。
-
即不使用
REDDEVIL/SQLEXPRESS,您可以使用.,其余的连接字符串将保持原样
标签: c# asp.net sql-server visual-studio sql-server-2008