【问题标题】:Can't connect to MySQL Database with C#; Error state: 18无法使用 C# 连接到 MySQL 数据库;错误状态:18
【发布时间】:2017-02-20 02:10:25
【问题描述】:

下面是我用来尝试连接到远程 MySQL 服务器的代码。我已经使用 pymysql 通过 Python 脚本连接到该服务器,但我无法使用 Visual Studios 连接到它。该应用程序在视觉工作室中“完全受信任”。当我运行它时,我得到 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());
            }
            
        }
    }
}

【问题讨论】:

标签: c# mysql database connection


【解决方案1】:

首先,您需要从 Nuget 下载 MySql.Data 包。

然后从数据源中取出“,3306”,同时删除 Trusted_Connection 元素...

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

(我删除了一些敏感信息,仅供参考...)

【讨论】:

    猜你喜欢
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 2013-04-22
    • 2016-12-11
    • 1970-01-01
    • 2015-03-09
    • 2021-01-20
    • 2012-10-29
    相关资源
    最近更新 更多