【发布时间】:2019-05-14 15:41:26
【问题描述】:
我正在为我的学习开发一个应用程序。现在我刚刚启动了一个应用程序,其中我有一个包含足球联赛和俱乐部等的数据库。现在我有一个俱乐部列表,现在我试图添加更多联赛的球员,然后只有 1 个。但是当我得到这个错误时做同样的事情然后做之前。 这是无效列表的代码:
public List<Competitie> GetAllCompetities()
{
List<Competitie> Competitie = new List<Competitie>();
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
string query = "Select * from competitie";
MySqlCommand selectallcompetitiecommand = new MySqlCommand(query, connection);
MySqlDataReader reader = selectallcompetitiecommand.ExecuteReader();
while (reader.Read())
{
Competitie comp = new Competitie();
comp.IdCompetitie = reader.GetInt32(0);
comp.NaamCompetitie = reader.GetString(1);
Competitie.Add(comp);
}
}
return Competitie;
}
然后这是正在运行的俱乐部的代码:
public List<Clubs> GetAllClubs(string selecteditem)
{ //Zorgt voor alle dingen van de tabel clubs.
List<Clubs> Clubs = new List<Clubs>();
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
string query = "Select * from databasevoetbal.clubs where competitie.naamcompetie = '" + selecteditem + "' and clubs.idcompetitie = competitie.idcompetitie";
MySqlCommand selectAllClubsCommand = new MySqlCommand(query, connection);
MySqlDataReader reader = selectAllClubsCommand.ExecuteReader();
while (reader.Read())
{
Clubs Club = new Clubs();
Club.idClubs = reader.GetInt32(0);
Club.NaamClubs = reader.GetString(1);
Club.aantalkampioenschappen = reader.GetInt32(2);
Club.opgericht = reader.GetInt32(3);
Club.idcompetitie = reader.GetInt32(4);
Clubs.Add(Club);
}
}
return Clubs;
}
这是相同的代码,只有俱乐部中的查询使用列表框中的选定项目,但任何人都知道为什么我在第一个列表中收到此错误:
错误 CS0050 可访问性不一致:返回类型 '
List<Competitie>' 比方法更难访问 'DatabaseManager.GetAllCompetities()'
类代码:
class Competitie
{
public int IdCompetitie { get; set; }
public string NaamCompetitie { get; set; }
public override string ToString()
{
return string.Format("{0}", NaamCompetitie);
}
}
【问题讨论】:
-
你在哪里定义了
Competitie类?是public,还是不是?如果不是,那么这可能会解释错误,因为可以调用 GetAllCompetities() 方法的代码(因为它是public)有可能无法访问该方法返回的类。显然这不是一个合乎逻辑的情况 -
不要忘记关闭任何打开的连接。连接.Close();
-
使用它会自动关闭连接
-
@SerhatOz 我想你不明白
using做了什么。 -
是的,所以它必须是
public class Competitie。否则,它默认为internal(即只能在它编译到的程序集中访问)。正如错误所说,该类必须至少与返回它的方法一样可访问
标签: c# .net access-modifiers