⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainform.cpp

📁 C++ BUILDER精彩编程实例集锦(源码)3 第五部分 系统编程 第六部分 数据库应用
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Mainform.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
   this->Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
   if(this->OpenDialog1->Execute())
   {
     this->Edit2->Text=this->OpenDialog1->FileName;
   }
}
//---------------------------------------------------------------------------
#include "Registry.hpp"
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{//创建ODBC数据源
   if(this->Edit2->Text.Length()<1)
     return;
   TRegistry *SystemReg=new TRegistry();
   SystemReg->RootKey = HKEY_LOCAL_MACHINE; //设置根键值为HKEY_LOCAL_MACHINE
   //找到Software\ODBC\ODBC.INI\ODBC 数据源
   if(SystemReg->OpenKey("Software\\ODBC\\ODBC.INI\\ODBC Data Sources", true))
   {//注册数据源名称
      SystemReg->WriteString(this->Edit1->Text, "Microsoft Access Driver (*.mdb)" );
      SystemReg->CloseKey();
   }
   else
   { //创建键值失败
     throw Exception("创建Access数据库ODBC数据源失败!");
     return;
   }
   //写入数据源配置信息
   if(SystemReg->OpenKey("Software\\ODBC\\ODBC.INI\\" + this->Edit1->Text, true))
   {
      SystemReg->WriteString("DBQ", this->Edit2->Text);//数据库文件全路径
      SystemReg->WriteString("Description",  this->Edit3->Text);//数据源描述
      char buf[MAX_PATH];
      ::GetSystemDirectory(buf, MAX_PATH);
      SystemReg->WriteString("Driver", AnsiString(buf) + "file://odbcjt32.dll/" );//驱动程序DLL文件
      SystemReg->WriteInteger("DriverId", 25 );     //驱动程序标识
      SystemReg->WriteString("FIL", "Ms Access;" ); //Filter依据
      SystemReg->WriteInteger("SafeTransaction", 0 );//支持的事务操作数目
      SystemReg->WriteString("UID", this->Edit4->Text);//用户名称
      SystemReg->WriteString("PWD", this->Edit5->Text);//用户密码
      BYTE bData = 0;
      SystemReg->WriteBinaryData("Exclusive", &bData, 1);   //独占方式
      SystemReg->WriteBinaryData("ReadOnly", &bData, 1 );   //只读方式
      SystemReg->CloseKey();
   }
   else
   {
     throw Exception("创建Access数据库ODBC数据源失败!");
     return;
   };
  //找到或创建Software\ODBC\ODBC.INI\MyAccess\Engines\Jet,写入数据源数据库引擎配置信息
   if (SystemReg->OpenKey("Software\\ODBC\\ODBC.INI\\" + this->Edit1->Text + "file://Engines//Jet", true))
    {
      SystemReg->WriteString( "ImplicitCommitSync", "Yes");
      SystemReg->WriteInteger("MaxBufferSize", 2048 );//缓冲区大小
      SystemReg->WriteInteger("PageTimeout", 10 );//页超时
      SystemReg->WriteInteger("Threads", 3 );//支持的线程数目
      SystemReg->WriteString("UserCommitSync", "Yes");
      SystemReg->CloseKey();
    }
    else
    {
      throw Exception("创建Access数据库ODBC数据源失败!");
      return;
    }
    MessageBox(Handle,"创建Access数据库ODBC数据源成功!","信息提示",MB_OK);
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -