MySqlCommand().ExecuteReader().GetString() 不起作用 [英] MySqlCommand().ExecuteReader().GetString() does not work
本文介绍了MySqlCommand().ExecuteReader().GetString() 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 C# 语言的 MySql.我正在尝试从我的数据库中获取一些数据.字段的组织方式如下:
I'm working with MySql in C# language. I'm trying get some data out of my database. The fields are organized like:
foo baa
38737 22222
如果我的哈希值等于 baa
,我需要获取 foo
的值我试过这个:我的代码(不工作)
I need to get value of foo
if my hash is equal to baa
I tried this:
My code(not working)
MySqlConnection con = new MySqlConnection("Server=localhost;Database=test;Uid=user;Pwd=pass;");
con.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = string.Format("SELECT * FROM info WHERE baa = '{0}'", Hash); ;
cmd.Connection = con;
MySqlDataReader reader = cmd.ExecuteReader();
String res = reader.GetString(0);
我收到以下错误:
Invalid attempt to access a field before calling Read()
有人能指出我的错误吗?提前致谢.
Can someone point out my error? Thanks in advance.
推荐答案
您缺少一个 reader.Read()
调用:
MySqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
String res = reader.GetString(0);
//...
}
这篇关于MySqlCommand().ExecuteReader().GetString() 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文