예제 #1
0
        void EntryUpdate()
        {
            int      FID;
            int      FParentID;
            DateTime FUpdateTime;
            int      FSeqNo;

            // ID check.
            if (string.IsNullOrEmpty(txtFID.Text))
            {
                MessageBox.Show("ID is empty.");
                return;
            }
            if (!int.TryParse(txtFID.Text, out FID))
            {
                MessageBox.Show("ID is not a integer value.");
                return;
            }

            // Fields check.
            if (string.IsNullOrEmpty(lblFUpdateTime.Text))
            {
                MessageBox.Show("Update time is empty.");
                return;
            }
            if (!lblFUpdateTime.Text.ZParseTime_MsDash(out FUpdateTime))
            {
                MessageBox.Show("Update time is not a valid datetime value.");
                return;
            }

            FParentID = -1;
            if (string.IsNullOrEmpty(txtFParentID.Text))
            {
                MessageBox.Show("Parent ID is empty.");
                return;
            }
            else
            {
                if (!int.TryParse(txtFParentID.Text, out FParentID))
                {
                    MessageBox.Show("Parent ID is not an integer.");
                    return;
                }
            }

            FSeqNo = -1;
            if (!string.IsNullOrEmpty(txtFSeqNo.Text))
            {
                if (!int.TryParse(txtFSeqNo.Text, out FSeqNo))
                {
                    MessageBox.Show("SeqNo is not an integer.");
                    return;
                }
            }

            // IO Check.
            DataRow row1 = DBReadRow_ID(FID);

            if (row1 == null)
            {
                MessageBox.Show($"Record does not exist. ID={FID}.");
                return;
            }
            string sFUpdateTime_DB = row1["FUpdateTime"].ZToDateTime().ZToString_MsDash();

            if (lblFUpdateTime.Text != sFUpdateTime_DB)
            {
                MessageBox.Show($"Someone else may change the record already. ID={FID}, Last Update Time={lblFUpdateTime.Text}");
                return;
            }

            Boolean bKeyChanged  = false;
            long    FParentID_DB = row1["FParentID"].ZToLong();
            string  FKey_DB      = row1["FKey"].ZToString();

            if (FParentID != FParentID_DB)
            {
                bKeyChanged = true;
            }
            if (txtFKey.Text != FKey_DB)
            {
                bKeyChanged = true;
            }
            if (bKeyChanged)
            {
                if (!DBExistParentID(FParentID))
                {
                    MessageBox.Show($"Parent record does not exist. ParentID={FParentID}.");
                    return;
                }

                // You can skip the validation if DB. supports unique index.
                if (DBExistParentIDAndKey(FParentID, txtFKey.Text))
                {
                    MessageBox.Show($"Record exists. ParentID={FParentID}, Key={txtFKey.Text}");
                    return;
                }

                if (DBExistChild(FID))
                {
                    MessageBox.Show($"Parent record can not update (ParentID and Key) fields if any child record exists. ID={FID}");
                    return;
                }
            }


            // IO Action.
            int    iAffected = 0;
            string sCmd      = "Update TConfig set FParentID=@FParentID, FSeqNo=@FSeqNo, FKey=@FKey, FValue=@FValue, FValueB=@FValueB, FReadonly=@FReadonly, FNote=@FNote, FUpdateTime=(datetime('now', 'localtime')) where FID=@FID and FUpdateTime=@FUpdateTime";

            try
            {
                using (SQLiteConnection cn1 = new SQLiteConnection(_sConnectionString))
                    using (SQLiteCommand command = cn1.CreateCommand())
                    {
                        command.CommandText = sCmd;
                        command.Parameters.AddWithValue("@FParentID", FParentID.ZToObject_DBNull());
                        command.Parameters.AddWithValue("@FSeqNo", FSeqNo.ZToObject_DBNull());
                        command.Parameters.AddWithValue("@FKey", txtFKey.Text.ZToObject_DBNull());
                        command.Parameters.AddWithValue("@FValue", txtFValue.Text.ZToObject_DBNull());
                        command.Parameters.AddWithValue("@FValueB", txtFValueB.Text.ZToObject_DBNull());
                        command.Parameters.AddWithValue("@FReadonly", chkReadonly.Checked.ZToObject_DBNull());
                        command.Parameters.AddWithValue("@FNote", txtFNote.Text.ZToObject_DBNull());
                        command.Parameters.AddWithValue("@FID", FID.ZToObject_DBNull());
                        command.Parameters.AddWithValue("@FUpdateTime", FUpdateTime.ZToObject_DBNull());
                        cn1.Open();
                        iAffected = command.ExecuteNonQuery();
                    }
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
                return;
            }
            if (iAffected != 1)
            {
                MessageBox.Show("Fail.");
                return;
            }
            myLoadKeyList();
            MessageBox.Show("OK.");
        }
