gpt4 book ai didi

c# - C#中的SQL从数据库中选择

转载 作者:行者123 更新时间:2023-11-30 19:18:29 25 4
gpt4 key购买 nike

我在 C# 中有以下代码:

string query = "SELECT * FROM Zboruri WHERE cod_aeroport = " + country;

using (var command = new SqlCommand(query, connection))
{
var list = new ArrayList();
var reader = command.ExecuteReader();

if (reader.HasRows)
{
while (reader.Read())
{
string data1 = reader.GetString(1);
string data2 = reader.GetString(2);
list.Add(cod_aeroport);
list.Add(data1);
list.Add(data2);
}
}
else
{
string raspuns = "nu este info";
list.Add(raspuns);
}

reader.Close();
connection.Close();
return list;
}

我的数据库表有这些列:

data1(numeric(18,0))
data2(numeric(18,0))
...........

它给我错误:

Index was outside the bounds of the array.

在这行代码中:

string data2 = reader.GetString(2); 

如何修复错误?

最佳答案

索引从 0 开始,而不是 1

string data1 = reader.GetString(0);
string data2 = reader.GetString(1);

关于c# - C#中的SQL从数据库中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12651853/

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