gpt4 book ai didi

c# - 无法找到列

转载 作者:行者123 更新时间:2023-11-29 08:07:26 25 4
gpt4 key购买 nike

为什么下面的陈述会变成错误?

    static void Main(string[] args)
{
string connString = "Server=localhost;Port=3306;Database=connection;Uid=root;password=;";
MySqlConnection conn = new MySqlConnection(connString);
MySqlCommand command = conn.CreateCommand();
command.CommandText = "Select number from user where id=1";
try
{
conn.Open();
}
catch (Exception ex) {
Console.WriteLine(ex.Message);
}
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader["text"].ToString());

}
Console.ReadLine();
}

这是我的数据库:数据库名称:连接表名称:用户

number: 18.81
id: 1

最佳答案

您当前仅从数据库中选择number 字段,但尝试从结果集中读取text 字段。将 text 字段添加到 select 语句(如果您想从数据库获取文本):

command.CommandText = "Select text from user where id=1";

或者从结果集中读取number(如果您描述的表结构正确,我认为您需要这个解决方案):

Console.WriteLine(reader["number"].ToString());

关于c# - 无法找到列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22424611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com