Accessing WordPress MySQL Database via Data Connection in Visual Studio 2010 using C#

So I’ve Googled and followed countless tutorials, read documentation on MySQL, Visual Studio, WordPress, and Plesk. I try to leave posting on this site as the final option and it seems it has come to that.

As simply put as possible, I want to display the list of categories from my WordPress blog on another ASP.NET/C# website that I have. There isn’t an RSS of the names of the categories specifically otherwise I would have used the XML from the RSS feed with XSLT to display the category list that way. I came to the conclusion that I had to access my WordPress database (where the category names are stored) directly from my other website.

To do so, I’ve downloaded and installed the MySQL Connector Net 6.5.4 on my localmachine (C: Drive) allowing the “MySQL Database” option to be available when adding a Data Connection in Visual Studio. I also, downloaded the ‘noinstall’ of the MySQL Connector Net 6.5.4 by selecting “.NET & MONO” from the “Select Platform:” dropdown on MySql.com in order to add a reference to the DLL in my C# code.

I get the same error when either attempting to run the code or adding a data connection. Adding the data connection was originally to test the actual connection to the database after I got the error from running the code.

Error: “Unable to connect to any of the specified MySQL hosts.”

Below is the code I have in my ‘Default.aspx.cs’ file:

...
using MySql.Data.MySqlClient;

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
...
        String MyConnectionString = "SERVER=localhost;UID=testusername;PASSWORD=testpassword;DATABASE=testdbname;";
        MySqlConnection connection = new MySqlConnection(MyConnectionString);
        MySqlCommand command = connection.CreateCommand();
        command.CommandText = "SELECT * FROM wp_terms wt " +
                      "INNER JOIN wp_term_taxonomy wtt ON wt.term_id=wtt.term_id " +
                      "WHERE wtt.taxonomy='category' ORDER BY wt.name";
        try
        {
            connection.Open();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        MySqlDataReader Reader = command.ExecuteReader();
        while (Reader.Read())
        {
            string thisrow = "";
            for (int i = 0; i < Reader.FieldCount; i++)
            {
                thisrow += Reader.GetValue(i).ToString() + ",";
                Label1.Text += thisrow;
            }
        }
        connection.Close();
    }
}

At this point, I am convinced that the problem lies with the credentials that I am entering in String MyConnectionString = “SERVER=localhost;UID=testusername;PASSWORD=testpassword;DATABASE=testdbname;”; or when attempting to ‘Add a Data Connection’. However, I checked the wp-config.php file within my WordPress installation in the blog’s root directory and am, therefore, positive that I am providing the correct credentials. The only one I am slightly unsure of is the Database server name [localhost].

Is there something else I am not doing correctly? Is it possible the WordPress databases don’t allow for remote access?

Any help would be greatly appreciated! Thank you in advance!

P.S. I’ve posted this question on Stack Overflow as well.

Solutions Collecting From Web of "Accessing WordPress MySQL Database via Data Connection in Visual Studio 2010 using C#"

There isn’t an RSS of the names of the categories specifically otherwise I would have used the XML from the RSS feed with XSLT to display the category list that way.

There is a handy function named add_feed(). You can create any feed or other output with it. Should be useful in your case. And it is pretty simple too.

Sample plugin

<?php
/**
 * Plugin Name: Category Feed
 * Description: XML output for the categories located on /catxml/
 */

add_action( 'init', array ( 'T5_Cat_Feed', 'init' ) );

class T5_Cat_Feed
{
    public static function init()
    {
        add_feed( 'catxml', array ( __CLASS__, 'display' ) );
    }

    public static function display()
    {
        $args = array(
            'hide_empty'               => 0,
            'hierarchical'             => 0,
            'taxonomy'                 => 'category'
        );
        $cats = get_categories( $args );

        if ( empty ( $cats ) or ! is_array( $cats ) )
        {
            return;
        }

        header( 'Content-Type: text/xml' );
        print '<categories>';

        foreach ( $cats as $cat )
        {
            $url = get_category_link( $cat->term_id );
            print "<category>
<name>$cat->cat_name</name>
<id>$cat->term_id</id>
<url>$url</url>
<description>$cat->category_description</description>
<parent>$cat->parent</parent>
<count>$cat->count</count>
</category>"
            ;
        }
        print '</categories>';
    }
}

Install as a plugin on your blog, visit the permalink settings page once to refresh the rewrite cache, and go to example.com/catxml/ or example.com/?feed=catxml. You get a simple XML file with category data here.