2013-03-10 69 views
0

嗨,我试图从VC++女士Access数据库中检索数据。由于我是VC++的新手,请帮助我。在VC++窗体中检索MsAccess数据到ComboBox应用程序

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

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"

回答

0

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

更改线路

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

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

+0

超级...它的工作原理就像一个魅力...谢谢... SoupyC – 2013-03-11 16:03:31

相关问题