【发布时间】:2012-10-10 08:02:56
【问题描述】:
我有一个连接到 MySQL 数据库的小项目。在 Windows 7 中它就像一个魅力,但是当我在 Mono 中做完全相同的事情时(在本地使用 monodevelop 和一个测试 mysql 服务器)我得到一个NullReferenceException。我查看了连接器手册和已知错误,但我没有任何线索。
也许有人知道为什么会发生这种情况:
Click here for larger picture
(来自 monodevelop 的截图,带有错误详情)
private static IDbConnection connection;
private static IDbCommand command;
private static IDataReader reader;
public static void Init()
{
try
{
string myConnectionString = "SERVER=127.0.0.1;PORT=3306;" +
"Database=game;" +
"UID=root;" +
"PASSWORD=root; Pooling=false";
connection = new MySqlConnection(myConnectionString);
connection.ConnectionString = myConnectionString;
}
catch (Exception e)
{
Console.WriteLine("Error at creating connection: " +e.Message);
}
}
public static void LoadMaps()
{
connection.Open();
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM maps";
reader = command.ExecuteReader();
while (reader.Read())
{
int ID = reader.GetInt32(0);
string Name = reader.GetString(1);
int width = reader.GetInt32(2);
int height = reader.GetInt32(3);
Console.WriteLine(ID + " " + Name);
}
reader.Close();
reader = null;
command.Dispose();
command = null;
connection.Close();
}
编辑:很难相信,但有时它会起作用,我真的不知道为什么,尤其是因为我什么都没改变。奇怪……
【问题讨论】:
-
FWIW,我真的很难相信这是你做错了什么。我怀疑 MySQL 连接器存在错误。我一直在谷歌搜索
get_ConnectionString NullReferenceException MysqlConnection,运气不佳。 -
你试过什么来调试这个问题?我们看不到您的代码。这很可能是您的代码问题,这意味着您从未成功建立连接。
-
我已经用我使用的代码更新了我的帖子。在 LoadMaps 之前调用 Init。感谢您的大力支持。
-
@Markus 您是否在 MySql 连接器开发团队中打开了一个错误?我很确定这应该是你的下一步。在此之前,请确保您运行的是最新版本,并且该错误仍然可以重现。
-
@Lynn:我想要,但问题消失得无影无踪。我什么都没改变。当此问题第二次发生时,我将打开一个错误。但是现在,我无法重现它....奇怪
标签: c# mysql mono mysql-connector