Reputation: 21
I have a table "Entry" and it has columns "vehicle" and "arrival_time". I have set the present time to DateTime.Now . Now I want to check values in every row for the column "arrival_time". If "arrival_time" exceeds present time, then value of "vehicle" from that particular row is inserted into a new table "Update". If this condition is true for multiple rows, those rows must be inserted into the next table at a single button click.
I have searched many sites and youtube videos but I am not getting any satisfying answer. Please help. I am working in windows form application c#.
This code selects just one row.
Upvotes: 0
Views: 1164
Reputation: 1075
Please don't post code as an image, takes longer to transcribe for me
This is an altered version that will do all of the INSERTS
in one fell swoop, and avoids all of the network chit-chat involved with performing the logic within your application.
As I do not know what methods are available via cn
, I just wrote using ADO.
An addition that I made is the InsertQuantity
variable, which will return the "rows affected" value that SQL returns, and if there is an error in execution it will be set to -1;
private void RefreshBtn_Click(object Sender, EventArgs e) {
int InsertQuantity;
using (SqlConnection conn = new SqlConnection(strConnection)) {
using (SqlCommand cmd = new SqlCommand()) {
cmd.Connection = conn;
cmd.CommandText = "INSERT Alert(Vehicle_no) SELECT Vehicle_no FROM Entry WHERE arrival_time <= GetDate()";
try {
conn.Open();
InsertQuantity = cmd.ExecuteNonQuery();
}
catch {
InsertQuantity = -1;
// whatever error handling you need
}
finally { conn.Close(); }
}
}
// InsertQuantity will tell you how many "alerts" were added
// If = -1 then there was an error
}
Upvotes: 0
Reputation: 784
Create a stored procedure and use this :
INSERT INTO Alert
SELECT vehicle_no
FROM Entry
WHERE arrival_time > GETDATE()
then call the sproc from your code.
Upvotes: 1