Note that there are some explanatory texts on larger screens.

plurals
  1. PODelete row from SQL table (Date & Time values error)
    primarykey
    data
    text
    <p>I try to write and then delete from an SQL table. My WRITE code is OK, however my DELETE code does not work as I want.</p> <pre><code>//SQL_ConnectionString defined above SqlConnection SQL_Connection = new SqlConnection(SQL_ConnectionString) string INSERT = "INSERT INTO " + tbTable_DAQ.Text + " (Date, Time, Comment)"; string VALUES = "VALUES " + " (@Date, @Time, @Comment)"; string SQL_WriteString = INSERT + VALUES; DateTime DateTimeNow = DateTime.Now; try { //get time DateTimeNow = DateTime.Now; //create SQL command object SqlCommand SQL_Write = new SqlCommand(SQL_WriteString, SQL_Connection); //attach values SQL_Write.Parameters.AddWithValue("@Date", DateTimeNow); SQL_Write.Parameters.AddWithValue("@Time", DateTimeNow); SQL_Write.Parameters.AddWithValue("@Comment", "Table Access Test. "); SQL_Write.ExecuteNonQuery(); //dispose &amp; nullify SQL_Write SQL_Write.Dispose(); SQL_Write = null; } catch (Exception eWrite) { MessageBox.Show(eWrite.ToString(), "SQL WRITE ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } //now delete inserted row from SQL table string DELETE = "DELETE FROM " + tbTable_DAQ.Text + " "; string FROM = "WHERE " + "Date = " + DateTimeNow + "AND " + "Time = " + DateTimeNow + "AND " + "Comment='Table Access Test.'"; //not working string SQL_DeleteString = DELETE + FROM; try { //create SQL command object SqlCommand SQL_Delete = new SqlCommand(SQL_DeleteString, SQL_Connection); SQL_Delete.ExecuteNonQuery(); //dispose &amp; nullify SQL_Delete SQL_Delete.Dispose(); SQL_Delete = null; } catch (Exception eDelete) { MessageBox.Show(eDelete.ToString(), "SQL DELETE ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } </code></pre> <p>It give me an error. I believe the error is due to the values of the Date and Time column. On the WRITE code, I think the SQL table converts <code>DateTimeNow</code> to on receiving it on the <code>Date</code> (date type) and <code>Time</code> (time7 type). However, to delete that specific row the values must match (no conversion is done) and I do not know how to get the values right.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload