不能与Database.Open连接(QUOT;&的MySqlConnection QUOT;) - 服务器未找到或无法访问 [英] Can't connect with Database.Open("MySQLConnection") - The server was not found or was not accessible

查看:452
本文介绍了不能与Database.Open连接(QUOT;&的MySqlConnection QUOT;) - 服务器未找到或无法访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据数据库中的结果,以使ASP.NET在Visual Studio图/图和我在与连接到数据库 Database.Open问题()。
我得到错误:


  

的SQLException(0x80131904):A-特定实例网络相​​关的或
  而与SQL Server建立连接时出现错误。该
  服务器未找到或无法访问。验证实例
  名称正确,并且该SQL服务器被配置为允许远程
  连接。 (provider:命名管道提供程序,error:40 - 无法
  打开到SQL Server的连接)


我使用MySQL和数据库处于联机状态(尽管我试图与本地数据库和我得到了同样的错误)

我发现这种创建图形的位置:的 http://www.asp.net/web-pages/overview/data/7-displaying-data-in-a-chart

这是code在.cshtml

  @using WebMatrix.Data;
@ {
    变种分贝= Database.Open(的MySqlConnection);
    VAR数据= db.Query(SELECT user_IP,user_Country FROM user_stats);
    VAR myChart =新图(宽度:600,高度:400)
        .AddTitle(Naslov)
        .DataBindTable(数据源:数据,xField:USERIP)
        。写();
}

下面是我使用的 Web.config文件的连接字符串(我删除了真实的用户名和密码,点击这里)

 <&是connectionStrings GT;
<添加名称=的MySqlConnection
     connectionString=\"Server=46.105.103.40;Database=miranzab_webtrafficanalyzer;Uid=username;Pwd=password;\"
     的providerName =System.Data.SqlClient的/>
< /&是connectionStrings GT;

P.S。我读过有关这种异常的,通常有一个与连接字符串的问题,但我使用没有任何问题,相同的连接字符串用于连接到应用程序的其他部分DATABSE(在模型和控制器),并​​能正常工作。 ..though我使用这个连接:

  cWebTrafficDb checkUserStatsWrapper =新cWebTrafficDb(); //这个类是用于打开和关闭连接
checkUserStatsWrapper.cmd.CommandText =的String.Format(从user_stats选择*其中user_ip ='{0}',USERIP);
MySqlDataReader将读者= checkUserStatsWrapper.cmd.ExecuteReader();

P.S.S我使用MySQL和我的几个用户说可​​能是这个问题已经改变供应商的名字......错误是一样的。

toddmo非常感谢你。这次终于到了连接字符串。但我现在用起来太大声笑...我是越来越喜欢 System.ArgumentException错误的问题:不支持关键字:服务器。与同为在连接字符串数据库属性,然后我用Google搜索错误,似乎我不得不改变连接字符串本<添加名称=的MySqlConnection的connectionString =数据源= 46.105 .103.40;初始目录= miranzab_webtrafficanalyzer;集成安全性= SSPI;用户ID =用户名;密码=密码的providerName =MySql.Data.MySqlClient/>

下面是整个Web.config文件:

\r
\r

<?XML版本=1.0编码=UTF-8? >\r
<! - \r
  有关如何配置ASP.NET应用程序的更多信息,请访问:\r
  http://go.microsoft.com/fwlink/?LinkId=301880\r
   - >\r
<结构>\r
  < configSections>\r
    <! - 有关Entity Framework的配置的详细信息,请访问http://go.microsoft.com/fwlink/?LinkID=237468 - >\r
    <节名称=的EntityFrameworkTYPE =System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,的EntityFramework,版本6.0.0.0 =文化=中性公钥= b77a5c561934e089requirePermission =FALSE/>\r
  < / configSections>\r
  \r
  <&的appSettings GT;\r
    <添加键=网页:版本值=3.0.0.0/>\r
    <添加键=网页:启用VALUE =FALSE/>\r
    <添加键=ClientValidationEnabledVALUE =真/>\r
    <添加键=UnobtrusiveJavaScriptEnabledVALUE =真/>\r
  < /的appSettings>\r
 \r
 <&是connectionStrings GT;\r
    <添加名称=的MySqlConnection\r
      的connectionString =数据源= 46.105.103.40;初始目录= miranzab_webtrafficanalyzer;集成安全性= SSPI;用户ID =用户名;密码=密码;\r
         的providerName =System.Data.SqlClient的/>\r
