简体   繁体   English

Azure - ASP.NET MVC 连接到 mysql 数据库

[英]Azure - ASP.NET MVC connect to mysql database

Actually, i wanted to check is my connection string valid.实际上,我想检查我的连接字符串是否有效。

There are asp.net mvc web app and mysql db deployed on azure, so i want use second thing in first.在 azure 上部署了 asp.net mvc web 应用程序和 mysql db,所以我想首先使用第二件事。 So, i added db to visual studio server explorer - db and all tables are visible.因此,我将 db 添加到 Visual Studio 服务器资源管理器 - db 和所有表都是可见的。

Now i tried to add connectionstring to my project, and, wow, there are two of them i found: First: In Visual studio data connection properties:现在我尝试将 connectionstring 添加到我的项目中,哇,我发现了其中两个:第一:在 Visual Studio 数据连接属性中:

server=us-cdbr-azure-central-a.cloudapp.net;user id=userid;persistsecurityinfo=True;database=crawlerdb

Second: In azure workplace's database property:第二:在azure工作场所的数据库属性中:

Database=CrawlerDB;Data Source=us-cdbr-azure-central-a.cloudapp.net;User Id=userid;Password=userpass

And both of them doesn't work.而且这两个都不起作用。 Never lucky.从来不走运。 Connection state checking code:连接状态检查代码:

using (SqlConnection conn = new SqlConnection(connstr))
        {
            try
            {
                conn.Open();
                var q = conn.State;
            }
            catch(Exception ex)
            {
                var q = ex.Message;
            }
        }

What am i doing wrong?:) Tell me plz:)我做错了什么?:) 请告诉我 :)


public class CrawledDataContext : DbContext
{
    public CrawledDataContext()
    {
        Database.SetInitializer<CrawledDataContext>(null);
        using (SqlConnection conn = new SqlConnection("server=us-cdbr-azure-central-a.cloudapp.net;user id=bb15193d20f901;persistsecurityinfo=True;database=crawlerdb"))
        {
            try
            {
                conn.Open();
                var q = conn.State;
            }
            catch(Exception ex)
            {
                var q = ex.Message;
            }
        }
    }
    public DbSet<GroupInfo> GroupInfoes { get; set; }
}

Here is exception message:这是异常消息:

"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)" “建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。(提供者:命名管道提供程序,错误:40 - 无法打开与 SQL Server 的连接)”

SqlConnection is for SQL Server Databases. SqlConnection 用于 SQL Server 数据库。 you should use MySqlConnection instead.您应该改用 MySqlConnection。

using (MySqlConnection conn = new MySqlConnection("server..."){}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM