MyCode代碼生成器,自動生成BCB版數(shù)據(jù)庫操作代碼(ADO),無需手工編寫,只需簡單調(diào)用就可以進(jìn)行繁瑣的數(shù)據(jù)庫操作。
調(diào)用方法
準(zhǔn)備工作:
登錄數(shù)據(jù)庫,點擊生成,將DBComm文件夾復(fù)制到使用程序目錄,在cpp文件頭添加如下:
#include ".DBCommDBTablesMyInfoOperator.h"
#include ".DBCommDBTablesRecordOperator.h"
#include ".DBCommDBTablesRecordViewOperator.h"
#include ".DBCommDBComm.h"
點擊“工程”菜單,選擇“添加到工程”,將DBComm內(nèi)所有cpp文件添加(包含DBTables下cpp文件)
調(diào)用示例
連接數(shù)據(jù)庫
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);
}
}
獲取數(shù)據(jù)源
DBGrid1->DataSource=DBComm::Instance().Tables<RecordViewOperator>(DBComm::RecordView)->Source();