如果2表的值相同,则不会插入

时间:2017-08-28 11:41:18

标签: c# sql sql-server

我的问题是,如果时间冲突,我想制作一个不会插入值的注册系统

我有一个两个表,即征兵和主题。这是入伍

Enlistment

表:主题 enter image description here

现在我的问题是,如果我插入一个会与时间和日期冲突的值,我的程序将只插入它,而不是给出错误计划不能插入导致冲突,这是我的问题。

这是我想要的输出:

我将在Enlistment示例中插入一个值: 48490000,48490000,103,8:00-9:30,MWF

现在这将创建一个错误,该错误无法插入冲突计划,现在它将允许它,这是我的代码

string offerNo = "", capacity = "", offNo = "" , time = "", day = "";
        string a = "", b = "";

        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();
        if (dr.Read())
        {
            offerNo = dr[0].ToString();
        }
        dr.Dispose();

        cmd = new SqlCommand("Select a.time, a.days from Enlistment a, Subject b where a.offerNo = b.offerNo AND a.studID = '" + textEnrollID.Text + "'", conn);
        dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            time = dr[0].ToString();
            day = dr[1].ToString();
        }
        dr.Dispose();

        cmd = new SqlCommand("Select offerNo,capacity,days,time from Subject  where offerNo ='" + textEnrollOfferNo.Text +  "'", conn);
        dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            offNo = dr[0].ToString();
            capacity = dr[1].ToString();
            a = dr[2].ToString();
            b = dr[3].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 ((a == day && b == time) || (day == a && time == b))
        {
            MessageBox.Show("The schedule is conflict!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            textEnrollOfferNo.Clear();
            textEnrollOfferNo.Focus();
        }
        else if ((textEnrollOfferNo.Text != offerNo && a != day && b != time) || (textEnrollOfferNo.Text != offerNo && a == day && b != time) || (textEnrollOfferNo.Text != offerNo && a != day && b == time))
        {
            //Button Add Subject for Student
            conn = koneksyon.getConnect();
            conn.Open();
            cmd = new SqlCommand("AddSubject", conn);
            cmd.CommandType = CommandType.StoredProcedure;


            cmd.Parameters.Add("@enlistID", SqlDbType.Int).Value = Convert.ToInt32(textEnrollID.Text);
            cmd.Parameters.Add("@studID", SqlDbType.Int).Value = Convert.ToInt32(textEnrollID.Text);
            cmd.Parameters.Add("@offerNo", 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 = koneksyon.getConnect();
            conn.Open();
            dataGridView1.Rows.Clear();
            cmd = new SqlCommand("Select a.offerNo,b.subj,b.description,b.units,b.room,b.days,b.time from Enlistment a, Subject b where a.offerNo = b.offerNo AND a.studID ='" + textEnrollID.Text + "'", conn);
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                dataGridView1.Rows.Add(dr[0], dr[1], dr[2], dr[3], dr[6], dr[4], dr[5]);
            }
            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 :(得分:2)

我假设您有权访问SQL Server Management Studio?如果是这样,请打开“新建查询”窗口并将以下内容粘贴到:

declare @enlistment table
(
EnlistID int,
StudID int,
OfferNo int,
StartTime datetime,
EndTime datetime,
StudDays varchar(3)
)
insert into @enlistment VALUES
(48490000, 48490000, 101, '08:00:00', '09:00:00', 'MWF'),
(48490000, 48490000, 102, '08:00:00', '09:00:00', 'TTh')

declare @subject table
(
OfferNo int,
subj varchar(10),
description varchar(200),
units int,
room varchar(20),
StudDays varchar(3),
StartTime datetime,
EndTime datetime,
Capacity int
)

INSERT INTO @subject VALUES
(101, 'IT 14', 'Software Engineering', 3, 'IT L 2', 'MWF', '08:00:00', '09:00:00', 4),
(102, 'IT 15', 'Multimedia Systems', 3, 'IT L 3', 'TTh', '17:00:00', '18:30:00', 5),
(103, 'Acctg 1', 'Intro to Accounting', 3, 'J03', 'TTh', '17:00:00', '18:30:00', 5),
(104, 'Re ed 1', 'Salvation History', 3, 'CH302', 'MWF', '08:00:00', '09:00:00', 5),
(105, 'Eng 3', 'Speech and Orators', 3, 'Speech Lab', 'MWF', '09:00:00', '10:00:00', 5)

declare @newStudID int = 48490000;
declare @newStart datetime = '08:00:00'
declare @newEnd datetime = '09:00:00'
declare @newDays varchar(3) = 'MWF'
declare @newOfferNo int = 103

INSERT INTO @enlistment 
SELECT @newStudID, @newStudID, @newOfferNo, @newStart, @newEnd, @newDays
WHERE NOT EXISTS (
SELECT 1 FROM @enlistment WHERE StudID = @newStudID 
and ((@newStart >= StartTime AND @newStart <= EndTime) OR  
(@newEnd <= EndTime AND @newEnd >= StartTime) OR 
(StartTime >= @newStart AND StartTime <= @newEND ) OR
(EndTime >= @newStart AND EndTime <= @newEND )) 
AND StudDays = @newDays)

SELECT @@ROWCOUNT as Success

如果你运行它,它将显示成功0,因为时间与现有课程重叠。玩开始和结束时间。它应该始终阻止与现有课程相交/重叠的任何组合。

同样的逻辑你可以简单地转移到你的c#(你可以逐字逐句的SQL)。您基本上可以选择:您可以运行整个INSERT并检查RowsAffected(如果无法插入则为0),或者您可以运行例如选择OfferNo FROM Enlistment WHERE ..作为单独的预检;如果结果是空结果集,那么你可以继续插入;如果它返回了某些内容,你可以给出一条消息,指出它与给定的课程编号冲突。在这两个中,第二个可能更友好!

附录

BTW在上面的例子中,我假设课程是MWF或TTh。如果你想应对其他组合,那么最好再改变表设计并让布尔代表周一,周二,周三,周四和周五。检查变得有点困难(!)但它仍然可行。如果是这种情况,请告诉我。