C#注册系统计划冲突

时间:2017-08-29 16:02:47

标签: c# sql sql-server

我的系统出现问题,我想在数据库中插入一个值后出错。这是我的样本以获得更好的解释。

我有两个表,即Enlistment和Subject,这是样本值。

My Table with sample Value

现在我的问题是我想放错误我的时间会相同或冲突,例如: 我将在Enlistment表上添加一个值,就是这个。

I insert new value to Enlistment Table

现在这将创建一个错误,因为您可以在Subject表中看到, offerNo = 102在Enlistment表中具有相同的StartTime,EndTime和插入值天数值。然后,如果它不是相同的值,它将只插入表中而不会出现错误。你能给我一些代码,关于如何解决这个问题,谢谢你的帮助

这是我的precedure代码

CREATE PROCEDURE [dbo].[addSubject]
(
@newStudID int,
@newofferNo int
)
AS
BEGIN
BEGIN TRANSACTION

declare @newStart datetime
set @newStart = (select StartTime from Subject where offerNo = @newofferNo)
declare @newEnd datetime
set @newEnd = (select EndTime from Subject where offerNo = @newOfferNo)
declare @newDays varchar(50)
set @newDays = (select StudDays from Subject where offerNo = @newOfferNo)

insert into Enlistment values(@newStudID, @newStudID, @newOfferNo, @newStart, @newEnd, @newDays)

UPDATE Subject
SET Capacity = Capacity - 1
WHERE offerNo = @newofferNo

COMMIT TRANSACTION 
END

现在这是我在前端的代码

        conn = koneksyon.getConnect();
        conn.Open();
        cmd = new SqlCommand("Select a.offerNo  from Enlistment a, Subject b where a.offerNo = b.offerNo AND a.offerNo ='" + textEnrollOfferNo.Text + "' AND a.studID = '" + textEnrollID.Text + "'", conn);
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            offerNo = dr[0].ToString();
        }
        dr.Dispose();

        cmd = new SqlCommand("Select offerNo,Capacity,StartTime, EndTime,StudDays from Subject  where offerNo ='" + textEnrollOfferNo.Text +  "'", conn);
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            offNo = dr[0].ToString();
            capacity = dr[1].ToString();
            start = dr[2].ToString();
            end = dr[3].ToString();
            day = dr[4].ToString();

        }
        dr.Dispose();

        cmd = new SqlCommand("Select StartTime, StudDays from Enlistment where StudDays =  '" + day + "'", conn);
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            START = dr[0].ToString();
            DAY = dr[1].ToString();
        }
        dr.Dispose();


        cmd.Dispose();
        conn.Close();
        if (textEnrollOfferNo.Text == "")
        {
            MessageBox.Show("Input Offer No.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            textEnrollOfferNo.Clear();
            textEnrollOfferNo.Focus();
        }
        else if (textEnrollOfferNo.Text == offerNo)
        {
            MessageBox.Show("Cannot insert duplicate schedules", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            textEnrollOfferNo.Clear();
            textEnrollOfferNo.Focus();
        }
        else if (offNo != textEnrollOfferNo.Text)
        {
            MessageBox.Show("Offer No doesn't exist!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);

            textEnrollOfferNo.Clear();
            textEnrollOfferNo.Focus();
        }
        else if (textEnrollOfferNo.Text == offNo && Convert.ToInt32(capacity) == 0)
        {
            MessageBox.Show("Subject is closed!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            textEnrollOfferNo.Clear();
            textEnrollOfferNo.Focus();
        }
        else if( (textEnrollOfferNo.Text != offerNo  && START == start && DAY == day) || (offerNo != textEnrollOfferNo.Text && start == START && day == DAY) || (textEnrollOfferNo.Text != offerNo && START == start && day == DAY) || (textEnrollOfferNo.Text != offerNo && start == START && DAY == day))
        {
            MessageBox.Show("Subject is conflict!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            textEnrollOfferNo.Clear();
            textEnrollOfferNo.Focus();
        }
        else
        {
            //Button Add Subject for Student
            conn = koneksyon.getConnect();
            conn.Open();
            cmd = new SqlCommand("AddSubject", conn);
            cmd.CommandType = CommandType.StoredProcedure;


            cmd.Parameters.Add("@newStudID", SqlDbType.Int).Value = Convert.ToInt32(textEnrollID.Text);
            cmd.Parameters.Add("@newofferNo", SqlDbType.Int).Value = Convert.ToInt32(textEnrollOfferNo.Text);

            cmd.ExecuteNonQuery();
            conn.Close();
            cmd.Dispose();

            MessageBox.Show("Added Successfully! ", "SUCCESS", MessageBoxButtons.OK, MessageBoxIcon.Information);
            conn = koneksyon.getConnect();
            conn.Open();
            dataGridView1.Rows.Clear();
            cmd = new SqlCommand("Select a.offerNo,b.subj,b.description,b.units,b.StartTime, b.EndTime,b.room,b.StudDays from Enlistment a, Subject b where a.offerNo = b.offerNo AND a.studID ='" + textEnrollID.Text + "' ORDER BY a.StudDays, a.StartTime", conn);
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                string startTime = dr[4].ToString();
                string EndTIme = dr[5].ToString();
                DateTime start1 = DateTime.Parse(startTime);
                DateTime end1 = DateTime.Parse(EndTIme);

                string Time = start1.ToString("h:mm") + " - " + end1.ToString("h:mm tt");

                dataGridView1.Rows.Add(dr[0], dr[1], dr[2], dr[3], Time, dr[6], dr[7]);
            }
            dr.Dispose();
            cmd.Dispose();
            conn.Close();

            conn = koneksyon.getConnect();
            conn.Open();

            cmd = new SqlCommand("Select sum(b.units) from Enlistment a, Subject b where a.offerNo = b.offerNo AND a.studID ='" + textEnrollID.Text + "'", conn);
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                textTotalUnits.Text = dr[0].ToString();
            }
            dr.Dispose();
            conn.Close();
            cmd.Dispose();
        }
        dr.Dispose();
        cmd.Dispose();
        conn.Close();

1 个答案:

答案 0 :(得分:0)

不确定如何使用C#代码,使用C#连接数据库的技术是什么? (EF?L2S ?, OR Mapping,Raw sql?...等)但如果使用此查询存在任何冲突,您可以使用存储过程或原始查询进行检查

declare @studentId  int = 1000,
        @offerId int = 102,
        @existOffer int 
select @existOffer = t.OfferNo
from Subject c, Subject t
where c.OfferNo = @offerId 
and   c.StartTime = t.StartTime
and   c.Days = t.Days
and   c.OfferNo!= t.OfferNo
and exists(
   select 1
   from Enlistment e 
   where e.OfferNo = t.OfferNo and e.StudentID = @studentId
)

现在如果@existOffer不为空,则表示存在冲突,您可以显示与最终用户存在冲突的消息。

希望这会对你有所帮助