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
.