How To Close The OLEDB Connection? [Access Database Engine 2010]

Paranthaman 1 Reputation point
2022-10-15T00:39:37.313+00:00

Hi,
I am using vs-2015. Managed C++/Cli Windows Forms Applications. I have Form1-MdiContainer & Form2. From Form1_Button_Click() accessing Form2. In Form2_Load() I have OLEDBConnection. When I press Form1-Button_Click(),
First time it's working good. Also when I re-click() the same Form1-Button() the whole application is getting closed. I analysed that when I close the OLEDBConnection, the whole program is also getting closed. And the same code working good in c#. I can't understand my mistakes..! Thanks.

Note:- For First time when I click the Form1-Button no problem it's working good. But for re-click() whole application is getting closed().

Another Note :- While I use access db engine 2010 getting the above problem. But after installing accessdbengine 2016
the problem get solved and working fine.

Any superiors can help me?

Thanks Again

My Codes
Form1-MdiContainer()
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Form2^ MyStForm = Form2::GetForm(true, this);
MyStForm->MdiParent = this;
MyStForm->FormBorderStyle = System::Windows::Forms::FormBorderStyle::None;
MyStForm->Dock = DockStyle::Fill;
MyStForm->Show();
}
Form2-Top side
public ref class Form2 : public System::Windows::Forms::Form
{
public: static Form2^ Form2::_instance = nullptr;
public: static Form2^ Form2::GetForm(bool IsMDIChild, System::Windows::Forms::Form^ MyInstFrm) {
if (_instance == nullptr)
_instance = gcnew Form2();
if (_instance->IsDisposed)
_instance = gcnew Form2();
if (IsMDIChild)
_instance->MdiParent = MyInstFrm;
return _instance;
}
blah..blah...blah...
}

Form2-Load()  
private: System::Void Form2_Load(System::Object^  sender, System::EventArgs^  e) {  
    String^ MyStrConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + MyExcelFile + "; Extended Properties=\"Excel 12.0; HDR=YES; OLE DB Services=-1; \"";  
    String^ MyExcelFile="C:\Students\Names.xlsx";  
    String^ MyExcelSheet = "[Sheet1$]";  
    String^ MySQLSelect = "select * from " + MyExcelSheet;  
    System::Data::OleDb::OleDbConnection^ Cn1 = nullptr;  
    Cn1 = gcnew System::Data::OleDb::OleDbConnection();  
    Cn1->ConnectionString = MyStrConn;  
    System::Data::OleDb::OleDbCommand^ MyCmd = nullptr;  
    MyCmd = gcnew System::Data::OleDb::OleDbCommand();  
    MyCmd->CommandText = MySQLSelect;  
    MyCmd->Connection = Cn1;  
    Cn1->Open();  
        System::Data::OleDb::OleDbDataAdapter^ Da1 = gcnew System::Data::OleDb::OleDbDataAdapter(MyCmd);  
        Da1->Fill(MyDataTable);  
    Cn1->Close(); //????????????????? getting stuck  
    if (Cn1 != nullptr){  
        MyCmd->Cancel();  
    }  
    //Cn1->ConnectionString = nullptr;  
    //Cn1->ReleaseObjectPool();  
    MyCmd = nullptr;  
    Cn1 = nullptr;  
  
}   

Thanks

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,839 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,647 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,545 questions
{count} votes

1 answer

Sort by: Most helpful
  1. YujianYao-MSFT 4,281 Reputation points Microsoft Vendor
    2022-10-18T06:46:23.873+00:00

    Hi @Paranthaman ,

    I am using Access db engine 2010, and after slightly modifying part of the code, clicking button1 repeatedly will not report an error.

    I suggest you to refer to the code:

    private:  
    		System::Void MyForm_Load(System::Object^ sender,  
    			System::EventArgs^ e) {  
    			DataTable^ MyDataTable = gcnew DataTable();  
    			String^ MyExcelFile = "E:\\Test.xlsx";  
    			String^ MyStrConn =  
    				"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +  
    				MyExcelFile +  
    				"; Extended Properties=\"Excel 12.0; HDR=YES; OLE DB "  
    				"Services=-1; \"";  
    			String^ MyExcelSheet = "[Sheet1$]";  
    			String^ MySQLSelect = "select * from " + MyExcelSheet;  
    			System::Data::OleDb::OleDbConnection^ Cn1 = nullptr;  
    			Cn1 = gcnew System::Data::OleDb::OleDbConnection(MyStrConn);  
    			System::Data::OleDb::OleDbCommand^ MyCmd = nullptr;  
    			MyCmd =  
    				gcnew System::Data::OleDb::OleDbCommand(MySQLSelect, Cn1);  
    			Cn1->Open();  
    			System::Data::OleDb::OleDbDataAdapter^ Da1 =  
    				gcnew System::Data::OleDb::OleDbDataAdapter(MyCmd);  
    			Da1->Fill(MyDataTable);  
    			Cn1->Close();  
    		}  
    	private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {  
    		MyForm2^ MyStForm =gcnew MyForm2();  
    		MyStForm->MdiParent = this->MdiParent;  
    		MyStForm->FormBorderStyle = System::Windows::Forms::FormBorderStyle::None;  
    		MyStForm->Dock = DockStyle::Fill;  
    		MyStForm->Show();  
    	}  
    

    Best regards,

    Elya


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments