-1

i am making a program in visual studio code with C#, it is a paid program so it needs an hwid system. Basically i want it to check if your computer HWID exists in the HWID table in my users database. But it says it can't connect to the database. Can you help me? This is my code.`

        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();

`

  • 4
    Have you got any errors? – Hary Nov 16 '18 at 14:09
  • 5
    What is the error message? – Dave Stokes Nov 16 '18 at 14:09
  • 1
    OK, so on the design side of things, if you are distributing an application with a connection string in it to a database hosted on some server, you will be hacked. Once your application ships, people can get at this. – Knelis Nov 16 '18 at 14:10
  • 2
    Even if you connection works, very like your query will not work because you need quotes around your value `GetHDDSerial`. – Filburt Nov 16 '18 at 14:13
  • 4
    There can be many reasons. e.g. You have not given permission on the MySQL server to connect from the machine you run your C# code on. The MySQL server runs a firewall that is not opened. There's a firewall somewhere else on your network. You typed the IP address of the MySQL server wrong. The MySQL server is not currently running etc - please check all that. The exact error message you get could provide more information. If you have an error, copy paste it so others can see it too. – nos Nov 16 '18 at 14:14
  • 3
    To give you a great answer, it might help us if you have a glance at [ask] if you haven't already. It might be also useful if you could provide a [mcve]. – Mat Nov 16 '18 at 14:14
  • See [How to get the connection String from a database](https://stackoverflow.com/q/10479763/205233) for some smart and simple ways to get a valid connection string - the linked post is about Sql Server but most of the solutions apply to MySql as well. – Filburt Nov 16 '18 at 14:22
  • I see you didn't even bother to read the tag descriptions. – EJoshuaS - Stand with Ukraine Nov 17 '18 at 23:58

2 Answers2

0

It could be that the connection string isn't formatted the way the MySQL connector wants it. The MySQL documentation shows "uid" instead of User, and "pwd" instead of Password. https://dev.mysql.com/doc/connector-net/en/connector-net-programming-connecting-connection-string.html

0

This should do what you need:

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.
                    }
                }
            }
        }
    }
}
Icemanind
  • 47,519
  • 50
  • 171
  • 296