【问题标题】:Can't connect to database from visual studio c#无法从 Visual Studio C# 连接到数据库
【发布时间】:2017-09-06 15:06:53
【问题描述】:

我想从我的 c# windows 窗体应用程序连接到数据库。我尝试使用

using(SqlConnection conn = new SqlConnection()) {
 conn.ConnectionString = "Data Source=localhost; User Id=root; Password=; Initial Catalog=dbName";
 conn.Open();
}

当我构建我的项目时,我收到一个错误 server wasn't found or wasn't accessible

我尝试通过数据源配置向导连接,但也找不到数据库。

我使用WAMP服务器,可以通过phpMyAdmin找到我的数据库。

【问题讨论】:

  • 发生了什么错误?
  • @SachithMW 这是一个运行时错误,上面写着:Unhandled exception has occured in your application ... ... A network-related or instance-specific error occurred while establishing a connection to SQL server. The server was not found or was not accessible. ...

标签: c# mysql database visual-studio wamp


【解决方案1】:

您正在尝试连接到 Microsoft SQL Server。

using System;
using System.Data;

using MySql.Data;
using MySql.Data.MySqlClient;

public class Tutorial1
{
    public static void Main()
    {
        string connStr = "server=localhost;user=root;database=world;port=3306;password=******;";
        MySqlConnection conn = new MySqlConnection(connStr);
        try
        {
            Console.WriteLine("Connecting to MySQL...");
            conn.Open();
            // Perform database operations
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        conn.Close();
        Console.WriteLine("Done.");
    }
}

查看此链接,希望对您有所帮助

你可以通过打开包管理器控制台工具> NuGet包管理器>包管理器控制台安装MySql.Data,然后在里面输入Install-Package MySql.Data -Version 6.9.9

【讨论】:

  • @SachitMW 使用MYSqlConnection 类我应该包括什么?
  • 我没有那个mysql.data库,只有System.Data,现在想找下载。
  • @wdc 只需在 Visual Studio 中使用 nuget nuget.org/packages/MySql.Data
  • @Takarii 谢谢,稍后再试。
【解决方案2】:

您需要使用MySqlConnection 连接到 MySQL 数据库,请在此处查看答案

How to connect to MySQL Database?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    • 2020-03-30
    • 2013-04-14
    相关资源
    最近更新 更多