|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThe main goal of writing this article is to explain how we can mix the .NET library with the MFC library in very easy steps, gaining the benefits of .NET in unmanaged code such as MFC projects. So for this reason I’ve added ADO.NET in a MFC project to use the simple, type-safe and powerful functionalities of that library.ADO.NETIn designing tools and technologies to meet the needs of today's developer, Microsoft recognized that an entirely new programming model for data access was needed, one that is built upon the .NET Framework. Building on the .NET Framework ensures that the data access technology would be uniform, components would share a common type system, design patterns and naming conventions. ADO.NET was designed to meet the needs of this new programming model: disconnected data architecture, tight integration with XML, common data representation with the ability to combine data from multiple and varied data sources, and optimized facilities for interacting with a database, all native to the .NET Framework. Mixing Managed and Unmanaged codeTo use managed code in a MFC projects we must change some MFC project settings and add some code to declare the .NET library. Changing Configuration To alter the configuration of your MFC project to use unmanaged code, change the following items in the Configuration Properties dialog box. General - Use Managed Extensions = YES C/C++ - General - Debug Information Format = Program Database ( C/C++ - General - Compile As Managed = Assembly Support ( C/C++ - Code Generation - Enable Minimal Rebuild = No C/C++ - Code Generation - Basic Runtime Checks = Default The #pragma managed
#using <mscorlib.dll>
#using <System.dll>
#using <system.data.dll>
#using namespace System;
#using namespace System::Data;
#using namespace System::Data::OleDb;
The Because we are using ADO.NET in our project so we have to add __gc Pointers in Unmanaged ClassesTo declare a managed pointer as member in a class we must use gcroot<OleDbConnection*> m_OleDbConnection;
gcroot<OleDbCommand*> m_OleDb;
gcroot<OleDbDataReader*> m_Reader;
Writing Managed CodeNow it is time to write managed code in an unmanaged function. #pragma push_macro("new")
#undef new
try
{
m_OleDbConnection = new OleDbConnection(
S"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\test.mdb"
);
m_OleDbConnection->Open(); // Open up the connection
m_OleDb = new OleDbCommand(S"select * from Persons", m_OleDbConnection);
m_Reader = m_OleDb->ExecuteReader();
int count = 0;
while (m_Reader->Read())
{
AddToList(count,m_Reader->get_Item("First Name")->ToString(),
m_Reader->get_Item("Last Name")->ToString(),
m_Reader->get_Item("Phone Number")->ToString()
);
count++;
}
}
catch(Exception *e)
{
AfxMessageBox(CString(e->ToString()));
}
__finally
{
m_Reader->Close();
m_OleDbConnection->Close();
}
#pragma pop_macro("new")
the This was a small sample that indicates how we can use .NET library in MFC based projects. By following this sample You can write more complicated projects and using more benefits of .NET. It is easy to cope with and entirely feasible to extend your own MFC applications with managed code.
|
||||||||||||||||||||||