Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果2表的值相同,则不会插入_C#_Sql_Sql Server - Fatal编程技术网

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

C# 如果2表的值相同,则不会插入,c#,sql,sql-server,C#,Sql,Sql Server,我的问题是,我想创建一个注册系统,例如,如果时间冲突,该系统将不会插入值 我有两张桌子,一张是登记表,一张是科目表。这是征兵用的 表:主题 现在我的问题是,如果我插入了一个与时间和天数冲突的值,我的程序只会插入它,而不会给出错误。无法插入计划是冲突的原因,这就是我的问题 这是我想要的可能输出: 我将在登记示例中插入一个值: 48490000,48490000,103,上午8:00-9:30,MWF 现在,这将创建一个错误,不能插入冲突计划,它现在将允许它,这是我的代码 string offe

我的问题是,我想创建一个注册系统,例如,如果时间冲突,该系统将不会插入值

我有两张桌子,一张是登记表,一张是科目表。这是征兵用的

表:主题

现在我的问题是,如果我插入了一个与时间和天数冲突的值,我的程序只会插入它,而不会给出错误。无法插入计划是冲突的原因,这就是我的问题

这是我想要的可能输出:

我将在登记示例中插入一个值: 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();

我假设您可以访问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
declare@登记表
(
登记ID int,
StudID int,
奥弗诺国际酒店,
开始时间日期时间,
EndTime日期时间,
StudDays varchar(3)
)
插入@登记值
(48490000,48490000,101,'08:00:00','09:00:00','MWF'),
(48490000,48490000,102,'08:00:00','09:00:00','TTh')
声明@subject表
(
奥弗诺国际酒店,
sub varchar(10),
描述varchar(200),
国际单位,
瓦查尔房间(20),
StudDays varchar(3),
开始时间日期时间,
EndTime日期时间,
容量int
)
在@subject值中插入
(101,“IT 14”,“软件工程”,3,“IT L2”,“MWF”,“08:00:00”,“09:00:00”,4),
(102,‘IT 15’、‘多媒体系统’、‘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','Saving History',3,'CH302','MWF','08:00:00','09:00:00',5),
(105,“英语3”,“演讲和演说家”,3,“演讲实验室”,“MWF”,“09:00:00”,“10:00:00”,5)
声明@newStudID int=48490000;
声明@newStart datetime='08:00:00'
声明@newEnd datetime='09:00:00'
声明@newDays varchar(3)='MWF'
声明@newOfferNo int=103
插入@登记
选择@newStudID、@newStudID、@newOfferNo、@newStart、@newEnd、@newDays
不存在的地方(
从@Enrolment中选择1,其中StudID=@newStudID

和(@newStart>=StartTime和@newStart=@newStart和StartTime=@newStart和EndTime我假设您有权访问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
declare@登记表
(
登记ID int,
StudID int,
奥弗诺国际酒店,
开始时间日期时间,
EndTime日期时间,
StudDays varchar(3)
)
插入@登记值
(48490000,48490000,101,'08:00:00','09:00:00','MWF'),
(48490000,48490000,102,'08:00:00','09:00:00','TTh')
声明@subject表
(
奥弗诺国际酒店,
sub varchar(10),
描述varchar(200),
国际单位,
瓦查尔房间(20),
StudDays varchar(3),
开始时间日期时间,
EndTime日期时间,
容量int
)
在@subject值中插入
(101,“IT 14”,“软件工程”,3,“IT L2”,“MWF”,“08:00:00”,“09:00:00”,4),
(102,‘IT 15’、‘多媒体系统’、‘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','Saving History',3,'CH302','MWF','08:00:00','09:00:00',5),
(105,“英语3”,“演讲和演说家”,3,“演讲实验室”,“MWF”,“09:00:00”,“10:00:00”,5)
声明@newStudID int=48490000;
声明@newStart datetime='08:00:00'
声明@newEnd datetime='09:00:00'
声明@newDays varchar(3)='MWF'
声明@newOfferNo int=103
插入@登记
选择@newStudID、@newStudID、@newOfferNo、@newStart、@newEnd、@newDays
不存在的地方(
从@Enrolment中选择1,其中StudID=@newStudID

及((@newStart>=StartTime和@newStart=@newStart和StartTime=@newStart和EndTime您需要在表中为不希望插入重复项的列添加一个唯一的键约束。我认为这不是关于重复项,而是关于主题中的提供103和登记中的提供103之间的不同时间,否?是否尝试检查不存在(...)例如,在我的登记表中,我没有103,8:00-9:00 AM,MWF,如果我将它插入登记表,它将创建一个错误,因为在我的登记表中,我已经有了相同的时间a天,即101,8:00-9:00 AM,MWF。现在这是我的问题,我的代码将只插入一个nd它将不显示计划正在执行的消息conflict@p.marino哦,还没有。你能给我看一下吗?谢谢,如果可以的话。你需要在表中为不希望插入重复项的列添加一个唯一的键约束。我认为这不是重复项,而是主题中的报价103和登记中的报价103之间的不同时间,没有?你有吗您尝试检查不存在的文件(…)例如,在我的登记表中,我没有103,8:00-9:00 AM,MWF,如果我将它插入登记表,它将创建一个错误,因为在我的登记表中,我已经有了相同的时间a天,即101,8:00-9:00 AM,MWF。现在这是我的问题,我的代码将只插入一个nd它将不显示计划正在执行的消息conflict@p.marino哦,还没有。你能给我看看吗?如果是的话,谢谢ok@Jontahan非常感谢你,先生,你刚刚解决了我的问题,我花了5个小时来解决This@Jontahan非常感谢先生,你刚刚解决了我的问题,我花了5个小时来解决这个问题