开发工具: pb 8.0+sql sever 2000
from:nereus.li
date:04/13/2010
1 数据库先建立两个表
1.1数据库记录表建立:
create table erp_userhistory
(record int not null primary key ,
useid char(10),
operate char(20),
operatedate datetime default(getdate()) ,
operatedetail char(50)
)
1.2数据库操作表
create table rs_employeebasic(
employeeid int not null primary key,
c_name varchar(10),
e_name varchar(10),
birth datetime not null,
sex varchar(2) not null,
nation varchar(5) )
/********************************************************************/
2 建立工程
名称:record
应该各式:
2.1 事件open代码如下:
string as_server,as_dbms,as_database,as_logid,as_logpass
string ls_inifile = "E:/record/pb.ini"
string connect_method= "INIFILE"
Choose Case connect_method
Case "INIFILE"
if FileExists (ls_inifile) then
as_server = ProfileString ( ls_inifile, "Database", "Servername", "")
as_dbms = ProfileString ( ls_inifile, "Database", "DBMS", "")
as_database = ProfileString ( ls_inifile, "Database", "Database", "")
as_logid = ProfileString ( ls_inifile, "Database", "LogID", "")
as_logpass = ProfileString ( ls_inifile, "Database", "LogPassword","")
end if
Case "SCRIPT"
as_server = "服务器名称"
as_dbms = "MSS Microsoft SQL Server 6.x"
as_database = "数据库名称"
as_logid = "sa"
as_logpass = "*********"
End Choose
SQLCA.DBMS = as_dbms
SQLCA.Database = as_database
SQLCA.LogPass = as_logpass
SQLCA.ServerName = as_server
SQLCA.LogId = as_logid
SQLCA.AutoCommit = false
SQLCA.DBParm = ""
connect using SQLCA;
if SQLCA.SQLCode <> 0 Then
MessageBox ("Cannot Connect to Database", SQLCA.SQLErrText )
end if
connect;
open(w_main)
2.2事件close 代码如下
disconnect;
/********************************************************************/
3.建立两个数据窗口
3.1 dw_employ(调用rs_employeebasic表)
3.2 dw_record(调用erp_userhistory表)
/********************************************************************/
4.建立一个主窗口
建立一个tab里面放两个page(tabpage_1, tabpage_2)
4.1 tab_1.tabpage_1(调用dw_employ数据窗口)
4.1.1 tab_1.tabpage_1.dw_employ里的数据窗口事件sqlpreview()代码:
string ls_act,ls_sql
if pos(sqlsyntax,'SELECT') > 0 then return
ls_sql= left(sqlsyntax,6)
if ls_sql= 'INSERT' then ls_act = '插入'
if ls_sql= 'UPDATE' then ls_act = '更新'
if ls_sql= 'DELETE' then ls_act = '删除'
//insert userlog into erp_userhistory
long maxrow
datetime dttoday
string ls_operate,ls_detail
ls_operate="窗口的名称"+ls_act
if len(sqlsyntax)<=2048 then
ls_detail=sqlsyntax
else
ls_detail=left(sqlsyntax,2048)
end if
dttoday=datetime(today(),now())
select count(*) into:maxrow from erp_userhistory;
maxrow=maxrow +1
insert into erp_userhistory (record,userid,operate,operatedate,operatedetail)
values(:maxrow,:g_chinesename,:ls_operate,:dttoday,:ls_detail );
if sqlca.sqlcode=0 then
commit using sqlca;
else
rollback using sqlca;
end if
4.1.2 放一个insert按钮事件如下:
long tot,tot1,tot2
tot1 = dw_1.rowcount()
tot2=dw_1.insertrow(0)
tot=tot1+1
dw_1.setitem(tot2,1,tot)
dw_1.SetFocus()
dw_1.ScrollToRow(tot2)
dw_1.SetColumn(1)
4.1.3 放一个delete按钮事件如下:
if dw_1.rowcount()=0 then
return
end if
int p_message
p_message= MessageBox("WARNING","do you delete?",Exclamation!, OKCancel!, 2)
choose case p_message
case 1
dw_1.deleterow(dw_1.getrow())
cb_2.PostEvent(Clicked!)
end choose
4.1.4 放一个save按钮事件如下:
if dw_1.rowcount()=0 then
return
end if
if dw_1.update(true,false)=1 then
dw_1.resetUpdate()
commit;
messagebox("OK","The data is saved successfully!")
else
rollback;
end if
dw_1.SetRedraw(FALSE)
dw_1.SetRedraw(TRUE)
/********************************************************************/
tab_1.tabpage_2(调用dw_record数据窗口)
4.1.4 放一个update按钮事件如下:
tab_1.tabpage_2.dw_2.settransobject(sqlca)
tab_1.tabpage_2.dw_2.retrieve()