1

我正在尝试显示SELECT * FROM phpbb_topics 从 C# 控制台应用程序运行的查询的输出,using the MySql connector api.
该查询在我运行时运行良好phpmyadmin,并为我提供了论坛主题列表。

但是,当我在 C# 应用程序中远程运行它时,它似乎什么也没做。

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; 
        }
    }
}

我需要在查询对象上调用一些方法吗?我能找到的唯一一个是msc.BeginExecuteReader();,但这似乎也没有改变执行。

4

2 回答 2

1

您将需要创建一个 MYSQL 数据读取器对象。

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

然后您可以在read.read()所有记录之后输出记录。

于 2013-01-19T01:28:55.263 回答
0

您必须创建 MySQL Data Reader 的对象,然后执行命令。

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);
            }

    }
于 2013-01-19T01:42:41.090 回答