通过ODBC访问MySQL数据库时出现问题

时间:2022-12-12 04:36:32
OdbcDataReader q = dbc.Query("SELECT * FROM `posts` WHERE `id`=" + id.ToString());
if (q.RecordsAffected < 1)
{
    this.Exists = false;
}
else
{
    this.Exists = true;
    this.Author = q.GetString(6);
}

The server returns No data exists for the row/column.

服务器返回行/列不存在数据。

My database table is structured like this (screencap from phpMyAdmin) http://1.img.anyhub.net/1243660397_6485910f8b3fc9ee3e2d93831ad554fd.png

我的数据库表结构如下(来自phpMyAdmin的screencap)http://1.img.anyhub.net/1243660397_6485910f8b3fc9ee3e2d93831ad554fd.png

By the way, dbc is just a database connection class of mine; the Query() function is this:

顺便说一句,dbc只是我的数据库连接类; Query()函数是这样的:

public OdbcDataReader Query(string QueryStr)
{
    OdbcCommand q = new OdbcCommand(QueryStr, conn);
    OdbcDataReader r = q.ExecuteReader();
    return r;
}

1 个解决方案

#1


I think you should use DataReader.Read method before you can get data from it.

我认为您应该使用DataReader.Read方法,然后才能从中获取数据。

q.Read(); 
this.Author = q.GetString(6);

And I recommend using a using block with you DataReader and Command objects

我建议在DataReader和Command对象中使用using块

#1


I think you should use DataReader.Read method before you can get data from it.

我认为您应该使用DataReader.Read方法,然后才能从中获取数据。

q.Read(); 
this.Author = q.GetString(6);

And I recommend using a using block with you DataReader and Command objects

我建议在DataReader和Command对象中使用using块