1
我在這裏有一個firebird中的插入過程,它所做的是驗證我試圖保存的數據是否存在於表中。如果我試圖保存的數據存在於表格中,那麼數據將不會被保存。 如果是這種情況,我將如何提示用戶數據不存在,因爲數據存在?如何從Firebird數據庫提示消息到C#程序插入過程
這裏是我的插入過程
CREATE PROCEDURE TRAINTRN_INSERT(
EMP_PK INTEGER,
TRAINTRN_EMP_TYPE VARCHAR(50) CHARACTER SET ISO8859_1 COLLATE ISO8859_1,
TRAINTRN_BATCH VARCHAR(50) CHARACTER SET ISO8859_1 COLLATE ISO8859_1,
TRAINTRN_AREA VARCHAR(100) CHARACTER SET ISO8859_1 COLLATE ISO8859_1,
TRAINTRN_SRCVCRDT DECIMAL(12, 2),
TRAINING_PK INTEGER,
USER_PK SMALLINT,
TRAINTRN_UPDATETIME TIMESTAMP)
AS
DECLARE VARIABLE EXSTING_EMP_PK INTEGER;
DECLARE VARIABLE EXSTING_TRAINING_PK INTEGER;
BEGIN
FOR
SELECT
COUNT(A.EMP_PK)
FROM TRAINTRN_TABLE A WHERE A.TRAINING_PK =: TRAINING_PK AND A.EMP_PK =: EMP_PK
INTO
:EXSTING_EMP_PK
DO
IF (EXSTING_EMP_PK = 0) THEN
BEGIN
INSERT INTO TRAINTRN_TABLE (
EMP_PK,
TRAINTRN_EMP_TYPE,
TRAINTRN_BATCH,
TRAINTRN_AREA,
TRAINTRN_SRCVCRDT,
TRAINING_PK,
USER_PK,
TRAINTRN_UPDATETIME)
VALUES (
:EMP_PK,
:TRAINTRN_EMP_TYPE,
:TRAINTRN_BATCH,
:TRAINTRN_AREA,
:TRAINTRN_SRCVCRDT,
:TRAINING_PK,
:USER_PK,
CURRENT_TIMESTAMP);
END
END;
這裏是我的C#
var adder = new ClsHR();
adder.SaveParticipants();
XtraMessageBox.Show("Successfully Added.", "Save ",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
public void SaveParticipants()
{
try
{
var adder = new FbConnection(ClsConnection.FirebirdSQL);
var fbcmd = new FbCommand("TRAINTRN_INSERT", adder)
{
CommandType = CommandType.StoredProcedure
};
fbcmd.Parameters.Add("@EMP_PK", FbDbType.VarChar).Value = UpdtHndlrEmp;
fbcmd.Parameters.Add("@TRAINTRN_EMP_TYPE", FbDbType.VarChar).Value = TRAINTRN_EMP_TYPE;
fbcmd.Parameters.Add("@TRAINTRN_BATCH", FbDbType.VarChar).Value = TRAINTRN_BATCH;
fbcmd.Parameters.Add("@TRAINTRN_AREA", FbDbType.VarChar).Value = TRAINTRN_AREA;
fbcmd.Parameters.Add("@TRAINTRN_SRCVCRDT", FbDbType.VarChar).Value = TRAINTRN_SRCVCRDT;
fbcmd.Parameters.Add("@TRAINING_PK", FbDbType.VarChar).Value = UpdateHandler;
fbcmd.Parameters.Add("@USER_PK", FbDbType.SmallInt).Value = ClsEmployee.USER_PK;
fbcmd.Parameters.Add("@TRAINTRN_UPDATETIME", FbDbType.VarChar).Value = EventTimestamp;
fbcmd.Connection.Open();
fbcmd.ExecuteNonQuery();
fbcmd.Connection.Close();
}
catch (Exception errorcode)
{
XtraMessageBox.Show(String.Format("Error in connection: {0}. Saving failed.", errorcode.Message), @"Server Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
'SELECT COUNT(A.EMP_PK)' - 這是InterBase /火鳥相當糟糕的主意 –