[英]Can't connect with Database.Open(“MySQLConnection”) - The server was not found or was not accessible [closed]
我试图根据数据库中的结果在Visual Studio的ASP.NET中制作图表/图形,但在使用Database.Open().
连接数据库时遇到问题Database.Open().
我收到错误消息:
SqlException(0x80131904):建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 服务器未找到或无法访问。 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (提供者:命名管道提供程序,错误:40-无法打开与SQL Server的连接)
我正在使用MySQL,并且数据库处于联机状态(即使我尝试使用本地db并遇到相同的错误)
我在这里找到了这种创建图形的方法: 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>
PS我已经读过这种例外情况,通常连接字符串存在问题,但是我使用相同的连接字符串在应用程序其他部分(在模型和控制器中)连接到数据库时没有任何问题,并且工作正常...虽然我正在用它来连接:
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();
PSS我正在使用MySql,并且我已经更改了提供程序名称,因为很少有用户说这可能是问题所在。错误是相同的。
toddmo非常感谢您。 这终于到了连接字符串。 但是现在我也遇到了问题,大声笑...我遇到了类似System.ArgumentException: Keyword not supported: 'server'
错误System.ArgumentException: Keyword not supported: 'server'
。 和连接字符串中的'database'属性相同,然后我用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文件:
<?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=\\"Web\\" /optionInfer+" /> </compilers> </system.codedom> </configuration>
您将需要using System.Configuration;
在您的代码模块中,更改Database.Open("MySQLConnection");
到Database.OpenConnectionString(ConfigurationManager.ConnectionStrings["MySQLConnection"].ConnectionString);
第一次尝试时,它实际上是在看MySQLConnection
,好像它是连接字符串一样。 您需要用该密钥附带的连接字符串替换密钥(MySQLConnection)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.