[英]Update a specific row(sql) via UPDATE using if-statement in C#
If a course is full, the next student will stand in line, 1,2,3 etc.. (0 = not in line, and boolean Accepted
is true, meaning the student is in the course). 如果一门课程已满,下一位学生将排队等候,1,2,3等。(0 =不在线,布尔
Accepted
为真,意味着学生在课程中)。 If a student quits the course, there will be a free spot in course table. 如果学生退出课程,课程表中将有一个免费点。
However, using C# and OleDbCommand
, how can I update every student row once course table has free spot? 但是,使用C#和
OleDbCommand
,如果课程表有免费地点,我如何更新每个学生行? I want every student get 1 value less for each free spot and if student who was in queue #1 (about to hit queue 0), I want it to update the Accepted
boolean to true (meaning the student is in the course). 我希望每个学生每个免费地点获得1个值减去,如果队列#1(即将排队0),我希望它将
Accepted
boolean更新为true(意味着学生在课程中)。
My problem: 我的问题:
I want to update every student queue index to "--1" for each free spots. 我想将每个学生队列索引更新为每个免费点的“--1”。
I don't know how to change the specific student,with queue index 1, to boolean Accepted = true
, when the student is about to hit queue index 0. 当学生即将命中队列索引0时,我不知道如何将具有队列索引1的特定学生更改为布尔值
Accepted = true
。
I don't know how to do this since I don't know how to combine the db and C# properly. 我不知道怎么做,因为我不知道如何正确地组合db和C#。
This is how far I can go: 这是我能走多远:
The first code below, I counted amounts free spots in course via SELECT and reader 下面的第一个代码,我通过SELECT和读者计算了免费的点数
//-----------------------------
//SELECT..... WHERE FreeSpots > 0...
int = QueueCount = 0;
while (reader.Read()) {
QueueCount = ++QueueCount;
};
//----------------------------
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "UPDATE [Course_Student] SET QueueIndex = @queueindex, Accepted = @accepted WHERE QueueIndex > 0 AND CID = " + CourseID + ";";
cmd.Connection = conn;
while (QueueCount > 0) {
if (/*HELP: how do I check specific student with QueueIndex = 1?*/ == 1)
{
var pAccepted = new OleDbParameter("@accepted", SqlDbType.Bit);
pAccepted.Value = true;
cmd.Parameters.Add(pAccepted);
} else {
var pAccepted = new OleDbParameter("@accepted", SqlDbType.Bit);
pAccepted.Value = false;
cmd.Parameters.Add(pAccepted);
}
var pQueueIndex = new OleDbParameter("@queueindex", SqlDbType.Int);
pQueueIndex.Value = /*HELP: for every row, --x QueueIndex for every students in queue*/;
cmd.Parameters.Add(pQueueIndex);
FreeSpot = --FreeSpot; //for later, to update freespots in course-table
cmd.ExecuteNonQuery();
}
In other words, how can I check, in C# using OleDBCommand
or whatever, check specific value before updating? 换句话说,如何在C#中使用
OleDBCommand
或其他方式检查,在更新之前检查特定值? Is there something similar Read.Reader
method? 是否有类似的
Read.Reader
方法? Where I can check for every single row? 哪里可以检查每一行? To be honest, I'm completely lost here.
说实话,我完全迷失在这里。
change your sql statement: 更改你的sql语句:
UPDATE [Course_Student]
SET QueueIndex = QueueIndex - 1,
Accepted = (CASE QueueIndex WHEN 1 THEN 1 ELSE 0 END)
WHERE QueueIndex > 0 AND CID = @CourseID;
Hope this can help; 希望这可以帮助;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.