XuGang

记录一个程序员的成长

 

Oracle数据库主键设计


首先
,创建一个队列Sequence
-- Create sequence
create sequence PRIMARYKEYSEQUENCE
minvalue
0
maxvalue
999999999999999999999999999
start with
83
increment by
1
nocache;


然后,创建触发器Triggers
-- Create triggers
create or replace trigger XG_ATTENDANCETRIGGER
 before insert on xg_attendance 
 for each row
declare
 nextid number;
begin
 if:new.AID is null or:new.AID=
0
 then
 select primarykeysequence.nextval into nextid from sys.dual;
 :new.AID:= nextid;
 end if;
end XG_ATTENDANCETRIGGER;


最后,在包Packages中创建存储过程Procedure
-- Create procedure
create or replace package body xg.AttendancePackage is
procedure proc_insertAttendance(wid in varchar2,inTime in varchar2,aid out varchar2)
is
begin
    insert into xg.xg_attendance(wid,aintime) values(wid,inTime);
    select xg.primarykeysequence.currval into aid from sys.dual;
end;
end AttendancePackage;


在ASP.NET中用企业库调用Oracle的存储过程

using System;
using System.Data;
using EntityLibrary;
using System.Data.Common;
using System.Diagnostics;

    
public class AttendanceBiz:Biz
    {
        
/// <summary>
        
/// 记录职工上班时间
        
/// </summary>
        
/// <param name="wid">职工号</param>
        
/// <param name="inTime">上班时间</param>
        
/// <returns>出勤记录编号</returns>
        public int SetInTime(string wid,string inTime)
        {
            DbCommand cmd 
= _database.GetStoredProcCommand("AttendancePackage.proc_insertAttendance");

            _database.AddInParameter(cmd, 
"wid", DbType.String,wid);
            _database.AddInParameter(cmd, 
"inTime", DbType.String,inTime);
            _database.AddOutParameter(cmd, 
"aid", DbType.Int32, 22);

            cmd.Connection 
= _database.CreateConnection();

            
int aid = 0;
            
try
            {
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                aid 
= Convert.ToInt32(cmd.Parameters["aid"].Value);
            }
            
catch(DbException e)
            {
                
if (!EventLog.Exists("csit"))
                {
                    EventLog.CreateEventSource(
"csit""csit");
                }
                EventLog.WriteEntry(
"csit", e.Message);
            }
            
finally
            {
                cmd.Connection.Close();
            }
            
            
return aid;
        }
     }


相关参考:http://www.cnblogs.com/tintown/archive/2005/03/02/111459.html

posted on 2008-02-20 10:21  钢钢  阅读(873)  评论(0编辑  收藏  举报

导航