Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to take one date that I stored in one column to another date that I stored as well in another column, but it gives me an inffulencent error.

The code:
C#
SQLiteCommand mathCommand = new SQLiteCommand("SELECT Cast (( JulianDay(sFinishedTime) - JulianDay(sStartedTime)) * 24 As Integer) FROM Database WHERE sSerialNumber = '" + sFinishedSerialNumber.Text + "'", conn);
Thanks.

What I have tried:

Nothing yet, I’ve tried to edit the columns.
Posted
Updated 17-Aug-20 5:51am
v2
Comments
Sandeep Mewara 16-Aug-20 5:59am    
It would be more helpful if you copy-paste the error here. Thanks.

Use Improve question or reply to me comment here.

1 solution

Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

The chances are that when you have fixed that throughout your whole app, the problem you have noticed will have gone as well.
 
Share this answer
 
Comments
Member 13182057 16-Aug-20 3:42am    
But what’s the specific solution for the sql command
OriginalGriff 16-Aug-20 3:53am    
Change it to a parameterised query:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT Age, Description FROM myTable WHERE ID = @ID", con))
        {
        cmd.Parameters.AddWithValue("@ID", myTextBox.Text);
        using (SqlDataReader reader = cmd.ExecuteReader())
            {
            while (reader.Read())
                {
                int age = (int) reader["Age"];
                string desc = (string) reader["Description"];
                Console.WriteLine($"{age}\n{desc}");
                }
            }
        }
    }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900