< /&是connectionStrings GT;\r
    \r
  <&的System.Web GT;\r
    <身份验证模式=无/>\r
    <编译调试=真targetFramework =4.5.2/>\r
    <的httpRuntime targetFramework =4.5.2/>\r
    <的customErrors模式=关/>\r
  < /system.web>\r
  < system.webServer>\r
    <模块>\r
      <清除NAME =FormsAuthentication/>\r
    < /模块>\r
  < /system.webServer>\r
  <&运行GT;\r
    < assemblyBinding的xmlns =瓮:架构 - 微软COM:asm.v1>\r
      < dependentAssembly>\r
        < assemblyIdentity名称=Microsoft.Owin.Security公钥=31bf3856ad364e35/>\r
        < bindingRedirect oldVersion =0.0.0.0-3.0.1.0NEWVERSION =3.0.1.0/>\r
      < / dependentAssembly>\r
      < dependentAssembly>\r
        < assemblyIdentity名称=Microsoft.Owin.Security.OAuth公钥=31bf3856ad364e35/>\r
        < bindingRedirect oldVersion =0.0.0.0-3.0.1.0NEWVERSION =3.0.1.0/>\r
      < / dependentAssembly>\r
      < dependentAssembly>\r
        < assemblyIdentity名称=Microsoft.Owin.Security.Cookies公钥=31bf3856ad364e35/>\r
        < bindingRedirect oldVersion =0.0.0.0-3.0.1.0NEWVERSION =3.0.1.0/>\r
      < / dependentAssembly>\r
      < dependentAssembly>\r
        < assemblyIdentity名称=Microsoft.Owin公钥=31bf3856ad364e35/>\r
        < bindingRedirect oldVersion =0.0.0.0-3.0.1.0NEWVERSION =3.0.1.0/>\r
      < / dependentAssembly>\r
      < dependentAssembly>\r
        < assemblyIdentity名称=Newtonsoft.Json文化=中性公钥=30ad4fe6b2a6aeed/>\r
        < bindingRedirect oldVersion =0.0.0.0-6.0.0.0NEWVERSION =6.0.0.0/>\r
      < / dependentAssembly>\r
      < dependentAssembly>\r
        < assemblyIdentity名称=System.Web.Optimization公钥=31bf3856ad364e35/>\r
        < bindingRedirect oldVersion =1.0.0.0-1.1.0.0NEWVERSION =1.1.0.0/>\r
      < / dependentAssembly>\r
      < dependentAssembly>\r
        < assemblyIdentity名称=WebGrease公钥=31bf3856ad364e35/>\r
        < bindingRedirect oldVersion =0.0.0.0-1.5.2.14234NEWVERSION =1.5.2.14234/>\r
      < / dependentAssembly>\r
      < dependentAssembly>\r
        < assemblyIdentity名称=System.Web.Helpers公钥=31bf3856ad364e35/>\r
        < bindingRedirect oldVersion =1.0.0.0-3.0.0.0NEWVERSION =3.0.0.0/>\r
      < / dependentAssembly>\r
      < dependentAssembly>\r
        < assemblyIdentity名称=System.Web.Mvc公钥=31bf3856ad364e35/>\r
        < bindingRedirect oldVersion =1.0.0.0-5.2.3.0NEWVERSION =5.2.3.0/>\r
      < / dependentAssembly>\r
      < dependentAssembly>\r
        < assemblyIdentity名称=System.Web.WebPages公钥=31bf3856ad364e35/>\r
        < bindingRedirect oldVersion =1.0.0.0-3.0.0.0NEWVERSION =3.0.0.0/>\r
      < / dependentAssembly>\r
    < / assemblyBinding>\r
  < /运行>\r
  <&的EntityFramework GT;\r
    < defaultConnectionFactory TYPE =System.Data.Entity.Infrastructure.LocalDbConnectionFactory,的EntityFramework>\r
      <&参数GT;\r
        <参数值=mssqllocaldb/>\r
      < /参数>\r
    < / defaultConnectionFactory>\r
    <供应商>\r
      <供应商invariantName =System.Data.SqlClient的TYPE =System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer/>\r
    < /供应商>\r
  < /&的EntityFramework GT;\r
  <系统codeDOM>\r
    <编译器>\r
      <编译语言=C#; CS; CSHARPCS。延长= TYPE =微软codeDom.Providers.DotNetCompilerPlatform.CSharp codeProvider,微软codeDom.Providers.DotNetCompilerPlatform版本= 1.0.0.0,文化=中性公钥= 31bf3856ad364e35warningLevel =4compilerOptions =/ langversion:6 / nowarn:1659; 1699; 1701/>\r
      <编译语言=VB,VBS,VisualBasic中; VBSCRIPTVB。延长= TYPE =微软codeDom.Providers.DotNetCompilerPlatform.VB codeProvider,微软codeDom.Providers.DotNetCompilerPlatform ,版本= 1.0.0.0,文化=中性公钥= 31bf3856ad364e35warningLevel =4compilerOptions =/ langversion:14 / nowarn:41008 /定义:_MYTYPE = \\&放大器; QUOT;网络\\&放大器; QUOT; / optionInfer + />\r
    < /编译器>\r
  < /system.$c$cdom>\r
