这是在 20CN网络安全小组第一代论坛 的论坛 编程破解 中的主题 本地数据库列程 (转载)


要查看这个主题,请使用这个 URL:
http://www.20cn.net/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic;f=5;t=000038

蓝色梦缘 发表于 :
 
本地数据库列程

作者:病毒 发表于 绿色兵团

很早前写的一个本地数据库程序,支持动态建库,备份,库与库导入记录,
增,删,改分类查询等,其中还有很多设计技巧,功能多多。现在看来此程序
有待完善,将源码贴出来,希望初学者可以试着去改动,弄明白了它 你本地
数据库设计应该没什么问题了。


//---------------------------------------------------------------------------
#ifndef mainH
#define mainH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <ImgList.hpp>
#include <Menus.hpp>
#include <ToolWin.hpp>
#include <Db.hpp>
#include <DBTables.hpp>
#include <Dialogs.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TMainMenu *MainMenu1;
TMenuItem *N1;
TMenuItem *NewCreat;
TMenuItem *OpenDatabase;
TMenuItem *Exit;
TMenuItem *N5;
TMenuItem *Add;
TMenuItem *ModyF;
TMenuItem *Query;
TMenuItem *About;
TMenuItem *ProHelp;
TMenuItem *Prouse;
TStatusBar *StatusBar1;
TToolBar *ToolBar1;
TToolButton *ToolButton1;
TToolButton *ToolButton2;
TToolButton *ToolButton3;
TToolButton *ToolButton4;
TToolButton *ToolButton5;
TToolButton *ToolButton6;
TImageList *ImageList1;
TListView *ListView1;
TQuery *Query1;
TMenuItem *Delete;
TToolButton *ToolButton7;
TToolButton *ToolButton8;
TToolButton *ToolButton9;
TToolButton *ToolButton10;
TToolButton *ToolButton11;
TToolButton *ToolButton12;
TMenuItem *Ref;
TPopupMenu *PopupMenu1;
TMenuItem *N2;
TMenuItem *N3;
TMenuItem *N4;
TMenuItem *N6;
TMenuItem *N7;
TMenuItem *N9;
TMenuItem *N10;
TMenuItem *N11;
TMenuItem *N12;
TMenuItem *N13;
TMenuItem *N14;
TMenuItem *N16;
TOpenDialog *OpenDialog1;
TSaveDialog *SaveDialog1;
TBatchMove *BatchMove1;
TTable *Table1;
TMenuItem *N18;
TMenuItem *N15;
TMenuItem *LIcon;
TMenuItem *ListView;
TMenuItem *ReportView;
TMenuItem *N22;
TMenuItem *SIcon;
TMenuItem *BackupData;
TMenuItem *N19;
TMenuItem *N20;
TMenuItem *N8;
TMenuItem *Restor;
TToolButton *ToolButton13;
TToolButton *ToolButton14;
TToolButton *ToolButton15;
TImageList *ImageList2;
void __fastcall AddClick(TObject *Sender);
void __fastcall DeleteClick(TObject *Sender);
void __fastcall ModyFClick(TObject *Sender);
void __fastcall ListView1DblClick(TObject *Sender);
void __fastcall RefClick(TObject *Sender);
void __fastcall QueryClick(TObject *Sender);
void __fastcall ListView1KeyPress(TObject *Sender, char &Key);
void __fastcall ExitClick(TObject *Sender);
void __fastcall OpenDatabaseClick(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
void __fastcall NewCreatClick(TObject *Sender);
void __fastcall LIconClick(TObject *Sender);
void __fastcall SIconClick(TObject *Sender);
void __fastcall ListViewClick(TObject *Sender);
void __fastcall ReportViewClick(TObject *Sender);
void __fastcall BackupDataClick(TObject *Sender);
void __fastcall RestorClick(TObject *Sender);
void __fastcall ProHelpClick(TObject *Sender);
void __fastcall ProuseClick(TObject *Sender);
void __fastcall ToolButton1MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y);
void __fastcall ToolButton2MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y);
void __fastcall ToolButton3MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y);
void __fastcall ToolButton4MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y);
void __fastcall ToolButton5MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y);
void __fastcall ToolButton6MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y);
void __fastcall ToolButton7MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y);
void __fastcall ToolButton8MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y);
void __fastcall ToolButton9MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y);
void __fastcall ToolButton10MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y);
void __fastcall ToolButton11MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y);
void __fastcall ToolButton12MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y);
void __fastcall ToolButton13MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y);
void __fastcall ToolButton14MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y);
void __fastcall ToolButton15MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y);
void __fastcall ListView1MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y);
private: // User declarations
void __fastcall TForm1::CreateTable(AnsiString TBName);
public: // User declarations
__fastcall TForm1(TComponent* Owner);
void __fastcall TForm1::ListAll(AnsiString DB);
void __fastcall TForm1::ReQuery();
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;

