【问题标题】:Why does my MySQL connection not work in visual studio code C#?为什么我的 MySQL 连接在 Visual Studio 代码 C# 中不起作用?
【发布时间】:2018-11-16 14:07:35
【问题描述】:

我正在用 C# 在 Visual Studio 代码中制作一个程序,它是一个付费程序,所以它需要一个 hwid 系统。基本上我希望它检查您的计算机 HWID 是否存在于我的用户数据库的 HWID 表中。但它说它无法连接到数据库。你能帮助我吗?这是我的代码。`

        string connectionString = "Server=SomeServer;Database=i got you this is notthe real database;User ID=same;Password=same for password;";

        MySqlConnection mydbCon = new MySqlConnection(connectionString);


        mydbCon.Open();

        MySqlCommand command = mydbCon.CreateCommand();


        command.CommandText = "SELECT * FROM yourTable WHERE hwid = GetHDDSerial";
        IDataReader reader = command.ExecuteReader();

`

【问题讨论】:

  • 你有什么错误吗?
  • 错误信息是什么?
  • 即使你的连接工作正常,很像你的查询也不会工作,因为你需要在你的值 GetHDDSerial 周围加上引号。
  • 可能有很多原因。例如您没有在 MySQL 服务器上授予从您运行 C# 代码的机器进行连接的权限。 MySQL 服务器运行未打开的防火墙。您的网络上的其他地方有防火墙。您输入了错误的 MySQL 服务器的 IP 地址。 MySQL 服务器当前未运行等 - 请检查所有这些。您收到的确切错误消息可以提供更多信息。如果您有错误,请复制粘贴,以便其他人也能看到。
  • 为了给您一个很好的答案,如果您还没有看过How to Ask,它可能会对我们有所帮助。如果您可以提供minimal reproducible example,它可能也很有用。

标签: c# mysql visual-studio


【解决方案1】:

可能是连接字符串的格式不符合 MySQL 连接器的要求。 MySQL 文档显示“uid”而不是 User,以及“pwd”而不是 Password。 https://dev.mysql.com/doc/connector-net/en/connector-net-programming-connecting-connection-string.html

【讨论】:

    【解决方案2】:

    这应该可以满足您的需要:

    string connectionString = "Server=SomeServer;Database=i got you this is notthe real database;User ID=same;Password=same for password;";
    
    using (MySqlConnection connection = new MySqlConnection(connectionString))
    {
        using (MySqlCommand command = new MySqlCommand())
        {
            string sql = "SELECT * FROM yourTable WHERE hwid = @val1";
            command.Connection = connection;
            command.CommandType = CommandType.Text;
            command.CommandText = sql;
            command.Parameters.AddWithValue("@val1", "GetHDDSerial");
    
            connection.Open();
            using (MySqlDataAdapter adapter = new MySqlDataAdapter())
            {
                using (DataSet ds = new DataSet())
                {
                    adapter.SelectCommand = command;
                    adapter.Fill(ds);
    
                    if (ds.Tables.Count > 0)
                    {
                        DataTable dt = ds.Tables[0];
    
                        foreach (DataRow row in dt.Rows)
                        {
                            // Do something here. You can access the data like this:
                            // row["Id"] or whatever your field names are.
                            // int id = (int) row["Id"];
                            // Of course, I don't know your field names, so you'll have to complete this.
                        }
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多