< /结构>

\r

\r
\r


解决方案

您需要使用System.Configuration 来包括:您code模块中
变化 Database.Open(的MySqlConnection); 来<$c$c>Database.OpenConnectionString(ConfigurationManager.ConnectionStrings[\"MySQLConnection\"].ConnectionString);

在第一次尝试,它是从字面上看的MySqlConnection ,如果它是连接字符串。您需要更换与使用该密钥伴随而来的连接字符串键(的MySqlConnection)。

I'm trying to make chart/graph in ASP.NET in Visual Studio based on results in the database and I'm having problems with connecting to database with Database.Open(). I'm getting error:

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)

I'm using MySQL and the database is online (even though I tried with local db and I'm getting the same error)

I found this kind of creating graphs here: http://www.asp.net/web-pages/overview/data/7-displaying-data-in-a-chart

This is the code in .cshtml

@using WebMatrix.Data;
@{
    var db = Database.Open("MySQLConnection");
    var data = db.Query("SELECT user_IP, user_Country FROM user_stats");
    var myChart = new Chart(width: 600, height: 400)
        .AddTitle("Naslov")
        .DataBindTable(dataSource: data, xField: "userIP")
        .Write();
}

Here is connection string I'm using in Web.config (I removed real username and password here)

<connectionStrings>
<add name="MySQLConnection"
     connectionString="Server=46.105.103.40;Database=miranzab_webtrafficanalyzer;Uid=username;Pwd=password;"
     providerName="System.Data.SqlClient" />
</connectionStrings>

P.S. I've read about this kind of exceptions and usually there's a problem with connection string, but I'm using that same connection string without any problems for connecting to databse in other parts of application (In Models and Controllers) and it works fine...though I'm using this for connecting:

cWebTrafficDb checkUserStatsWrapper = new cWebTrafficDb();  //this class is for opening and closing connections
checkUserStatsWrapper.cmd.CommandText = string.Format("select * from user_stats where user_ip = '{0}'", userIp);
MySqlDataReader reader = checkUserStatsWrapper.cmd.ExecuteReader();

P.S.S I am using MySql and I've changed provider name as few users said it might be the problem... The error was the same.

toddmo thank you very much. this finally got to the connection string. but now I'm having problems with it too lol...I was getting error like System.ArgumentException: Keyword not supported: 'server'. and the same for 'database' property in connection string and then I googled for error and it seems that I had to change connection string to this <add name="MySQLConnection" connectionString="Data Source=46.105.103.40;Initial Catalog=miranzab_webtrafficanalyzer;Integrated Security=SSPI;User Id=username;Password=password" providerName="MySql.Data.MySqlClient" />

Here is the whole Web.config file:

<?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=301880
  -->
<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=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
 
 <connectionStrings>
    <add name="MySQLConnection"
      connectionString="Data Source=46.105.103.40;Initial Catalog=miranzab_webtrafficanalyzer;Integrated Security=SSPI;User Id=username;Password=password;"
         providerName="System.Data.SqlClient" />
</connectionStrings>
    
  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
    <customErrors mode="Off" />
  </system.web>
  <system.webServer>
    <modules>
      <remove name="FormsAuthentication" />
    </modules>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
</configuration>

解决方案

You will need to include using System.Configuration; in your code module and change Database.Open("MySQLConnection"); to Database.OpenConnectionString(ConfigurationManager.ConnectionStrings["MySQLConnection"].ConnectionString);

In the first attempt, it is literally looking at MySQLConnection as if it was the connection string. You need to replace the key (MySQLConnection) with the connection string that goes along with that key.

这篇关于不能与Database.Open连接(QUOT;&的MySqlConnection QUOT;) - 服务器未找到或无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