本文介绍了连接到远程MySQL数据库的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始了一个涉及使用数据库生成动态网站内容的项目。我可以在Webmatrix的数据库选项卡中连接到数据库,但我似乎无法在代码中(数据库对外部连接开放)。下面是我正在使用的Razor / HTML代码以及在浏览器中启动项目时显示的错误。



var connectionString =server = ignored; uid =省略;数据库=省略; pwd =省略;

var providerName =System .Data.SqlClient;

var db = Database.OpenConnectionString(connectionString,providerName);

var databaseQueryString =SELECT * FROM rob_projects ORDER BY Name;



< table>

< thead>

< tr>

< th>名称< / th>

< th>语言< / th>

< th>说明< / th>

< th>网址< / th>

< / tr>

< / thead>

< tbody>

@foreach(db.Query(databaseQueryString)中的var行){

< tr>

< td> @ row.Name< ; / td>

< td> @ row.Language< / td>

< td> @ row.Description< / td>

< td> @ row.URL< / td>

< / tr>

}

< / tbody>

< / table>









未找到网络路径

描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。



异常详细信息:System.ComponentModel.Win32Exception:找不到网络路径



来源错误:





第23行:

第24行:第25行:@foreach(db.Query(databaseQueryString)中的var行){

第26行:第27行:@ row.NameSource文件:c:\ Users \\ \\ robert \Documents \ Myy Sites\EmptySite\index.cshtml行:25



Stack Trace:





[Win32Exception(0x80004005):找不到网络路径]



[SqlException(0x80131904):A建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供者:命名管道提供程序,错误:40 - 无法打开与SQL Server的连接)]

System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,Boolean breakConnection,Action`1 wrapCloseInAction) +5352431

System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose)+244
System.Data.SqlClient.TdsParser.Connect(ServerInfo) serverInfo,SqlInternalConnectionTds connHandler,Boolean ignoreSniOpenTimeout,Int64 timerExpire,Boolean encrypt,Boolean trustServerCert,Boolean integratedSecurity,Boolean withFailover)+5363103

System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo,String newPassword,SecureString newSecurePassword,Boolean ignoreSniOpenTimeout,TimeoutTimer timeout,Boolean withFailover)+ 145

System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo,Str ing newPassword,SecureString newSecurePassword,Boolean redirectedUserInstance,SqlConnectionString connectionOptions,SqlCredential credential,TimeoutTimer timeout)+891
System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout,SqlConnectionString connectionOptions,SqlCredential credential,String newPassword, SecureString newSecurePassword,Boolean redirectedUserInstance)+307

System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity,SqlConnectionString connectionOptions,SqlCredential credential,Object providerInfo,String newPassword,SecureString newSecurePassword,Boolean redirectedUserInstance,SqlConnectionString userConnectionOptions ,SessionData reconnectSessionData)+518

System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions选项,DbConnectionPoolKey poolKey,Object poolGroupProviderInfo,DbConnectionPool池,DbConnection owningConnection,DbCo nnectionOptions userOptions)+278

System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool池,DbConnection owningObject,DbConnectionOptions选项,DbConnectionPoolKey poolKey,DbConnectionOptions userOptions)+38

系统。 Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject,DbConnectionOptions userOptions,DbConnectionInternal oldConnection)+732
System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject,DbConnectionOptions userOptions,DbConnectionInternal oldConnection)+85

System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,UInt32 waitForMultipleObjectsTimeout,Boolean allowCreate,Boolean onlyOneCheckConnection,DbConnectionOptions userOptions,DbConnectionInternal&连接)+1057

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)+196

System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection,DbConnectionFactory connectionFactory ,TaskCompletionSource`1 retry,DbConnectionOptions userOptions)+146

System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection,DbConnectionFactory connectionFactory,TaskCompletionSource`1 retry,DbConnectionOptions userOptions)+16

System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1重试) +94

System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)+110

System.Data.SqlClient.SqlConnection.Open()+96

WebMatrix.Data.Database.EnsureConnectionOpen()+46

WebMatrix.Data。< queryinternal> d__0.MoveNext()+67

系统。 Collections.Generic.List`1..ctor(IEnumerable`1 collection)+381

System.Linq.Enumerable.ToList(IEnumerable`1 source)+58

WebMatrix .Data.Database.Query(String commandText,Object []参数)+98

ASP._Page_index_cshtml.Execute()在c:\ Users \ Robobe \ Document \ MyT Sites \ _EmptySite \index.cshtml:25

System.Web.WebPages.WebPageBase.ExecutePageHierarchy()+199

System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable` 1执行者)+69

System.Web.WebPages.WebPage.ExecutePageHierarchy()+ 147

System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext,TextWriter writer,WebPageRenderingBase startPage)+78

System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext)+116





版本信息:Microsoft .NET Framework版本:4.0.30319; ASP.NET版本:4.0.30319.18408

I've begun a project that involves generating dynamic website content by using a database. I can connect to the databse fine in Webmatrix's database tab but I can't seem to in the code (the database is open to external connection). Below is the Razor/HTML code I'm using and the error that is displayed when launching the project in a browser.

var connectionString = "server=omitted;uid=omitted;database=omitted;pwd=omitted";
var providerName = "System.Data.SqlClient";
var db = Database.OpenConnectionString(connectionString, providerName);
var databaseQueryString = "SELECT * FROM rob_projects ORDER BY Name";

<table>
<thead>
<tr>
<th>Name</th>
<th>Language</th>
<th>Description</th>
<th>URL</th>
</tr>
</thead>
<tbody>
@foreach(var row in db.Query(databaseQueryString)){
<tr>
<td>@row.Name</td>
<td>@row.Language</td>
<td>@row.Description</td>
<td>@row.URL</td>
</tr>
}
</tbody>
</table>




The network path was not found
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ComponentModel.Win32Exception: The network path was not found

Source Error:


Line 23:
Line 24: Line 25: @foreach(var row in db.Query(databaseQueryString)){
Line 26: Line 27: @row.NameSource File: c:\Users\Robert\Documents\My Web Sites\EmptySite\index.cshtml Line: 25

Stack Trace:


[Win32Exception (0x80004005): The network path was not 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.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5352431
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +244
System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) +5363103
System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +145
System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) +891
System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) +307
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData) +518
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +278
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) +732
System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +85
System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +1057
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) +196
System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +146
System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +16
System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) +94
System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +110
System.Data.SqlClient.SqlConnection.Open() +96
WebMatrix.Data.Database.EnsureConnectionOpen() +46
WebMatrix.Data.<queryinternal>d__0.MoveNext() +67
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +381
System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
WebMatrix.Data.Database.Query(String commandText, Object[] parameters) +98
ASP._Page_index_cshtml.Execute() in c:\Users\Robert\Documents\My Web Sites\EmptySite\index.cshtml:25
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +199
System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +69
System.Web.WebPages.WebPage.ExecutePageHierarchy() +147
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +78
System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext) +116


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18408

推荐答案


这篇关于连接到远程MySQL数据库的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 09:43