MyCode代码生成器,自动生成BCB版数据库操作代码(ADO),无需手工编写,只需简单调用就可以进行繁琐的数据库操作。
调用方法
准备工作:
登录数据库,点击生成,将DBComm文件夹复制到使用程序目录,在cpp文件头添加如下:
#include ".DBCommDBTablesMyInfoOperator.h"
#include ".DBCommDBTablesRecordOperator.h"
#include ".DBCommDBTablesRecordViewOperator.h"
#include ".DBCommDBComm.h"
点击“工程”菜单,选择“添加到工程”,将DBComm内所有cpp文件添加(包含DBTables下cpp文件)
调用示例
连接数据库
void __fastcall TForm1::FormCreate(TObject *Sender)
{
WideString wstrConnStr="Provider=SQlolEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=mytest";
if(DBComm::Instance().ConnectDB(wstrConnStr))
{
DBGrid1->DataSource=DBComm::Instance().Tables<RecordViewOperator>(DBComm::RecordView)->Source();
}
}
新增一条记录
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
DBComm::Instance().Tables<MyInfoOperator>(DBComm::MyInfo)->New(LabeledEdit2->Text,LabeledEdit3->Text.ToDouble(),"admin",Now());
}
}
删除一条记录
void __fastcall TForm1::SpeedButton3Click(TObject *Sender)
{
if(DBGrid1->DataSource->DataSet->RecordCount)
{
int nID=DBGrid1->DataSource->DataSet->Fields->Fields[0]->AsInteger;
String strCondition="ID="+String(nID);
DBComm::Instance().Tables<RecordOperator>(DBComm::Record)->Delete(strCondition);
}
}
更新一条记录
void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
if(DBGrid1->DataSource->DataSet->RecordCount)
{
int nID=DBGrid1->DataSource->DataSet->Fields->Fields[1]->AsInteger;
String strName=LabeledEdit2->Text;
float fMoney=LabeledEdit3->Text.ToDouble();
String strCondition="ID="+String(nID);
DBComm::Instance().Tables<MyInfoOperator>(DBComm::MyInfo)->Update(strName,fMoney,"admin",Now(),strCondition);
}
}
获取数据源
DBGrid1->DataSource=DBComm::Instance().Tables<RecordViewOperator>(DBComm::RecordView)->Source();