1

NOTE: Many thanks to user @David. The issue has been resolved. That was my mistake. Please see the UPDATE section below.

Environment: Microsoft.Data.Sqlite, VS2019 v16.6.0, Windows10 Pro v1903

Question: why is the following code inserting the value "05-29-2020" into the last column DocContent? I need it to insert TestContent instead. What I may be missing here and how can it be resolved?

Remark: On a button click MyAddData() calls AddData()

private void MyAddData()
{
    for (Int16 t=0; t<5; t++)
        MathDocsContext.AddData("Test" + t + "Author", "Test" + t + "Title", "Test Content");
}

public static void AddData(string sAuthor, string sTitle, string sDocContent)
{
    string SQLiteDbpath = System.IO.Path.Combine(GetDeployedFilePath, "SQLiteDb.db");

    using (SqliteConnection db = new SqliteConnection($"Filename={SQLiteDbpath}"))
    {
        db.Open();

        using (SqliteCommand insertCommand = new SqliteCommand())
        {
            insertCommand.Connection = db;

            // Use parameterized query to prevent SQL injection attacks
            insertCommand.CommandText = "INSERT INTO Docs (Author, Title, DateModified, DocContent) VALUES (@Author, @Title, @DateModified, @DocContent);";
            insertCommand.Parameters.AddWithValue("@Author", sAuthor);
            insertCommand.Parameters.AddWithValue("@Title", sTitle);
            insertCommand.Parameters.AddWithValue("@DateModified", "05-29-2020");
            insertCommand.Parameters.AddWithValue("@DocContent", sDocContent);

            insertCommand.ExecuteNonQuery();
        }

        db.Close();
    }
}

SQLite table: data Inserted from above code

Remark: DocId is an auto-increment primary key identity column

UPDATE:

My apologies. I stand corrected. As user @David stated:

Could be a variety of things outside the context of the code we're looking at.

The Test Content value in the MyAddData() method is not a value; instead a variable is passed here that has a value of 05-29-2020.

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
nam
  • 21,967
  • 37
  • 158
  • 332
  • 2
    Running an old version of the code? Inserting into a different database instance than what you're observing? Could be a variety of things outside the context of the code we're looking at. – David May 29 '20 at 23:44
  • @David I stand corrected. You were right in stating: `Could be a variety of things outside the context of the code we're looking at`. I have added a `NOTE` and an `UPDATE` section to my original post. Thank you for pointing out what could have been wrong. – nam May 30 '20 at 01:38

0 Answers0