我有一个使用数据库的 C# 方法。现在我尝试返回该值,但因为该字符串是在 while
循环中创建的,所以它说它不存在。有没有一种方法可以返回字符串 kampioen
及其在 while read 循环中创建的值?
public string getkampioen(string selecteditem)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
string query = ("select * FROM clubs where naam = '" + selecteditem + "'");
MySqlCommand cmd = new MySqlCommand(query, connection);
MySqlDataReader Reader = cmd.ExecuteReader();
while (Reader.Read())
{
string kampioen = (string)Reader["aantalkampioenschappen"].ToString();
}
return kampioen;
}
}
最佳答案
您可以在 while 循环之前声明字符串。
public string getkampioen(string selecteditem)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
string query = ("select * FROM clubs where naam = '" + selecteditem + "'");
MySqlCommand cmd = new MySqlCommand(query, connection);
MySqlDataReader Reader = cmd.ExecuteReader();
string kampioen = "";
while (Reader.Read())
{
kampioen = (string)Reader["aantalkampioenschappen"].ToString();
}
return kampioen;
}
}
关于c# - 在 while 循环中返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53656025/