使用 C# 和 MySQL
当我选择组合框值时,相应的值应该显示在文本框中
C# 代码。
cmd2 = new OdbcCommand("Select name from users where username='" + cmbuser.Text + "'", con);
dr= cmd2.ExecuteReader();
while (dr.Read())
{
txtusername.Text = dr("user");
}
以上代码在 VB.Net 中有效,但在 C# 中显示错误为错误“dr' is a 'field' but is used like a 'method'”
这行显示错误 txtusername.Text = dr("user");
如何解决这个错误,我的代码有什么问题。
需要 C# 代码帮助
最佳答案
在c#中使用方括号:
txtusername.Text = dr["user"];
编辑:您必须在之后将对象转换为所需的类型。
关于c# - 使用 DataReader 添加列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3388312/