예제 #2
0
        void EntryAdd()
        {
            int FParentID;
            int FSeqNo;

            // ID check.
            if (!string.IsNullOrEmpty(txtFID.Text))
            {
                MessageBox.Show("ID is not empty. ID is an IDENTITY field in DB.");
                return;
            }

            // Fields check.
            if (string.IsNullOrEmpty(txtFKey.Text))
            {
                MessageBox.Show("Key is empty.");
                return;
            }

            FParentID = -1;
            if (string.IsNullOrEmpty(txtFParentID.Text))
            {
                MessageBox.Show("Parent ID is empty.");
                return;
            }
            else
            {
                if (!int.TryParse(txtFParentID.Text, out FParentID))
                {
                    MessageBox.Show("Parent ID is not an integer.");
                    return;
                }
            }

            FSeqNo = -1;
            if (!string.IsNullOrEmpty(txtFSeqNo.Text))
            {
                if (!int.TryParse(txtFSeqNo.Text, out FSeqNo))
                {
                    MessageBox.Show("SeqNo is not an integer.");
                    return;
                }
            }

            // IO Check.
            int    iAffected = 0;
            string sCmd      = string.Empty;

            if (!DBExistParentID(FParentID))
            {
                MessageBox.Show($"Parent record does not exist. ParentID={FParentID}.");
                return;
            }
            // You can skip the validation if DB. supports unique index.
            if (DBExistParentIDAndKey(FParentID, txtFKey.Text))
            {
                MessageBox.Show($"Record exists. ParentID={FParentID}, Key={txtFKey.Text}.");
                return;
            }

            // Action.
            iAffected = 0;
            sCmd      = "INSERT INTO TConfig (FParentID, FSeqNo, FKey, FValue, FValueB, FReadonly, FNote) VALUES (@FParentID, @FSeqNo, @FKey, @FValue, @FValueB, @FReadonly, @FNote)";
            try
            {
                using (SQLiteConnection cn1 = new SQLiteConnection(_sConnectionString))
                    using (SQLiteCommand command = cn1.CreateCommand())
                    {
                        command.CommandText = sCmd;
                        command.Parameters.AddWithValue("@FParentID", FParentID.ZToObject_DBNull());
                        command.Parameters.AddWithValue("@FSeqNo", FSeqNo.ZToObject_DBNull());
                        command.Parameters.AddWithValue("@FKey", txtFKey.Text.ZToObject_DBNull());
                        command.Parameters.AddWithValue("@FValue", txtFValue.Text.ZToObject_DBNull());
                        command.Parameters.AddWithValue("@FValueB", txtFValueB.Text.ZToObject_DBNull());
                        command.Parameters.AddWithValue("@FReadonly", chkReadonly.Checked.ZToObject_DBNull());
                        command.Parameters.AddWithValue("@FNote", txtFNote.Text.ZToObject_DBNull());
                        cn1.Open();
                        iAffected = command.ExecuteNonQuery();
                    }
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
                return;
            }
            if (iAffected != 1)
            {
                MessageBox.Show("Fail.");
                return;
            }
            myLoadKeyList();
            MessageBox.Show("OK.");
        }