C++: read access data using ADOX.DLL and System::Data::OleDb
#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); }
};
}
C++: read access data using ADOX.DLL and System::Data::OleDb的更多相关文章
- 报错:无法将类型"System.Data.EntityState"隐式转换为"System.Data.Entity.EntityState"
报错:无法将类型"System.Data.EntityState"隐式转换为"System.Data.Entity.EntityState". 出错语句停留 ...
- MVC ---- 无法将类型"System.Data.EntityState"隐式转换为"System.Data.Entity.EntityState"
1.EF 5.0解决方法 先卸载EF:Uninstall-Package EntityFramework -Force 在安装EF5.0:Install-Package EntityFramework ...
- 『开源重编译』System.Data.SQLite.dll 自适应 x86 x64 AnyCPU 重编译
背景: > System.Data.SQLite.dll 程序集 不能良好的支持 AngCPU 格式 System.Data.SQLite.dll 在 适应 x86 和 x64 有三个方案: & ...
- C++: read SQL server data using System::Data::SqlClient
stdafx.h: // stdafx.h : include file for standard system include files, // or project specific inclu ...
- 在使用EFCodeFirst中出现类型“System.Data.Objects.ObjectContext”在未被引用的程序集中定义的解决方案
我安装了EF4.1版本,并在一个项目中映射一个数据库并生成了EF的MODEL实体层 测试:在Default.aspx页面上加了个GridView控件,后台进行绑定 using System; usin ...
- System.Data.SQLite安装的相关问题
在使用System.Data.SQLite的过程中,遇到各种问题,特此记录下.(都被搞的折寿了,不仔细看文档的下场!) 1.选对.net Framework的版本. 2.X64和X86的问题,如果项目 ...
- .net引用System.Data.SQLite操作SQLite
之所以要做这个笔记,是因为在.NET中使用System.Data.SQLite的时候,遇到了些问题,这些问题是相对于引用其他dll没有遇到过的,所以作个笔记,记录一下. 简单起见,首先建立一个控制台项 ...
- 【WinForm】“System.Data.SqlClient.SqlConnection”的类型初始值设定项引发异常,无法识别的配置节 system.serviceModel
出现问题的原因: 在本机上没有出现问题,让一个同事测试的时候,在另外一台电脑上出现连接数据库失败,系统不能打开的问题 在网上搜了一下,有说是数据库连接字符串错误的,有说app.config文件配置不匹 ...
- 使用EF6.0出现:CS0029 无法将类型“System.Data.Entity.Core.Objects.ObjectContext”隐式转换为“System.Data.Objects.ObjectContext”错误
这是因为EF6.0重构了一些命名空间后,和VS原有的实体数据模型模板不一致了(ObjectContext context = ((IObjectContextAdapter)dataContext). ...
随机推荐
- 移动h5开发资源整理
这2年来,移动h5开发逐渐成为一种主流,也不断趋向于成熟.硬件和浏览器的不断更新,曾经的浏览器兼容也不再是开发者的噩梦. 接触h5开发一年多,从最初的新手到现在,陆陆续续遇到过很多坑.这里把想到的一些 ...
- SqlServer 错误1053:服务并未及时响应启动或控制请求
sqlserver 的登录用户修改成域账户后,启动不了 解决方法: 计算器管理选择管理员组 将域账户加入到管理员组即可
- fir.im Weekly - 从零开始创建 Android 新项目
今年的 Google I/O 大会上,人工智能和虚拟现实的产品发布让我们对未来多了几分惊喜.对于开发者部分,Google 发布了 Android N 系统,感受最深的是全新的 Android Stud ...
- salesforce 零基础开发入门学习(七)PickList的value值获取
之前介绍过PickList类型的声明以及赋值,但是如何取出呢?一个sObject对象可以理解为一条数据.通过sObject直接取恐怕很难做到,因为他只会显示一个值.这时候就要用到Schema命名空间中 ...
- IOS开发之控件篇UITabBarControllor第二章 - 遮掩TableView问题
在IOS7.0以后UITabBar 里面放入一个UITableView会出现一个问题,Table会被TabBar掩盖了,当移动到最后一项的时候,永远看不到,如下面的例子,总共是99项,但是只能显示到9 ...
- Java时间日期格式转换
1.这个是系统自动默认的时间格式,或者说是美式格式: Long time = System.currentTimeMillis(); Date date = new Da ...
- AngularJS快速入门01-基础
记得第一次听说AngularJS这项很赞的Web的前端技术,那时还是2014年,年中时我们我的一个大牛兄弟当时去面试时,被问到了是否熟悉该技术,当时他了解和使用的技术比较多.我们询问他面试情况时,他给 ...
- jS事件之网站常用效果汇总
下拉菜单 <!--简单的设置了样式,方便起见,将style和script写到同一个文档,着重练习事件基础--> <!DOCTYPE html> <html> < ...
- hibernate(十)双向关联关系的CRUD
本文链接:http://www.orlion.ml/28/ 一.保存 1. 假设一个group有多个user,一个user只属于一个group,当保存user对象到数据库中时可以 User u = n ...
- JSP网站开发基础总结《二》
有了上一篇的学习,我相信大家对于JSP一定有了一定的认识,从今天开始我们真正开启JSP模式,如果你有HTML的基础,那学起JSP来也就方便了很多了,首先JSP做为网站开发语言,它与HTML有很多相似的 ...