无法连接Database.Open(" MySQLConnection") - 找不到服务器或无法访问服务器

时间:2015-11-20 23:39:15

标签: c# mysql asp.net razor

我试图根据数据库中的结果在Visual Studio中的ASP.NET中创建图表/图表,但我在使用Database.Open().连接到数据库时出现问题 我收到错误:

  

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

我使用MySQL并且数据库处于联机状态(即使我尝试使用本地数据库并且我得到了同样的错误)

我在这里创建了这种图表:http://www.asp.net/web-pages/overview/data/7-displaying-data-in-a-chart

这是.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();
}

以下是我在Web.config中使用的连接字符串(我在此处删除了真实的用户名和密码)

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

P.S。我已经了解了这种异常情况,并且通常存在连接字符串的问题,但是我使用相同的连接字符串而没有任何问题连接到应用程序的其他部分中的数据库(在模型和控制器)它工作得很好......虽然我用它来连接:

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我正在使用MySql并且我已经更改了提供商名称,因为很少有用户说这可能是问题......错误是一样的。

托普莫非常感谢你。这终于得到了连接字符串。但现在我也遇到了问题lol ......我收到的错误就像System.ArgumentException: Keyword not supported: 'server'。和#39;数据库&#39;连接字符串中的属性,然后我用google搜索错误,似乎我必须将连接字符串更改为此<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" />

这是整个Web.config文件:

&#13;
&#13;
<?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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您需要在代码模块中包含using System.Configuration;Database.Open("MySQLConnection");更改为Database.OpenConnectionString(ConfigurationManager.ConnectionStrings["MySQLConnection"].ConnectionString);

在第一次尝试中,它实际上是在查看MySQLConnection,就好像它是连接字符串一样。您需要将密钥(MySQLConnection)替换为与该密钥一起使用的连接字符串。