C++: read access data using ADOX.DLL and System::Data::OleDb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | #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","geovindu@163.com"); //playerTable->Rows->Add("聚文","涂","geovindu@qq.com"); // //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); } }; } |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
Cpp programming
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2013-08-05 Css:Conditional comments 条件注释
2013-08-05 SQL Server迭代求和