尝试通过C#在数据库上选择时IndexOutOfRangeException

FRP*_*P72 5 c# sql sql-server unity-game-engine

对不起,如果这听起来很无聊.我正在尝试使用C#和SQL Server中的数据库在Unity上制作测验游戏.在这一部分,我试图显示最高分,所以,我做了一个代码来选择数据库中的最大值:

  public bool BuscarScoreFinal1()
    {
        SqlDataReader dataReader = null;

        if (RunQuery(string.Format("SELECT MAX (PlayerScore1) FROM ScoreQuiz1", tableScoreQuiz1), ref dataReader))
        {
            while (dataReader.Read())
            {
                id1 = dataReader.GetInt32(dataReader.GetOrdinal("ID"));
                PlayerName1 = dataReader.GetString(dataReader.GetOrdinal("PlayerName1"));
                PlayerScore1 = dataReader.GetInt32(dataReader.GetOrdinal("PlayerScore1"));

                break;
            }
        }

        if (dataReader != null) dataReader.Close();

        return true;
    }
Run Code Online (Sandbox Code Playgroud)

然后,我尝试在此脚本中的UI文本中打印数据:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UpdateScore : MonoBehaviour
{

    public GameObject Question3Display;
    public Text yourText;
    public bool Buscardados13;

    // Use this for initialization
    void Start()
    {
        Buscardados13 = GameObject.Find("DatabaseConnection").GetComponent<DatabaseInterface>().Connect() == true;
        Buscardados13 = GameObject.Find("DatabaseConnection").GetComponent<DatabaseInterface>().BuscarScoreFinal1() == true;
        yourText.text = Question3Display.GetComponent<DatabaseInterface>().PlayerScore1.ToString();
    }

    // Update is called once per frame
    void Update()
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

它给了我这个错误:

IndexOutOfRangeException:ID System.Data.ProviderBase.BasicFieldNameLookup.GetOrdinal(System.String fieldName的)(在<8012ff544f1c4cb384c200861f770215>:0)System.Data.SqlClient.SqlDataReader.GetOrdinal(System.String名)(在<8012ff544f1c4cb384c200861f770215>:0)DatabaseInterface .BuscarScoreFinal1()(在Assets/Scripts/DatabaseInterface.cs:621)UpdateScore.Start()(在Assets/Scripts/UpdateScore.cs:17)

我认为这个错误不是选择错误表的连接问题,因为如果我尝试不同的查询(例如,只是一个简单的选择),它的工作没有错误.我不认为我写的查询不好,因为它在SQL Server中工作.这是我想要访问的表格;

这是我正在使用的表格的屏幕截图

Gor*_*off 6

我只能推测 - 因为你没有显示数据 - 但我很确定你想要:

SELECT TOP (1) Id, PlayerName1, PlayerScore1
FROM ScoreQuiz1
ORDER BY PlayerScore1 DESC;
Run Code Online (Sandbox Code Playgroud)

您无法真正访问未包含在内的列SELECT.