CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2007
    Posts
    609

    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.
    http://www.uovalor.com :: Free UO Server

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: decent documentation for C# / MySQL

    Quote Originally Posted by Red Squirrel View Post
    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?

  3. #3
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    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.

  4. #4
    Join Date
    Jul 2007
    Posts
    609

    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.
    http://www.uovalor.com :: Free UO Server

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  6. #6
    Join Date
    Jul 2007
    Posts
    609

    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.
    http://www.uovalor.com :: Free UO Server

  7. #7
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    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
  •  





Click Here to Expand Forum to Full Width

Featured