0

嗨,我正在尝试从 Ms Access 数据库中检索数据VC++。由于我是新手VC++,请帮助我。

这是我到目前为止编写的代码。

System::Data::DataSet^ ds=gcnew System::Data::DataSet();

        OleDbConnection ^ con=gcnew OleDbConnection("Provider= Microsoft.ACE.OLEDB.12.0;Data source=dbmc.accdb; Persist Security Info=True");
        OleDbCommand^ com =gcnew OleDbCommand();
        OleDbDataReader^ myReader;
        com->CommandText ="SELECT name FROM Table1";
        com->Connection = con;
        con->Open();
        try
        {
            myReader=com->ExecuteReader();
            while(myReader->Read())
            {
                String^ vName = myReader->GetString('name');
                comboBox1->Items->Add(vName);
                myReader->Close();
            }
        }
        catch(Exception^ex)
        {
            MessageBox::Show(ex->Message);
        }   

当我运行这个程序时,我得到一个错误"Index Out of Bound"

4

1 回答 1

0

GetString()方法将列号的整数作为参数,而不是列名(完整文档请参见此处)。

换行

String^ vName = myReader->GetString('name');

String^ vName = myReader->GetString(0);

于 2013-03-11T13:24:04.333 回答