无法识别mysql请求中的问题

时间:2013-12-03 20:39:22

标签: c# mysql

我有两个函数,一个工作正常,另一个不是,代码是相同的,除了非功能代码在数据库请求中有一个WHERE,我只是不能让它工作,我很绝望。 cID是一个给函数的字符串,这是唯一正确的东西

Geotag c = new Geotag();
String myConnection = WebConfigurationManager.ConnectionStrings["mydatabase"].ConnectionString;
MySqlConnection myConn = new MySqlConnection(myConnection);
String strSQL = "SELECT Id,geotag,item,date,nameofplace FROM geolist WHERE Id = 'cID';";
MySqlCommand myCommand = new MySqlCommand();
myCommand.Connection = myConn;
myCommand.CommandText = strSQL;
try
{
    myConn.Open();
    MySqlDataReader myReader;
    myReader = myCommand.ExecuteReader();
    myReader.Read();

    while (myReader.Read())
    {        

我的读者是空的,所以它跳过并继续......为什么?我尝试了我所相信的一切,比如没有' '围绕cID字符串。

如果我删除了WHERE = cID,我将直接给出一个返回的对象,这是列表中的第一个,所以我知道代码可以工作。数据库具有匹配的字符串,即使我将字符串硬编码到WHERE中它仍然无法工作,所以我在这里缺少其他东西。

1 个答案:

答案 0 :(得分:4)

您正在比较一个Id字段,因此您可能会返回一条记录但是您正在调用Read方法两次

myReader.Read();

while (myReader.Read()) // <--- empty