C# 如何使用C连接到mysql并获取json数据?
如上所述: 我使用C连接到MySQL数据库,我想读取JSON数据类型。 我使用MySqlCommand.ExecuteReader方法:C# 如何使用C连接到mysql并获取json数据?,c#,mysql,json,C#,Mysql,Json,如上所述: 我使用C连接到MySQL数据库,我想读取JSON数据类型。 我使用MySqlCommand.ExecuteReader方法: 难道我不能在这里使用ExecuteReader吗?请更正您的sql命令 String sql_Command = @"SELECT id FROM orders.jsontest"; 更正您的sql命令 String sql_Command = @"SELECT id FROM orders.jsontest"; 我知道,但如果您的问题尚未找到解决方案
难道我不能在这里使用ExecuteReader吗?请更正您的sql命令
String sql_Command = @"SELECT id FROM orders.jsontest";
更正您的sql命令
String sql_Command = @"SELECT id FROM orders.jsontest";
我知道,但如果您的问题尚未找到解决方案,或者其他人遇到类似问题,则以下代码应适用于您:
private IEnumerable<int> GetIds()
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
string commandText = @"SELECT id FROM jsontest"; // Assuming that `orders` is your database, then you do not need to specify it here.
using (MySqlCommand command = new MySqlCommand(commandText, connection))
{
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
yield return reader.GetInt32(0);
}
}
}
}
只要MySqlDataReader仍然可以读取有效的结果和此行,它就会从jsontest表中获取结果
while (reader.Read())
yield return reader.GetInt32(0);
它指示读取器获取并以Int32 int的形式返回所获取表的每条记录。如果表列类型不是int,则需要更改此设置。
因为您只选择了一列,即SELECT id,所以参数为0,因为您获取的结果表只包含一列
此外,在代码中,您似乎希望将结果作为DataTable获取;如果是这样,您应该使用MySqlDataAdapter而不是MySqlDataReader,如下所示:
DataTable resultTable = new DataTable("ResultTable");
MySqlDataAdapter adapter = new MySqlDataAdapter(command);
adapter.Fill(table);
我知道,但如果您的问题尚未找到解决方案,或者其他人遇到类似问题,则以下代码应适用于您:
private IEnumerable<int> GetIds()
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
string commandText = @"SELECT id FROM jsontest"; // Assuming that `orders` is your database, then you do not need to specify it here.
using (MySqlCommand command = new MySqlCommand(commandText, connection))
{
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
yield return reader.GetInt32(0);
}
}
}
}
只要MySqlDataReader仍然可以读取有效的结果和此行,它就会从jsontest表中获取结果
while (reader.Read())
yield return reader.GetInt32(0);
它指示读取器获取并以Int32 int的形式返回所获取表的每条记录。如果表列类型不是int,则需要更改此设置。
因为您只选择了一列,即SELECT id,所以参数为0,因为您获取的结果表只包含一列
此外,在代码中,您似乎希望将结果作为DataTable获取;如果是这样,您应该使用MySqlDataAdapter而不是MySqlDataReader,如下所示:
DataTable resultTable = new DataTable("ResultTable");
MySqlDataAdapter adapter = new MySqlDataAdapter(command);
adapter.Fill(table);
有一个额外的;在stringI中,我不会放一个,但怀疑它会呕吐,因为itIt的show{在命令执行过程中遇到致命错误。}有一个额外的;在字符串中,我不会放一个,但怀疑它会呕吐,因为itIt的show{在命令执行过程中遇到致命错误。}始终显示确切的错误消息it的show{在命令执行过程中遇到致命错误。}当我的程序运行到MySqlDataReader sdr=scmd.ExecuteReader;当我的程序运行到MySqlDataReader sdr=scmd.ExecuteReader时,始终显示准确的错误消息it's show{在命令执行期间遇到致命错误。};