我在我的系统中有一个问题,我想在将值插入数据库后发生错误。这是我更好解释的例子。C#注册系统调度冲突
我有两张表是Enlistment和Subject,这是样本值。现在
我的问题是我想把和我的错误的时间将是例如相同或冲突: 我将广告在入伍表,这是一个值。
现在,这将创建一个错误,因为你可以对主题表看, offerNo = 102有开始时间,结束时间,并插入值的天数在入伍表中的值相同。然后,如果它不是相同的值,它只会插入表中而不会出现错误。你能给我一些代码,就如何解决这个问题,谢谢大家的帮助
这是我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();
@scsimon感谢编辑先生 – Angelica
那么,为什么会有错误?这是你想要实现的约束吗?什么决定了一个错误? – scsimon
@scsimon我的坏先生,我的意思是,如果插入的值将具有相同的开始时间,结束时间和天数值,那么将在消息框中显示错误,否则,将只插入值 – Angelica