//---------------------------------------------------------------------------
#endif


//---------------------------------------------------------------------------
// 《网友通讯录》
//
// 作者: 贾佳
// Email: jiasys@21cn.com
//
//---------------------------------------------------------------------------

#include <vcl.h>
#include <dir.h>
#pragma hdrstop

#include "main.h"
#include "Add.h"
#include "Edit.h"
#include "Find.h"
#include "About.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}

//---------------------------------------------------------------------------
// CreateTable
//
// 自定义成员函数,调用SQL语句动态建立数据库
// 数据库名由TBName参数指定
//
//---------------------------------------------------------------------------

void __fastcall TForm1::CreateTable(AnsiString TBName)
{
AnsiString CTB="CREATE TABLE "+TBName;

CTB=CTB+"\ (\
NNAME char(150),\
TNAME char(50),\
SEX char(2),\
OICQ char(200),\
EMAIL char(80),\
TEL char(80),\
GZDW char(150),\
JTZZ char(150))";

Query1->Close();
Query1->SQL->Clear();
Query1->SQL->Add(CTB);
Query1->ExecSQL();

}


//---------------------------------------------------------------------------
// ReQuery
//
// 自定义成员函数,将当前所有记录
// 由ListView列出
//
//---------------------------------------------------------------------------

void __fastcall TForm1::ReQuery()
{
TListItem *ListAdd;
int iRecCount=Query1->RecordCount;

ListView1->Items->BeginUpdate();
ListView1->Items->Clear();
ListView1->Items->EndUpdate();


Query1->Open();
Query1->First();

if(iRecCount>0)
{
while(!Query1->Eof)
{
ListAdd=ListView1->Items->Add();
ListAdd->SubItems->BeginUpdate();
ListAdd->Caption=Query1->FieldByName("NNAME")->AsString;
ListAdd->SubItems->Add(Query1->FieldByName("TNAME")->AsString);
ListAdd->SubItems->Add(Query1->FieldByName("SEX")->AsString);
ListAdd->SubItems->Add(Query1->FieldByName("OICQ")->AsString);
ListAdd->SubItems->Add(Query1->FieldByName("EMAIL")->AsString);
ListAdd->SubItems->Add(Query1->FieldByName("TEL")->AsString);
ListAdd->SubItems->Add(Query1->FieldByName("GZDW")->AsString);
ListAdd->SubItems->Add(Query1->FieldByName("JTZZ")->AsString);
ListAdd->SubItems->EndUpdate();
Query1->Next();
}
ListView1->Items->Item[0]->Selected=true;
StatusBar1->Panels->Items[0]->Text=" 当前共找到 "+String(iRecCount)+"条 记录";
}
else
{
StatusBar1->Panels->Items[0]->Text="共找到0条记录";
ShowMessage("没有找到任何记录!");
}
}

//---------------------------------------------------------------------------
// ListAll
//
// 自定义成员函数,列出指定数据库中所有记录
//
//
//---------------------------------------------------------------------------

void __fastcall TForm1::ListAll(AnsiString DB)
{
String StrQ;
StrQ="SELECT * FROM "+DB;

Query1->Close();
Query1->SQL->Clear();
Query1->SQL->Add(StrQ);
Query1->Prepare();
Query1->Open();
ReQuery();
}

//---------------------------------------------------------------------------

void __fastcall TForm1::AddClick(TObject *Sender)
{
Form2->Show();
}

//---------------------------------------------------------------------------
// DeleteClick
//
// 删除所有已选中的记录
//
//
//---------------------------------------------------------------------------

void __fastcall TForm1::DeleteClick(TObject *Sender)
{
int iCount=ListView1->Items->Count;

try
{
if((ListView1->Items->Count)&&(ListView1->Selected->Index>=0))
{
if(Application->MessageBox("确定删除此记录?","记录删除",MB_YESNO|MB_ICONQUESTION)==IDYES)
{
for(int i=0;i<iCount;i++)
{
Query1->First();
Query1->Edit();
Query1->MoveBy(ListView1->Selected->Index);
Query1->Delete();
ListView1->Items->BeginUpdate();
ListView1->Items->Delete(ListView1->Selected->Index);
ListView1->Items->EndUpdate();
}
ReQuery();
}
}
else ShowMessage("以无记录可删,请重新添加/查询");
}

catch(...)
{
Application->MessageBox("请先选中要删除的记录","提示",MB_OK|MB_ICONWARNING);
}

}

//---------------------------------------------------------------------------
// ModyFClick
//
// 修改当前记录
//
//
//---------------------------------------------------------------------------


void __fastcall TForm1::ModyFClick(TObject *Sender)
{
try
{
Query1->Open();
Query1->First();
Query1->MoveBy(ListView1->Selected->Index);
Form3->NName->Text=Query1->FieldByName("NNAME")->AsString;
Form3->TName->Text=Query1->FieldByName("TNAME")->AsString;
Form3->Sex->Text=Query1->FieldByName("SEX")->AsString;
Form3->OICQ->Text=Query1->FieldByName("OICQ")->AsString;
Form3->EMail->Text=Query1->FieldByName("EMAIL")->AsString;
Form3->Tel->Text=Query1->FieldByName("TEL")->AsString;
Form3->Gzdw->Text=Query1->FieldByName("GZDW")->AsString;
Form3->Jtzz->Text=Query1->FieldByName("JTZZ")->AsString;
Form3->Show();
}

catch(...)
{
Application->MessageBox("请先选中要修改的记录","提示",MB_OK|MB_ICONWARNING);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListView1DblClick(TObject *Sender)
{
ModyFClick(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RefClick(TObject *Sender)
{
//刷新数据库

ListAll(ExtractFileName(OpenDialog1->FileName));
}
//---------------------------------------------------------------------------


void __fastcall TForm1::QueryClick(TObject *Sender)
{
Form4->Show();
}

//---------------------------------------------------------------------------
// ListView1KeyPress
//
// 判断当前按键,给出相应操作
//
//
//---------------------------------------------------------------------------

void __fastcall TForm1::ListView1KeyPress(TObject *Sender, char &Key)
{
try
{
if((ListView1->Items->Count)&&(ListView1->Selected->Index>=0))
{
switch(Key)
{
case VK_RETURN:
ModyFClick(Sender);
break;

case 46:
DeleteClick(Sender);
break;
}
}
else
Application->MessageBox("无记录可编辑,请重新添加/查询","提示",MB_OK|MB_ICONWARNING);
}
catch(...)
{
Application->MessageBox("请先选中要修改的记录","提示",MB_OK|MB_ICONWARNING);
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ExitClick(TObject *Sender)
{
////退出系统
Close();
}

//---------------------------------------------------------------------------
// OpenDatabaseClick
//
// 打开指定数据库
//
//
//---------------------------------------------------------------------------

void __fastcall TForm1::OpenDatabaseClick(TObject *Sender)
{
if(OpenDialog1->Execute())
{
if(FileExists(OpenDialog1->FileName))
{
StatusBar1->Panels->Items[1]->Text=" 当前打开数据库 "+OpenDialog1->FileName;
ListAll(ExtractFileName(OpenDialog1->FileName));
}
else
Application->MessageBox("此库不存在,请重新选择","错误",MB_OK|MB_ICONSTOP);
}
}

//---------------------------------------------------------------------------
// FormCreate
//
// 程序启动时自动装载以有的DB库。
// 如不存在则自动建立
//
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
ReportViewClick(Sender);
Query1->DatabaseName=ExtractFilePath(Application->ExeName);
OpenDialog1->FileName="db.db";
if(FileExists(OpenDialog1->FileName))
{
ListAll(OpenDialog1->FileName);
StatusBar1->Panels->Items[1]->Text=" 当前打开数据库 "+ExtractFilePath(Application->ExeName)+OpenDialog1->FileName;
}
else
{
Application->MessageBox("没有发现db数据库,为使系统正常工作将重建db数据库!","提示",MB_OK|MB_ICONWARNING);
CreateTable(OpenDialog1->FileName);
ListAll(OpenDialog1->FileName);
}
}

//---------------------------------------------------------------------------
// NewCreatClick
//
// 新建数据库
//
//
//---------------------------------------------------------------------------

void __fastcall TForm1::NewCreatClick(TObject *Sender)
{
String DStr;

ListView1->Items->BeginUpdate();
ListView1->Items->Clear();
ListView1->Items->EndUpdate();

ListAll(ExtractFileName(OpenDialog1->FileName));
if(SaveDialog1->Execute())
{
if(FileExists(SaveDialog1->FileName))
{
Application->MessageBox("此数据库以存在,请选择其他名称!","新建数据库",MB_OK|MB_ICONWARNING);
}
else
{
CreateTable(ExtractFileName(SaveDialog1->FileName));
ShowMessage(SaveDialog1->FileName+" 库建立完毕 ");
}
}
}
//---------------------------------------------------------------------------


void __fastcall TForm1::LIconClick(TObject *Sender)
{
//大图标显示
ListView1->ViewStyle=vsIcon;
ToolButton10->Down=true;
ToolButton11->Down=false;
ToolButton12->Down=false;
ToolButton13->Down=false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::SIconClick(TObject *Sender)
{
//小图标显示
ListView1->ViewStyle=vsSmallIcon;
ToolButton11->Down=true;
ToolButton10->Down=false;
ToolButton12->Down=false;
ToolButton13->Down=false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ListViewClick(TObject *Sender)
{
//列表显示
ListView1->ViewStyle=vsList;
ToolButton12->Down=true;
ToolButton11->Down=false;
ToolButton10->Down=false;
ToolButton13->Down=false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ReportViewClick(TObject *Sender)
{
ListView1->ViewStyle=vsReport;
ToolButton13->Down=true;
ToolButton11->Down=false;
ToolButton10->Down=false;
ToolButton12->Down=false;
}

//---------------------------------------------------------------------------
// BackupDataClick
//
// 备份当前数据库到指定库中
//
//---------------------------------------------------------------------------


void __fastcall TForm1::BackupDataClick(TObject *Sender)
{
ListAll(ExtractFileName(OpenDialog1->FileName));
if(SaveDialog1->Execute())
{
if(FileExists(SaveDialog1->FileName))
{
if(Application->MessageBox("此数据库以存在,是否覆盖?","备份数据库",MB_YESNO|MB_ICONQUESTION)==IDYES)
{
Table1->TableName=SaveDialog1->FileName;
BatchMove1->Source=Query1;
BatchMove1->Destination=Table1;
BatchMove1->Mode=batCopy;
BatchMove1->Execute();
ShowMessage("共备份 "+IntToStr(BatchMove1->MovedCount)+" 条记录到 "+SaveDialog1->FileName+" 库中");
}
}
else
{
Table1->TableName=SaveDialog1->FileName;
BatchMove1->Source=Query1;
BatchMove1->Destination=Table1;
BatchMove1->Mode=batCopy;
BatchMove1->Execute();
ShowMessage("共备份 "+IntToStr(BatchMove1->MovedCount)+" 条记录到 "+SaveDialog1->FileName+" 库中");
}
}
}

//---------------------------------------------------------------------------
// RestorClick
//
// 从指定库中向当前库导入记录
//
//---------------------------------------------------------------------------


void __fastcall TForm1::RestorClick(TObject *Sender)
{
if(Query1->RecordCount<1500)
{
if(OpenDialog1->Execute())
{
Table1->TableName=OpenDialog1->FileName;
BatchMove1->Source=Table1;
BatchMove1->Destination=(TTable *)Query1;
BatchMove1->Mode=batAppend;
BatchMove1->Execute();
ShowMessage("共导入 "+IntToStr(BatchMove1->MovedCount)+" 条记录 ");
}
ReQuery();
}
else
ShowMessage("当前库中记录已大于1500条,无法导入新记录,请另建新库!");
}
//---------------------------------------------------------------------------


void __fastcall TForm1::ProHelpClick(TObject *Sender)
{
AboutPro->ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ProuseClick(TObject *Sender)
{
ShowMessage("作者:贾 佳 \n\nEmail: jiasys@21cn.com ");
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ToolButton1MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
StatusBar1->Panels->Items[2]->Text=ToolButton1->Hint;
}
//---------------------------------------------------------------------------


void __fastcall TForm1::ToolButton2MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
StatusBar1->Panels->Items[2]->Text=ToolButton2->Hint;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ToolButton3MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
StatusBar1->Panels->Items[2]->Text=ToolButton3->Hint;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ToolButton4MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
StatusBar1->Panels->Items[2]->Text=ToolButton4->Hint;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ToolButton5MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
StatusBar1->Panels->Items[2]->Text=ToolButton5->Hint;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ToolButton6MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
StatusBar1->Panels->Items[2]->Text=ToolButton6->Hint;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ToolButton7MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
StatusBar1->Panels->Items[2]->Text=ToolButton7->Hint;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ToolButton8MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
StatusBar1->Panels->Items[2]->Text=ToolButton8->Hint;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ToolButton9MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
StatusBar1->Panels->Items[2]->Text=ToolButton9->Hint;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ToolButton10MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
StatusBar1->Panels->Items[2]->Text=ToolButton10->Hint;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ToolButton11MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
StatusBar1->Panels->Items[2]->Text=ToolButton11->Hint;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ToolButton12MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
StatusBar1->Panels->Items[2]->Text=ToolButton12->Hint;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ToolButton13MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
StatusBar1->Panels->Items[2]->Text=ToolButton13->Hint;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ToolButton14MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
StatusBar1->Panels->Items[2]->Text=ToolButton14->Hint;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ToolButton15MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
StatusBar1->Panels->Items[2]->Text=ToolButton15->Hint;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ListView1MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
StatusBar1->Panels->Items[2]->Text="双击记录进行修改";
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
#ifndef AddH
#define AddH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm2 : public TForm
{
__published: // IDE-managed Components
TEdit *NName;
TEdit *TName;
TComboBox *Sex;
TEdit *OICQ;
TEdit *EMail;
TEdit *Gzdw;
TEdit *Jtzz;
TLabel *Label1;
TLabel *Label2;
TLabel *Label3;
TLabel *Label4;
TLabel *Label5;
TLabel *Label6;
TLabel *Label7;
TButton *Button1;
TButton *Button2;
TEdit *Tel;
TLabel *Label8;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm2(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form2;
//---------------------------------------------------------------------------
#endif


//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "main.h"
#include "Add.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}

//---------------------------------------------------------------------------
// Button1Click (新增记录入库)
//
// 用TQuery类本身的成员函数完成新增记录
// 当前库中记录大于1500条时,给出提示
//
//---------------------------------------------------------------------------


void __fastcall TForm2::Button1Click(TObject *Sender)
{

if(Form1->Query1->RecordCount<1500)
{
Form1->Query1->Close();
Form1->Query1->Open();
Form1->Query1->Next();
Form1->Query1->Edit();
Form1->Query1->Append();
Form1->Query1->FieldByName("NNAME")->AsString=NName->Text;
Form1->Query1->FieldByName("TNAME")->AsString=TName->Text;
Form1->Query1->FieldByName("SEX")->AsString=Sex->Text;
Form1->Query1->FieldByName("OICQ")->AsString=OICQ->Text;
Form1->Query1->FieldByName("EMAIL")->AsString=EMail->Text;
Form1->Query1->FieldByName("TEL")->AsString=Tel->Text;
Form1->Query1->FieldByName("GZDW")->AsString=Gzdw->Text;
Form1->Query1->FieldByName("JTZZ")->AsString=Jtzz->Text;
Form1->Query1->Post();

NName->Text=""; TName->Text=""; OICQ->Text="";
EMail->Text=""; Gzdw->Text=""; Jtzz->Text=""; Tel->Text="";

Form1->ListAll(ExtractFileName(Form1->OpenDialog1->FileName));
}
else
ShowMessage("当前库中记录已大于1500条,无法新增记录,请另建新库!");
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button2Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
#ifndef EditH
#define EditH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm3 : public TForm
{
TLabel *Label1;
TLabel *Label2;
TLabel *Label3;
TLabel *Label4;
TLabel *Label5;
__published: // IDE-managed Components
TLabel *Label6;
TLabel *Label7;
TEdit *NName;
TEdit *TName;
TComboBox *Sex;
TEdit *OICQ;
TEdit *EMail;
TEdit *Gzdw;
TEdit *Jtzz;
TButton *Button1;
TButton *Button2;
TEdit *Tel;
TLabel *Label8;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall EMailMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y);
void __fastcall FormMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y);
void __fastcall EMailDblClick(TObject *Sender);
void __fastcall EMailKeyPress(TObject *Sender, char &Key);
private: // User declarations
public: // User declarations
__fastcall TForm3(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm3 *Form3;
//---------------------------------------------------------------------------
#endif


//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Edit.h"
#include "main.h"
#include <shellapi.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm3 *Form3;
//---------------------------------------------------------------------------
__fastcall TForm3::TForm3(TComponent* Owner)
: TForm(Owner)
{
}

//---------------------------------------------------------------------------
// Button1Click (修改入库)
//
// 用TQuery类本身的成员函数完成修改
//
//---------------------------------------------------------------------------

void __fastcall TForm3::Button1Click(TObject *Sender)
{
Form1->Query1->Edit();
Form1->Query1->FieldByName("NNAME")->AsString=NName->Text;
Form1->Query1->FieldByName("TNAME")->AsString=TName->Text;
Form1->Query1->FieldByName("SEX")->AsString=Sex->Text;
Form1->Query1->FieldByName("OICQ")->AsString=OICQ->Text;
Form1->Query1->FieldByName("EMAIL")->AsString=EMail->Text;
Form1->Query1->FieldByName("TEL")->AsString=Tel->Text;
Form1->Query1->FieldByName("GZDW")->AsString=Gzdw->Text;
Form1->Query1->FieldByName("JTZZ")->AsString=Jtzz->Text;
Form1->Query1->Post();
Form1->ReQuery();
ShowMessage("修改完毕");
}
//---------------------------------------------------------------------------
void __fastcall TForm3::Button2Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------


void __fastcall TForm3::EMailMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
EMail->Font->Color=clBlue;
EMail->Cursor=crHandPoint;
EMail->Font->Style=EMail->Font->Style<<fsItalic<<fsUnderline;
}
//---------------------------------------------------------------------------

void __fastcall TForm3::FormMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
EMail->Font->Color=clBlack;
EMail->Cursor=crDefault;
EMail->Font->Style=EMail->Font->Style>>fsItalic>>fsUnderline;
}
//---------------------------------------------------------------------------

void __fastcall TForm3::EMailDblClick(TObject *Sender)
{
ShellExecute(Handle,NULL,String("mailto:"+EMail->Text).c_str(),NULL,NULL,SW_SHOWNORMAL);
}
//---------------------------------------------------------------------------

void __fastcall TForm3::EMailKeyPress(TObject *Sender, char &Key)
{
EMail->Font->Color=clBlack;
EMail->Cursor=crDefault;
EMail->Font->Style=EMail->Font->Style>>fsItalic>>fsUnderline;
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
#ifndef FindH
#define FindH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm4 : public TForm
{
__published: // IDE-managed Components
TRadioGroup *RadioGroup1;
TEdit *NName;
TEdit *TName;
TEdit *Oicq;
TEdit *EMail;
TEdit *Tel;
TButton *Button1;
TCheckBox *CheckBox1;
TCheckBox *CheckBox2;
void __fastcall RadioGroup1Click(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
void __fastcall CheckBox2Click(TObject *Sender);
void __fastcall CheckBox1Click(TObject *Sender);
private: // User declarations
void __fastcall TForm4::SetText();
void __fastcall TForm4::FindRecord(String DBName,String Field,String QStr);
public: // User declarations
__fastcall TForm4(TComponent* Owner);

};
//---------------------------------------------------------------------------
extern PACKAGE TForm4 *Form4;
//---------------------------------------------------------------------------
#endif


//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Find.h"
#include "Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm4 *Form4;
//---------------------------------------------------------------------------
__fastcall TForm4::TForm4(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm4::SetText()
{
NName->Text=""; TName->Text=""; Oicq->Text="";
EMail->Text=""; Tel->Text="";
}

//---------------------------------------------------------------------------
// FindRecord
//
// 自定义函数,用AnsiString构造SQL语句,DBName当前数据库名,Field字段名,QStr查找方式
// 设置好用调用TQuery类执行
//
//---------------------------------------------------------------------------

void __fastcall TForm4::FindRecord(String DBName,String Field,String QStr)
{
AnsiString MyQ;

MyQ="SELECT * FROM "+DBName+" WHERE ";
MyQ=MyQ+Field+" LIKE "+":"+Field;
if(CheckBox1->Checked)
QStr=QStr+"%";
else QStr=QStr="%"+QStr+"%";

Form1->Query1->Close();
Form1->Query1->SQL->Clear();
Form1->Query1->SQL->Add(MyQ);
Form1->Query1->ParamByName(Field)->AsString=QStr;
Form1->Query1->Prepare();
Form1->Query1->Open();
Close();
Form1->ReQuery();
}

//---------------------------------------------------------------------------
// RadioGroup1Click
//
// 选择查找根据
//
//---------------------------------------------------------------------------


void __fastcall TForm4::RadioGroup1Click(TObject *Sender)
{
if(RadioGroup1->Items->Strings[RadioGroup1->ItemIndex]=="网络昵称")
{
NName->Enabled=true;
TName->Enabled=false;
Oicq->Enabled=false;
EMail->Enabled=false;
Tel->Enabled=false;
}
else if(RadioGroup1->Items->Strings[RadioGroup1->ItemIndex]=="真实姓名")
{
TName->Enabled=true;
NName->Enabled=false;
Oicq->Enabled=false;
EMail->Enabled=false;
Tel->Enabled=false;
}
else if(RadioGroup1->Items->Strings[RadioGroup1->ItemIndex]=="OICQ号码")
{
Oicq->Enabled=true;
NName->Enabled=false;
TName->Enabled=false;
EMail->Enabled=false;
Tel->Enabled=false;
}
else if(RadioGroup1->Items->Strings[RadioGroup1->ItemIndex]=="EMAIL地址")
{
EMail->Enabled=true;
NName->Enabled=false;
Oicq->Enabled=false;
TName->Enabled=false;
Tel->Enabled=false;
}
else if(RadioGroup1->Items->Strings[RadioGroup1->ItemIndex]=="电话号码")
{
Tel->Enabled=true;
NName->Enabled=false;
Oicq->Enabled=false;
TName->Enabled=false;
EMail->Enabled=false;
}


}

//---------------------------------------------------------------------------
// Button1Click
//
// 调用FindRecord函数查找
//
//---------------------------------------------------------------------------

void __fastcall TForm4::Button1Click(TObject *Sender)
{
if(NName->Enabled)
FindRecord(ExtractFileName(Form1->OpenDialog1->FileName),"nname",NName->Text);
else if(TName->Enabled)
FindRecord(ExtractFileName(Form1->OpenDialog1->FileName),"tname",TName->Text);
else if(Oicq->Enabled)
FindRecord(ExtractFileName(Form1->OpenDialog1->FileName),"Oicq",Oicq->Text);
else if(EMail->Enabled)
FindRecord(ExtractFileName(Form1->OpenDialog1->FileName),"EMail",EMail->Text);
else if(Tel->Enabled)
FindRecord(ExtractFileName(Form1->OpenDialog1->FileName),"tel",Tel->Text);
}
//---------------------------------------------------------------------------

void __fastcall TForm4::FormCreate(TObject *Sender)
{
RadioGroup1->ItemIndex=0;
}
//---------------------------------------------------------------------------

void __fastcall TForm4::CheckBox2Click(TObject *Sender)
{
if(CheckBox2->Checked)
CheckBox1->Checked=false;
}
//---------------------------------------------------------------------------

void __fastcall TForm4::CheckBox1Click(TObject *Sender)
{
if(CheckBox1->Checked)
CheckBox2->Checked=false;
}
//---------------------------------------------------------------------------



 






Powered by Infopop Corporation
UBB.classic™ 6.5.0
NetDemon修改版 1.5.0, 20CN网络安全小组 版权所有。