且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

不能与Database.Open连接(QUOT;&的MySqlConnection QUOT;) - 服务器未找到或无法访问

更新时间:2022-10-23 18:57:05

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

在第一次尝试,它是从字面上看的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.