1

注意:非常感谢用户@David。问题已解决。那是我的错误。请参阅下面的更新部分。

环境Microsoft.Data.Sqlite,VS2019 v16.6.0,Windows10 Pro v1903

问题:为什么下面的代码将值"05-29-2020"插入到最后一列DocContent?我需要它来TestContent代替。我在这里可能缺少什么以及如何解决?

备注:在按钮上单击MyAddData()调用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 表:从上述代码插入的数据

备注DocId是一个自增主键标识列

更新

我很抱歉。我站得更正了。正如用户@David所说:

可能是我们正在查看的代码上下文之外的各种事情。

方法中的Test ContentMyAddData()不是值;相反,这里传递了一个值为 的变量05-29-2020

在此处输入图像描述

4

0 回答 0