[.net] Accessing MySql databse with C#

Started by
4 comments, last by Spodi 15 years, 10 months ago
I've got an idea to see if I can put together a basic web based game and I'm trying to see if I can get some client side C# code to access and read/write to the database on the web sever. This is my first attempt at any kind of coding that looks outside the actual application so I'm finding it a bit tricky. Right now I'm trying to get some C# code to add the text "Successful" to a field in a table I've created in the database. I've got some code off the MSDN for OleDb that looked like it might do what I wanted, and I've done some googling etc but as everything about this is new to me I'm finding it difficult to fully understand a lot of what I'm reading. Anyway, this is what I've got:

private void connectToDatabase_Click(object sender, EventArgs e)
        {
            string connectionString = "Provider=MySQLProv;Data Source=mysql4.freehostia.com;User Id=myDatabaseName;Password=myDatabsePassword;";
            string insertSQL = "INSERT INTO test (test_field) VALUES ('Successful')";
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                OleDbCommand command = new OleDbCommand(insertSQL);

                // Set the Connection to the new OleDbConnection.
                command.Connection = connection;

                // Open the connection and execute the insert command.
                try
                {
                    connection.Open();
                    command.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                // The connection is automatically closed when the
                // code exits the using block.
            }
        }
It compiles, but when I run the app and press the button to access the databse nothing happens and no data is added to the database. I'd appreciate it if someone could give me a basic run through of how to access a MySql database, how to read values from it and how to write values to it. Or at the least point me in the right direction. Thanks. And I hope this is the right forum for this...
------Studying Games Computing at the University of Lincoln. First year. Teaching us teh newbie stuff :
Advertisement
Have you considered debugging to find out where the problem is?

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

The easiest way to deal with SQL from C#, in my experience, is using the new LINQ to SQL features from .NET 3.0/3.5 ... Provided you don't mind the dependency on the framework version, Scott Guthrie has some good (if not slightly outdated) tutorials on the subject. Best of luck.
Did you try the MySQL Connector/Net? I've been using it for a while and it works magic.
NetGore - Open source multiplayer RPG engine
I looked into MySql Connector and I think I've got the code right. I kept getting an "Unable to connect to any of the specified MySQL hosts." error though every time I tried to connect to my database. It isn't a programming error though, I finally discovered.

My host doesn't allow connections of that sort to the database from outside your server side account (i.e. PHP).

Teh gay...
------Studying Games Computing at the University of Lincoln. First year. Teaching us teh newbie stuff :
Yeah, thats not uncommon for this exact reason. Many hosts will restrict connections to the database from the address your server is on to prevent people using it for non-website reasons.

If you want to use a stand-alone client, you might just have to find yourself a new database host or host your own. Theres some free MySQL database hosts out there.
NetGore - Open source multiplayer RPG engine

This topic is closed to new replies.

Advertisement