程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++: read access data,readaccessdata

C++: read access data,readaccessdata

編輯:C++入門知識

C++: read access data,readaccessdata


#pragma once
#include "Form2.h"

namespace cdemo {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::Data::OleDb;
	using namespace System::IO;

	/// <summary>
	/// Form1 摘要
	///http://1code.codeplex.com/
	///http://www.codeproject.com/Tips/810596/Csharp-VB-NET-Cplusplus-CLI-Create-read-and-write
	/// 警告: 如果更改此類的名稱,則需要更改
	///          與此類所依賴的所有 .resx 文件關聯的托管資源編譯器工具的
	///          “資源文件名”屬性。否則,
	///          設計器將不能與此窗體的關聯
	///          本地化資源正確交互。
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: 在此處添加構造函數代碼
			//
		}

	protected:
		/// <summary>
		/// 清理所有正在使用的資源。
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::DataGridView^  dataGridView1;
	private: System::Windows::Forms::ComboBox^  comboBoxTables;
	private: System::Windows::Forms::Button^  buttonOK;
	protected: 

	private:
		/// <summary>
		/// 必需的設計器變量。
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// 設計器支持所需的方法 - 不要
		/// 使用代碼編輯器修改此方法的內容。
		/// </summary>
		void InitializeComponent(void)
		{
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->dataGridView1 = (gcnew System::Windows::Forms::DataGridView());
			this->comboBoxTables = (gcnew System::Windows::Forms::ComboBox());
			this->buttonOK = (gcnew System::Windows::Forms::Button());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->dataGridView1))->BeginInit();
			this->SuspendLayout();
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(533, 268);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(41, 12);
			this->label1->TabIndex = 0;
			this->label1->Text = L"label1";
			// 
			// dataGridView1
			// 
			this->dataGridView1->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
			this->dataGridView1->Location = System::Drawing::Point(32, 84);
			this->dataGridView1->Name = L"dataGridView1";
			this->dataGridView1->RowTemplate->Height = 23;
			this->dataGridView1->Size = System::Drawing::Size(478, 344);
			this->dataGridView1->TabIndex = 1;
			this->dataGridView1->CellDoubleClick += gcnew System::Windows::Forms::DataGridViewCellEventHandler(this, &Form1::dataGridView1_CellDoubleClick);
			// 
			// comboBoxTables
			// 
			this->comboBoxTables->FormattingEnabled = true;
			this->comboBoxTables->Location = System::Drawing::Point(87, 27);
			this->comboBoxTables->Name = L"comboBoxTables";
			this->comboBoxTables->Size = System::Drawing::Size(121, 20);
			this->comboBoxTables->TabIndex = 2;
			// 
			// buttonOK
			// 
			this->buttonOK->Location = System::Drawing::Point(228, 27);
			this->buttonOK->Name = L"buttonOK";
			this->buttonOK->Size = System::Drawing::Size(75, 23);
			this->buttonOK->TabIndex = 3;
			this->buttonOK->Text = L"button1";
			this->buttonOK->UseVisualStyleBackColor = true;
			this->buttonOK->Click += gcnew System::EventHandler(this, &Form1::buttonOK_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(586, 485);
			this->Controls->Add(this->buttonOK);
			this->Controls->Add(this->comboBoxTables);
			this->Controls->Add(this->dataGridView1);
			this->Controls->Add(this->label1);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->FormClosed += gcnew System::Windows::Forms::FormClosedEventHandler(this, &Form1::Form1_FormClosed);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->dataGridView1))->EndInit();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion



		String ^DBPath;

        OleDbConnection ^conn;
        OleDbDataAdapter ^adapter;
        DataTable ^dtMain;

	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {


                DBPath = Application::StartupPath + "\\test.mdb";
				 // create DB via ADOX if not exists
				 if (!File::Exists(DBPath)) {
					 ADOX::Catalog ^cat = gcnew ADOX::Catalog(); //添加引用 Interop.ADOX.2.8.dll
                     cat->Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DBPath);
                     cat = nullptr;
				 }

				 // connect to DB
            	 conn = gcnew OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DBPath);
            	 conn->Open();
     //         try {
					//OleDbCommand ^cmd = gcnew OleDbCommand("CREATE TABLE [geovindu] ([id] COUNTER PRIMARY KEY, [FirstName] MEMO, [Age] INT,[Email] MEMO);", conn); //創建表
     //       		cmd->ExecuteNonQuery();
					//delete cmd;
				 //} catch (Exception ^ex) {if (ex != nullptr) ex = nullptr; }

				 // get all tables from DB
				 DataTable ^dt = conn->GetSchema("Tables");
				 for (int i = 0; i < dt->Rows->Count; i++) {
                    if (dt->Rows[i]->ItemArray[dt->Columns->IndexOf("TABLE_TYPE")]->ToString() == "TABLE") {
                        comboBoxTables->Items->Add(dt->Rows[i]->ItemArray[dt->Columns->IndexOf("TABLE_NAME")]->ToString());
                    }


                 }
				 delete dt;



				 int i,pr=0;
				 for(i=1;i<=10;i++)				 
					 pr=pr*i;
                    
				 //char s="pr=%d";

				 this->label1->Text= pr.ToString();

				    
				 
				 //設置dataGridView的一些屬性
    //        dataGridView1->Dock=DockStyle::Fill; 
    //        dataGridView1->AutoResizeColumns();
    //        dataGridView1->AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode::AllCells;
    //        dataGridView1->EnableHeadersVisualStyles=true;/*根據應用程序來設置的,如果為false則根據自己的設置來繪制*/

    //        //設置表頭的屬性
    //        DataGridViewCellStyle^ headerStyle=gcnew DataGridViewCellStyle;
    //        headerStyle->Font=gcnew System::Drawing::Font("Times New Roman",12,FontStyle::Bold);
    //        headerStyle->BackColor=Color::AliceBlue;
    //        headerStyle->ForeColor=Color::BurlyWood;
    //        dataGridView1->ColumnHeadersDefaultCellStyle=headerStyle;

    //        dataGridView1->AutoResizeColumnHeadersHeight();

				//DataSet^ myDataSet = gcnew DataSet("Game Data");
				//myDataSet->Tables->Add("Player");
				//myDataSet->Tables->Add("Score");
 
				//DataTable^ playerTable = myDataSet->Tables["Player"];
				//DataColumn^ firstNameColumn1 = playerTable->Columns->Add("First Name",String::typeid);
				//DataColumn^ lastNameColumn1 = playerTable->Columns->Add("Last Name",String::typeid);
				//playerTable->Columns->Add("Email", String::typeid);
				//playerTable->Rows->Add("geovin","du","[email protected]");
				//playerTable->Rows->Add("聚文","塗","[email protected]");
    //             //1.
				////dataGridView1->DataSource=myDataSet->Tables["Player"];//playerTable;
				////2.
    //            dataGridView1->DataSource=playerTable;
				//dataGridView1->Refresh();
				////dataGridView1->DataBindings::get;

			 }
             
			 ////
	        private: System::Void dataGridView1_CellDoubleClick(System::Object^  sender, System::Windows::Forms::DataGridViewCellEventArgs^  e) {

             Form2 ^ frm2 = gcnew Form2();
			 frm2->Text="編輯資料";
			 frm2->textBox1->Text=dataGridView1->Rows[e->RowIndex]->Cells["FirstName"]->Value->ToString();
			 frm2->textBox2->Text=dataGridView1->Rows[e->RowIndex]->Cells["Email"]->Value->ToString();
             frm2->Show();
             //this->Hide();
                
                 
                
			 }
			
					 ///
	         private: System::Void buttonOK_Click(System::Object^  sender, System::EventArgs^  e) {

                if (comboBoxTables->SelectedItem == nullptr) return;

                adapter = gcnew OleDbDataAdapter("SELECT * FROM [" + comboBoxTables->SelectedItem->ToString() + "]", conn);

             gcnew OleDbCommandBuilder(adapter);

             dtMain = gcnew DataTable();
             adapter->Fill(dtMain);
             dtMain->Columns["id"]->ReadOnly = true; // deprecate id field edit to prevent exceptions
             dataGridView1->DataSource = dtMain;

			 }
					  ///
			private: System::Void Form1_FormClosed(System::Object^  sender, System::Windows::Forms::FormClosedEventArgs^  e) {

           if (adapter == nullptr) return;

             adapter->Update(dtMain);

		 }
};
}

  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved