📄 dmunit.~pas
字号:
unit DMUnit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
DBTables, Db, syncobjs,
PkgUnit;
type
TDM = class(TDataModule)
QueryBase: TQuery;
DatabaseStudent: TDatabase;
SessionStudent: TSession;
TableSysYear: TTable;
DBTable: TDatabase;
TBSession: TSession;
WkQuery: TQuery;
WkDatabase: TDatabase;
WkSession: TSession;
ExecQuery: TQuery;
ExecStoredProc: TStoredProc;
QueryCredit: TQuery;
QueryCourse: TQuery;
procedure QueryBaseAfterOpen(DataSet: TDataSet);
procedure DataModuleCreate(Sender: TObject);
procedure DataModuleDestroy(Sender: TObject);
private
{ Private declarations }
nWNCritical:TCriticalSection;
//function getxh:string;
public
{ Public declarations }
procedure S_Add(const Inpkg: TSPkgData);
procedure S_Md(const Inpkg: TSPkgData);
procedure S_C_Add(const pStudent_id, pcourse, pcourse_t: WideString;
pperiod, pcredit: Integer; const pbegin_date, pend_date: WideString);
procedure S_C_Md(const pstudent_id, pcourse, pcourse_t: WideString;
pperiod, pcredit: Integer; const pbegin_date, pend_date: WideString);
procedure S_G_Md(const pstudent_id, pcourse: WideString; pgrades: Integer);
procedure S_Year(const CurrentYear: WideString);
function ExistData(const SQLStatment:string):boolean;
procedure ExecSQL(const SQLStatment:string);
function getxh:string;
end;
var
DM: TDM;
implementation
{$R *.DFM}
uses MainUnit;
procedure TDM.QueryBaseAfterOpen(DataSet: TDataSet);
begin
{ Update the query counter }
MainForm.IncQueryCount;
end;
function TDM.ExistData(const SQLStatment:string):boolean;
begin
nWNCritical.Enter;
try
With WkQuery do
begin
if Active then Close;
SQL.Clear;
SQL.Add(SQLStatment);
Open;
result:=RecordCount>0;
end;
except
result:=False;
end;
nWNCritical.Leave;
end;
procedure TDM.ExecSQL(const SQLStatment:string);
begin
nWNCritical.Enter;
try
with ExecQuery do
begin
if Active then Close;
SQL.Clear;
SQL.Add(SQLStatment);
ExecSQL;
end;
except
Raise;
end;
nWNCritical.Leave;
end;
function TDM.getxh:string;
var s:string;
begin
{1.取学号}
nWNCritical.Enter;
With ExecStoredProc do
begin
StoredProcName := 'newwks';
ParamBindMode :=pbByName;
Params.Clear;
Params.CreateParam(ftString, 'STUDENT_NAME', ptInput);
Params.CreateParam(ftString, 'STUDENT_ID', ptOutput);
ParamByName('STUDENT_NAME').AsString := 'Name';
ExecProc;
GetResults;
s:=ParamByName('STUDENT_ID').AsString;
end;
Result:=S;
nWNCritical.Leave;
end;
procedure TDM.S_Add(const Inpkg: TSPkgData);
var pkg:TSPackage;
fstudent_id: string[8]; //*学号*/
fname: string[12];//*学员姓名*/
fsex: string[1];//*性别*/
fgrade: string[4];//*入学年度*/
fnative: string[20];//*籍贯*/
fidentity: string[18];//*身份证编号*/
fage: string; //*年龄*/
begin
pkg := TSPackage.Create(Inpkg);
{拆解数据包}
with pkg do
begin
fname := Item['name']; //*学员姓名*/
fsex := Item['sex']; //*性别*/
fgrade := Item['grade']; //*入学年度*/
fnative := Item['native']; //*籍贯*/
fidentity := Item['identity']; //*身份证编号*/
fage := Item['age']; //*年龄*/
end;
pkg.Free;
{分析数据 ... }
{取新的学号}
fstudent_id:=getxh;
{插入数据库 ... }
ExecSQL('Insert into BaseTB(student_id,name,sex,grade,native,identity,age) '+
'Values('+
QuotedStr(fstudent_id)+','+
QuotedStr(fname)+','+
QuotedStr(fsex)+','+
QuotedStr(fgrade)+','+
QuotedStr(fnative)+','+
QuotedStr(fidentity)+','+
fage+')' );
{
fstudent_id: string[8]; //*学号*/
fname:= string[12];//*学员姓名*/
fsex: string[1];//*性别*/
fgrade: string[4];//*入学年度*/
fnative: string[20];//*籍贯*/
fidentity: string[18];//*身份证编号*/
fage: integer; //*年龄*/
}
end;
procedure TDM.S_Md(const Inpkg: TSPkgData);
var pkg:TSPackage;
fstudent_id: string[8]; //*学号*/
fname: string[12];//*学员姓名*/
fsex: string[1];//*性别*/
fgrade: string[4];//*入学年度*/
fnative: string[20];//*籍贯*/
fidentity: string[18];//*身份证编号*/
fage: string; //*年龄*/
s:string;
begin
pkg := TSPackage.Create(Inpkg);
{拆解数据包}
with pkg do
begin
fstudent_id:= Item['student_id']; //*学号*/
fname := Item['name']; //*学员姓名*/
fsex := Item['sex']; //*性别*/
fgrade := Item['grade']; //*入学年度*/
fnative := Item['native']; //*籍贯*/
fidentity := Item['identity']; //*身份证编号*/
fage := Item['age']; //*年龄*/
end;
pkg.Free;
{分析数据 ... }
{插入数据库 ... }
s:='Update BaseTB Set '+
' name='+QuotedStr(fname)+','+
' sex='+QuotedStr(fsex)+','+
' grade='+QuotedStr(fgrade)+','+
' native='+QuotedStr(fnative)+','+
' identity='+QuotedStr(fidentity)+','+
' age='+fage+
' where student_id='+QuotedStr(fstudent_id) ;
ExecSQL(s);
end;
procedure TDM.DataModuleCreate(Sender: TObject);
begin
{}
nWNCritical:=TCriticalSection.Create;
end;
procedure TDM.DataModuleDestroy(Sender: TObject);
begin
nWNCritical.Free;
end;
procedure TDM.S_C_Add(const pStudent_id, pcourse, pcourse_t: WideString;
pperiod, pcredit: Integer; const pbegin_date, pend_date: WideString);
begin
if not ExistData('select * from CourseTB where student_id='+QuotedStr(pstudent_id)+' and course='+QuotedStr(pcourse) ) then
begin
ExecSQL('insert into CourseTB(student_id,course,course_t,period,credit,begin_date,end_date)'+
' values('+
QuotedStr(pStudent_id)+','+
QuotedStr(pcourse)+','+
QuotedStr(pcourse_t)+','+
inttostr(pperiod)+','+
inttostr(pcredit)+','+
QuotedStr(pbegin_date)+','+
QuotedStr(pend_date)+')');
{插入成绩,单初值为0}
ExecSQL('insert into CreditTB(student_id,course,grades)'+
' values('+
QuotedStr(pStudent_id)+','+
QuotedStr(pcourse)+',0)');
end;
end;
procedure TDM.S_C_Md(const pstudent_id, pcourse, pcourse_t: WideString;
pperiod, pcredit: Integer; const pbegin_date, pend_date: WideString);
begin
if ExistData('select * from CourseTB where student_id='+QuotedStr(pstudent_id)+' and course='+QuotedStr(pcourse) ) then
ExecSQL('Update CourseTB Set '+
' course_t='+QuotedStr(pcourse_t)+','+
' period='+ inttostr(pperiod)+','+
' credit='+ inttostr(pcredit)+','+
' begin_date='+ QuotedStr(pbegin_date)+','+
' end_date='+ QuotedStr(pend_date)+
' where student_id='+QuotedStr(pstudent_id)+
' and course='+QuotedStr(pcourse) );
end;
procedure TDM.S_G_Md(const pstudent_id, pcourse: WideString; pgrades: Integer);
begin
ExecSQL('Update CreditTB Set grades='+ inttostr(pgrades)+
' where student_id='+QuotedStr(pstudent_id)+
' and course='+QuotedStr(pcourse) );
end;
procedure TDM.S_Year(const CurrentYear: WideString);
begin
ExecSQL('Update SysPramTB Set PaMa='+ QuotedStr(CurrentYear)+
' where pmky='+QuotedStr('student')+
' and cdky='+QuotedStr('grade') );
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -