Click to See Complete Forum and Search --> : decent documentation for C# / MySQL


Red Squirrel
February 18th, 2009, 11:58 PM
I need to connect to a MySQL db with C#. I have yet to find any decent doc. This is the best thing I could find:

http://bitdaddys.com/MySQL-ConnectorNet.html

Problem is, C# totally crashes out if say, the server can't be reached. I want to get more info on error handling. I am also at complete lost as to how actual queries are made. Like how do I do fetch_row and stuff?

Just need more documentation. Thanks.

dannystommen
February 19th, 2009, 02:27 AM
I need to connect to a MySQL db with C#. I have yet to find any decent doc. This is the best thing I could find:

http://bitdaddys.com/MySQL-ConnectorNet.html


I believe I used the same example when I tried to connect to MySQL. And it worked fine.

Why can't the server be reached? What is the exception?

MadHatter
February 19th, 2009, 02:58 PM
it works exactly the same way as the SqlClient... failing to handle exceptions is going to cause problems no matter what provider you use.

the driver wasn't written against mysql's standard API, it was written to operate the same as every other ADO.NET database api.

fetching rows is done with a DataAdapter. google SqlClient, and add a My in front of what you read, and that's how you use mysql's provider.

Red Squirrel
February 19th, 2009, 07:28 PM
Hmm so Sqlclient documentation should apply to MySql?

As for not reaching the server, that could be anything, such as a network outage. I just don't want my entire application to crash just because the server can't be reached. So I'd like to know how I can better handle such situation. I'm guessing a try/catch statement is needed just not sure how it's suppose to be handled for this class.

Arjay
February 19th, 2009, 09:21 PM
Along with the try/catch block do yourself a favor and get into the practice of using a 'using' block.

Modifying the sample code from the above link, you end up with:

try
{
string myConString = "SERVER=localhost;DATABASE=mydatabase;UID=testuser;PASSWORD=testpassword;";

using( MySqlConnection connection = new MySqlConnection( myConString) )
{
using( MySqlCommand command = connection.CreateCommand( ) )
{
command.CommandText = "select * from mycustomers";
connection.Open();

MySqlDataReader reader = command.ExecuteReader( );

while ( reader.Read( ) )
{
string thisrow = "";

for ( int i= 0; i < reader.FieldCount; i++ )
{
thisrow += reader.GetValue( i ).ToString( ) + ",";
listBox1.Items.Add( thisrow );
}
}
}
}
catch( Exception ex )
{
// catch exception (or more granular exceptions here)
}
}

Comments:
1) You'll notice that I don't explicitly call Close on the MySqlConnection or MySqlCommand objects. This is because when the using block leaves scope, it automatically calls Dispose( ) which calls Close( ). The benefit is that this will be performed even if an exception occurs. Even if no exception occurs, helps clean up the database resources as soon as possible.
2) More of a style issue, but in .Net local variables are declared in camelCase rather than in PascalCase - as such, I've renamed the Reader variable as lowercase reader. You know, when in Rome....
3) In order for the using block to work, the folks that created the MySql .net code need to implement IDisposable. I attempted to check the code in the link, but in order to download it, I would have had to register as a user. At any rate, if you attempt to use a using block and receive a compile error stating that MySqlConnection or MySqlCommand must be derived from IDisposable, you'll know that they didn't implement it this way (if that's the case, you may want to file a bug with them).

Red Squirrel
February 19th, 2009, 11:56 PM
Thanks I got it working, did not do the using thing but might consider that as well. Managed to write a row to a database yay! I have not tried selects yet but that example seems to be clear.

MadHatter
February 20th, 2009, 12:02 AM
I had the same questions about 5 or 6 years ago when I used that provider (this was back before mysql bought it from the guy). I asked for samples / examples, and he (the author of the provider) just told me to google for sqlclient code and use it.

gotta love the ado.net interfaces.