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 summary // http://1code.codeplex.com////http://www.codeproject.com/Tips/8105 96/Csharp-VB-NET-Cplusplus-CLI-Create-read-and-write // warning: If you change the name of this class, you need to change the // and all the dependencies of this class. the "resource file name" attribute of the managed resource compiler tool associated with the resx file. Otherwise, the /// designer cannot be associated with the form. // The localized resource cannot interact correctly. /// </Summary> public ref class Form1: public System: Windows: Forms: Form {public: Form1 (void) {InitializeComponent (); //// TODO: add the constructor code here //} protected: // <summary> // clear all resources in use. /// </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> /// required designer variables. /// </Summary> System: ComponentModel: Container ^ components; # pragma region Windows Form Designer generated code // <summary> // The Designer supports the required methods-do not // use the code editor to modify the content of this method. /// </Summary> void InitializeComponent (void) {this-> label1 = (gcnew System: Windows: Forms: Label ()); this-> maid = (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-> Suspe NdLayout (); // 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-> maid-> Location = System: Drawing: Point (32, 84); this-> maid-> Name = L "maid "; this-> maid-> RowTemplate-> Height = 23; this-> maid-> Size = System: Drawing: Size (478,344 ); this-> maid-> TabIndex = 1; this-> maid-> CellDoubleClick + = gcnew System: Windows: Forms: Maid (this, & Form1: Maid CellDoubleClick ); /// c OmboBoxTables // 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: S Ize (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 <S Ystem: ComponentModel: ISupportInitialize ^> (this-> maid)-> EndInit (); this-> ResumeLayout (false); this-> maid ();} # pragma endregionString ^ 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: catox (); // Add reference 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 KE Y, [FirstName] MEMO, [Age] INT, [Email] MEMO); ", conn); // create a table // 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 (); // set some attributes of the dataGridView // dataGridView1-> Dock = DockStyle: Fill; // dataGridView1-> AutoResizeColumns (); // dataGridView1-> AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode: AllCells; // dataGridView1-> EnableHeadersVisualStyles = true;/* Set Based on the application, if this parameter is set to false, you can set * // you can set the attributes of the header. // 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 ", "geovindu@163.com"); // playerTable-> Rows-> Add ("juwen", "tu", "geovindu@qq.com"); // 1. /// maid-> DataSource = myDataSet-> Tables ["Player"]; // playerTable; // 2. // maid-> DataSource = playerTable; // maid (); // maid-> DataBindings: get;} // private: System :: void maid (System: Object ^ sender, System: Windows: Forms: Maid () {Form2 ^ frm2 = gcnew Form2 (); frm2-> Text = "Edit materials "; frm2-> textBox1-> Text = maid-> Rows [e-> RowIndex]-> Cells ["FirstName"]-> Value-> ToString (); frm2-> textBox2-> Text = maid-> 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 );}};}