|
-
February 19th, 2009, 12:58 AM
#1
decent documentation for C# / MySQL
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.
-
February 19th, 2009, 03:27 AM
#2
Re: decent documentation for C# / MySQL
 Originally Posted by Red Squirrel
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?
-
February 19th, 2009, 03:58 PM
#3
Re: decent documentation for C# / MySQL
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.
Last edited by MadHatter; February 19th, 2009 at 04:00 PM.
-
February 19th, 2009, 08:28 PM
#4
Re: decent documentation for C# / MySQL
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.
-
February 19th, 2009, 10:21 PM
#5
Re: decent documentation for C# / MySQL
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:
Code:
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).
Last edited by Arjay; February 19th, 2009 at 10:23 PM.
-
February 20th, 2009, 12:56 AM
#6
Re: decent documentation for C# / MySQL
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.
-
February 20th, 2009, 01:02 AM
#7
Re: decent documentation for C# / MySQL
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|