Hithere Paperbag
Hithere Paperbag

Reputation: 2808

Showing the output query?

I'm trying to show the output of the query SELECT * FROM phpbb_topics I'm running this from a C# console app, using the MySql connector api.
The query works fine when I run it phpmyadmin, and gives me a list of forum topics.

When I run it in the C# app remotely though, it doesn't seem to do anything.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql;
using MySql.Data; 

namespace SqlConsoleSlr
{
    class Program
    {
        static void Main(string[] args)
        {
            MySql.Data.MySqlClient.MySqlConnection mycon =
            new  MySql.Data.MySqlClient.MySqlConnection(GetConnectionString());
            Console.WriteLine(GetConnectionString());

            if (mycon.State != System.Data.ConnectionState.Open)

                try
                {
                    mycon.Open();
                    Console.WriteLine("we're in");
                }

                catch (System.Data.SqlClient.SqlException ex)
                {
                    Console.WriteLine(ex);
                }

                MySql.Data.MySqlClient.MySqlCommand msc = new MySql.Data.MySqlClient.MySqlCommand("SELECT * FROM `phpbb_topics` ");

            Console.WriteLine("completed");  /// gets to here, but doesn't show output of msc

            Console.ReadLine();
        }

        public static string GetConnectionString()
        {
            string hostname = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;";
            string username = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx;";
            string dbname = "xxxxxxxxxxxxxxxxxxxxxxxx;";
            string password = "xxxxxxxxxxxxxxxxxxxxxxxxxxx;";

            string s = "Server=" + hostname + "User=" + username + "Database=" + dbname + "Password=" + password;

            return s; 
        }
    }
}

Is there some method I need to call on the query object? The only one I could find is msc.BeginExecuteReader();, but that doesn't seem to change the execution either.

Upvotes: 1

Views: 2291

Answers (2)

Muhammad Hani
Muhammad Hani

Reputation: 8664

You have to create an object of MySQL Data Reader then execute the command.

MySql.Data.MySqlClient.MySqlCommand msc = new MySql.Data.MySqlClient.MySqlCommand("SELECT * FROM `phpbb_topics` ");

    MySql.Data.MySqlClient.MySqlDataReader read = msqlCommand.ExecuteReader();

    if(read != null)
    {
    //Sample output
    while (read.Read())
            {
                int TopicID = Convert.ToInt32(read["Topic_ID"]);
                string TopicName = Convert.ToString(read["Topic_Name"]);
                Console.WriteLine(TopicID.ToString() + " : " + TopicName);
            }

    }

Upvotes: 0

Taicho
Taicho

Reputation: 143

You will need to create a MYSQL Data Reader object.

MySql.Data.MySqlClient.MySqlDataReader read = msqlCommand.ExecuteReader();

and then you can output the records after you read.read() all of the records.

Upvotes: 1

Related Questions