【问题标题】:Microsoft.Data.SQLite parameters not workingMicrosoft.Data.SQLite 参数不起作用
【发布时间】:2019-07-06 20:26:35
【问题描述】:

我正在使用 Microsoft.Data.SQLite 构建一个数据库信息亭。我在构建命令时使用参数化查询。这个 WAS 一直工作到 VS 出现构建问题,并且在应用了各种更新(如下详述)之后,我的参数不再被应用。将值硬编码到 commandText 中有效。

我之前用的是基本款

selectCommand.Parameters.Add("@Table", Table)

我尝试使用更明确的参数。这是完整的命令:

public static List<String> GetTables(string Table) // Get the Items in a Table
        {
            List<String> entries = new List<string>();

            using (SqliteConnection db = new SqliteConnection(ConnectionString)) // the db
            {
                db.Open();

                SqliteCommand selectCommand = new SqliteCommand("SELECT Item FROM @Table;", db);

                selectCommand.Parameters.Add("@Table", SqliteType.Text).Value = Table;

                SqliteDataReader query = selectCommand.ExecuteReader();

                while (query.Read()) // while still reading data
                {
                    entries.Add(query.GetString(0)); // add string to entries
                }

                db.Close();
            }

            return entries;
        }

但该参数仍未应用。这是完整的错误:

SQLite Error 1: 'near "@Table": syntax error'.
at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
at Microsoft.Data.Sqlite.SqliteCommand.<PrepareAndEnumerateStatements>d__62.MoveNext()
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader()
at DataAccessLibrary.DataAccess.GetTables(String Table)
at brogle.SelectionPage.<LoadItemGridContent>d__8.MoveNext()}   Microsoft.Data.Sqlite.SqliteException

显然,@Table 没有被更改为字符串 Table,其值为“Bulbs”。 (有一个“灯泡”表,再次将其硬编码到命令中可以按预期工作。)调试表明该命令接受参数@Table,值为“灯泡”。

为了解决我的构建问题(代码分析引发错误),我安装了 Microsoft.CodeAnalysis.FxCopAnalyzers,将 SQLitePCLRaw.bundle_winsqlite3 和 Microsoft.NETCore.UniversalWindowsPlatform 更新到最新稳定版,并将 VS 更新到 15.9.7。

如果您需要有关任何事情的更多详细信息,我可以提供。谢谢。

【问题讨论】:

  • 你不能这样参数化表名。您需要使用字符串连接添加它。

标签: c# sqlite uwp


【解决方案1】:

用于参数。你不能那样给表名,你可以做的解决方法是:

SqliteCommand selectCommand = new SqliteCommand("SELECT Item FROM "+Table, db);

更多详情请参考Retrieve data from the SQLite database官方文档。

【讨论】:

    猜你喜欢
    • 2021-10-20
    • 2013-12-19
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 2012-11-17
    • 2011-11-10
    • 2010-09-20
    • 2016-09-15
    相关资源
    最近更新 更多