C#Mono MySql.data什么都没写出来

时间:2013-12-05 07:27:28

标签: c# monodevelop

我有问题,然后我使用mysql.data在我的控制台中没有写出来。甚至不是我的console.writeline(“hej”);

代码:

public static void Main (string[] args) {
    Console.WriteLine("hej");


    string connString = "datasource=localhost;port3306;database=program;uid=joffe;password=hej123;";

    MySqlConnection conn = new MySqlConnection(connString);
    MySqlCommand command = conn.CreateCommand();
    command.CommandText = "SELECT * FROM tabell";
    try{
        conn.Open();
        MySqlDataReader reader = command.ExecuteReader();
        while (reader.Read()){
            Console.WriteLine(reader["Nick"]);
        }

    conn.Close();
    }
    catch(Exception ex){
        Console.WriteLine(ex);
    }
}

输出:

Console image 1

但如果我在console.writeline()之后评论所有内容;写出第一个字符串。

一切评论: Console image 2 working

1 个答案:

答案 0 :(得分:0)

string connString = "Server=localhost;port3306;Database=program;Uid=joffe;Pwd=hej123;";
var sql = "SELECT * FROM tabell";
using(var connection = new MySqlConnection(connString))
{
    connection.Open();
    using (var command = new MySqlCommand(sql, connection))
    {
        var reader = command.ExecuteReader();
        while (reader.Read()) Console.WriteLine((string)reader["Nick"]);
    }
    connection.Close();
}

我一直在测试它并发现问题,在你的解决方案中你需要将MySql.Data DLL的引用复制到Local,为此,打开references文件夹,在MySQL.Data上单击一次,然后在右边你会看到一个带有齿轮的图标,单击它,然后制作MySql.Data的本地副本

enter image description here