Sign in to follow this  

[.net] Accessing MySql databse with C#

This topic is 3484 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3484 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this