Sql Datarow System.IndexOutOfRangeException

Sql Datarow System.IndexOutOfRangeException,sql,database,c++-cli,Sql,Database,C++ Cli,试图访问表行,但它变为IndexOutOfRangeException数据库只有一个表和两行 SqlConnection ^ conDatabase = gcnew SqlConnection(L"Data Source=(LocalDB)\v11.0;AttachDbFileName='C:/Users/pcusername/Documents/Visual Studio 2010/Projects/nv/nv/project/project/vn.mdf';Integrate

试图访问表行,但它变为IndexOutOfRangeException数据库只有一个表和两行

SqlConnection ^ conDatabase =
        gcnew SqlConnection(L"Data Source=(LocalDB)\v11.0;AttachDbFileName='C:/Users/pcusername/Documents/Visual Studio 2010/Projects/nv/nv/project/project/vn.mdf';Integrated Security='True'");
    SqlCommand ^ cmdDatabase =
        gcnew SqlCommand(L"SELECT * FROM Table;", conDatabase);

    dataSet1 = gcnew DataSet("Table");
    SqlDataAdapter ^ sda = gcnew SqlDataAdapter();


    sda->SelectCommand = cmdDatabase;
    dataSet1->Tables;

    DataRow ^ recEmployee = dataSet1->Tables[1]->Rows[0];/*System.IndexOutOfRangeException*/

C++集合是基于零的,因此您应该使用
表[0]
返回第一个表,而不是
表[1]
尝试:

DataRow ^ recEmployee = dataSet1->Tables[0]->Rows[0];

<>因为C++是基于零索引的,第一个表将在索引0,而不是索引。在尝试检索表和行之前,最好先检查它们是否存在。确保您确实从数据库中获得了任何结果。如果您没有得到任何信息,它可能是sql语句中的分号。有些连接不允许SQL中的半冒号。

这是C++ + CLI而不是C++。请适当地标记您的问题。谢谢您的更正。很抱歉。谢谢您,请再问一个问题。DataRow^recEmployee=dataSet1->Tables[0]->Rows[0]处的System.IndexOutOfRangeException;这次是数据行。@user3541906您确定有行吗?是否检查了调试器中的行数?