Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ACCESDB中的Join查询出现IndexOutOfRangeException错误_C#_Ms Access - Fatal编程技术网

C# ACCESDB中的Join查询出现IndexOutOfRangeException错误

C# ACCESDB中的Join查询出现IndexOutOfRangeException错误,c#,ms-access,C#,Ms Access,我相信所有的表名都可以。我想得到每个队的球员名单 public List<Speler> getSpelersPerPloeg(int ploegID) { List<Speler> spelersLijst = new List<Speler>(); connection.Open(); OleDbCommand command = new OleDbCommand("select * from p

我相信所有的表名都可以。我想得到每个队的球员名单

 public List<Speler> getSpelersPerPloeg(int ploegID)
    {
        List<Speler> spelersLijst = new List<Speler>();

        connection.Open();
        OleDbCommand command = new OleDbCommand("select * from proj1Speler inner join proj1Ploeg on proj1Speler.ploegID = proj1Ploeg.ploegID where proj1Ploeg.ploegID = @ploegID", connection);
        command.Parameters.Add(new OleDbParameter("@ploegID", ploegID));

        OleDbDataReader dataReader = command.ExecuteReader();
        while (dataReader.Read())
        {
            spelersLijst.Add(new Speler((int)dataReader["spelerID"], dataReader["spelerNaam"].ToString(), (int)dataReader["ploegID"], dataReader["ploegNaam"].ToString(), (int)dataReader["spelerTypeID"]));
        }

        dataReader.Close();
        connection.Close();

        return spelersLijst;
    }
public List getSpelersPerPloeg(int-ploegID)
{
List spelersLijst=新列表();
connection.Open();
OleDbCommand命令=新的OleDbCommand(“从proj1Speler.ploegID=proj1Ploeg.ploegID上的proj1Speler内部联接proj1Ploeg中选择*,其中proj1Ploeg.ploegID=@ploegID”,连接);
Add(新的OleDbParameter(“@ploegID”,ploegID));
OleDbDataReader dataReader=command.ExecuteReader();
while(dataReader.Read())
{
添加(新的Speler((int)数据读取器[“spelerID”]、数据读取器[“spelerNaam”].ToString()、(int)数据读取器[“ploegID”]、数据读取器[“ploegNaam”].ToString()、(int)数据读取器[“spelerTypeID”]);
}
dataReader.Close();
connection.Close();
返回spelersLijst;
}
它在这一行“spelersLijst.Add(new Speler)(…”,有什么想法吗


有趣的是,sql server的工作没有任何问题,也许我的访问关系是错误的?

您正在使用*连接两个表,这样,两个表中的字段都会从查询中返回

但是,如果在同一查询中的两个不同表中有两个同名字段,会发生什么情况?会发生一些自动重命名,将tablename添加到两个相同的字段名中以消除列名的歧义

因此,当您尝试使用简单名称(不使用表名)时,会出现错误。我敢打赌,罪魁祸首是出现在两个表中的“ploegID”字段

我建议只返回代码真正需要的字段名

OleDbCommand command = new OleDbCommand(@"select ps.spelerID, ps.spelerNaam, 
                                       pp.ploegID, pp.ploegNaam, ps.spelerTypeID
                                       from proj1Speler ps 
                                       inner join proj1Ploeg pp 
                                       on ps.ploegID = pp.ploegID 
                                       where pp.ploegID = @ploegID", connection);
 command.Parameters.Add(new OleDbParameter("@ploegID", ploegID));
 OleDbDataReader dataReader = command.ExecuteReader();

(注意,我不确定哪个表中包含哪个字段,这只是为了了解情况)

哪一行确实抛出了错误?spelersLijst.Add(new Speler((…)不,构造器很好,当我连接我的sql server数据库时,构造器可以工作,这一定是AccesDBI的问题,我不明白,因为我使用的sql server与它工作的表和内容相同!是的,他抱怨ploegID字段,而该字段在两个表中。这很有效,谢谢你!!但是为什么会出现错误呢我在sql server中使用的ion?嗯,我能说什么?Access!=sql server