I am creating C# application if the last value of trigger_0 is False insert my current date and time, help me to solve this:
String test = DateTime.Now.ToShortDateString();
String test1 = DateTime.Now.ToString("hh:mm");
con.Open();
SqlCommand cmdt = con.CreateCommand();
cmdt.CommandType = CommandType.Text;
cmdt.CommandText = "SELECT TOP 1 trigger_0 FROM WINDATAA ORDER BY trigger_0 DESC ";
cmdt.ExecuteNonQuery();
con.Close();
I guess you are trying to update all rows:
UPDATE WINDATAA
SET [date] = CONVERT(VARCHAR(10), GETUTCDATE(), 121)
,[time] = LEFT(CAST(GETUTCDATE() AS TIME), 5)
WHERE [triggger_0] = 'false' -- or [triggger_0] = 0 if boolean
If you need to modify only the last, you can use some id if you have, or get the maximum/last date in advanced and use the value in the WHERE
clause.
Just get result from your executed query and see whether it is true
. If true
then just insert your data:
bool trigger = true;
SqlCommand cmdt = con.CreateCommand();
cmdt.CommandType = CommandType.Text;
var result = cmdt.ExecuteScalar();
bool.TryParse(result, out trigger);
if (trigger == false) {
// insert your data. For example:
string query = "INSERT INTO dbo.FooTable (id,username) VALUES (@id,@username)";
SqlCommand command = new SqlCommand(query, db.Connection);
string preventNull = null;
preventNull = preventNull == null ? "Is not null" : "Hey!";
string fooUserName = null;
fooUserName = fooUserName == null ? "Bob not null" : "Hey!";
command.Parameters.Add("@id", preventNull );
command.Parameters.Add("@username", fooUserName );
command.ExecuteNonQuery();
}
If I understand it properly you want to check if the last created item in your table has a value true or false. If the field time in your database describes the creation time you can just order the items by date and time eg
SELECT TOP 1 trigger_0 FROM WINDATAA ORDER BY date, time DESC
Another option I see is if the id of your table is an integer then you can just order the items by Id:
SELECT TOP 1 trigger_0 FROM WINDATAA ORDER BY Id DESC
Then you can create a statement in C#, and use the data of the object if it's needed
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.