简体   繁体   English

C#WebSite与MySQL数据库的连接

[英]C# WebSite connection to a mySQL database

I want to connect my WebSite to a local database. 我想将我的网站连接到本地数据库。 My connection String is the following: 我的连接字符串如下:

     <connectionStrings>

<add name ="localhost"

 connectionString="server=localhost;user id=root;persistsecurityinfo=True;database=doctorappointments" 
     providerName="MySql.Data.MySqlClient"
 />

I can connect from MySql Workbench and also from Visual Studio if I add a new connection in Server Explorer. 如果在服务器资源管理器中添加新连接,则可以从MySql Workbench连接,也可以从Visual Studio连接。 The connection String from above is the same as the one from server Explorer. 上面的连接字符串与服务器资源管理器中的连接字符串相同。 I checked that my server is running, I also put a firewall rule for the port that it connects to. 我检查了我的服务器是否正在运行,还为它连接的端口设置了防火墙规则。

This how I am using the connectionString: 这是我使用connectionString的方式:

      using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["localhost"].ConnectionString))
        {

        con.Open();
        Response.Redirect("MainPage.aspx");

        }

And on the con.Open() it gives me this error: 在con.Open()上,它给了我这个错误:

An exception of type 'System.Data.SqlClient.SqlException' occurred in >System.Data.dll but was not handled in user code > System.Data.dll中发生类型'System.Data.SqlClient.SqlException'的异常,但未在用户代码中处理

Additional information: A network-related or instance-specific error occurred >while establishing a connection to SQL Server. 附加信息:在建立与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. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: Named Pipes Provider, error: >40 - Could not open a connection to SQL Server) (提供者:命名管道提供程序,错误:> 40-无法打开与SQL Server的连接)

and also: 并且:

  • ServerVersion 'con.ServerVersion' threw an exception of type 'System.InvalidOperationException' string {System.InvalidOperationException} ServerVersion'con.ServerVersion'引发了类型为'System.InvalidOperationException'字符串{System.InvalidOperationException}的异常

I am new to this and I have tried everything I found online. 我对此并不陌生,我已经尝试了所有在网上找到的内容。 I do not know what am I doing wrong... 我不知道我在做什么错...

SqlConnection is for Sql Server. SqlConnection适用于Sql Server。

Make sure you have installed MySql.Data via nuget package and use MySqlConnection instead. 确保已通过nuget软件包安装了MySql.Data ,并改用MySqlConnection

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

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