简体   繁体   English

无法使用C#连接到MySQL数据库; 错误状态:18

[英]Can't connect to MySQL Database with C#; Error state: 18

Below is the code I used to try to connect to a remote MySQL server. 下面是我用来尝试连接到远程MySQL服务器的代码。 I've connected to this server with a Python script using pymysql but I am unable to connect to it with Visual Studios. 我已使用pymysql使用Python脚本连接到此服务器,但无法使用Visual Studios连接到该服务器。 The application is "fully trusted" in visual studios. 该应用程序在视觉工作室中是“完全受信任的”。 When I run this, I get System.InvalidOperationException stating an internal connection fatal error. 运行此命令时,我得到System.InvalidOperationException声明内部连接致命错误。

 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Data.SqlClient; namespace Rextester { public class Program { public static void Main(string[] args) { //Your code goes here Console.WriteLine("Hello, world!!"); SqlConnection myConnection = new SqlConnection("User ID={UserID};" + "password={Password};Data Source=sql9.freesqldatabase.com, 3306;" + "Trusted_Connection=yes;" + "Initial Catalog=sql9159612; " + "connection timeout=30"); try { myConnection.Open(); Console.WriteLine("Opened connection"); } catch(Exception e) { Console.WriteLine("Can't open connection"); Console.WriteLine(e.ToString()); } } } } 

First, you'll want to download the MySql.Data package from Nuget. 首先,您需要从Nuget下载MySql.Data包。

Then take out ",3306" from the Data Source, and also remove the Trusted_Connection element... 然后从数据源中取出“,3306”,并删除Trusted_Connection元素...

var myConnection = new MySqlConnection("User ID={UserId};" +
                                                   "password={Password};Data Source=sql9.freesqldatabase.com;" +
                                                   "Initial Catalog={database}; " +
                                                   "connection timeout=30");

(I remove some of the sensitive information, fyi...) (我删除了一些敏感信息,仅供参考...)

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

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