以下是网学网为您推荐的ASP.net-在managed C++应用中使用ADO.NET,希望本篇文章对您学习有所帮助。
摘要:#using // Add access to .NET Framework classes. #using #using using namespace System; using namespace System::Data::ADO; |
int main(void) { ADOConnection* connection; // ADO connection. ADOCommand* command; // ADO command ADODataReader* dataReader; // ADO data reader try { // Create connection, set connection string and open connection to // specified database. connection = new ADOConnection(); connection->ConnectionString = S"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\Data\\grocertogo.mdb;Persist Security Info=False"; connection->Open(); // Create command and get data reader by executing this command. command = new ADOCommand(S"SELECT ProductName, UnitPrice FROM Products", connection); command->Execute(&dataReader); // Print table header Console::WriteLine(S"_____________________________________"); Console::WriteLine(S"Product | Price"); Console::WriteLine(S"_____________________________________"); // Iterate through rows set and print data. while(dataReader->Read()) Console::WriteLine(S"{0, -30}| {1}", dataReader->get_Item("ProductName"), dataReader->get_Item("UnitPrice")); // Print table footer. Console::WriteLine(S"_____________________________________"); // Close DataReader dataReader->Close(); // Close connection. connection->Close(); } catch(Exception* e) { // Print error message and close connection. Console::WriteLine("Error occured: {0}", e->Message); if (dataReader && !dataReader->IsClosed) dataReader->Close(); if (connection->State == DBObjectState::Open) connection->Close(); } Console::WriteLine("Press ENTER to continue"); Console::ReadLine(); return 0; } |