【问题标题】:Check if a database table contains any rows检查数据库表是否包含任何行
【发布时间】:2018-08-17 15:27:25
【问题描述】:

我正在将数据加载到具有 3 个Entry 控件的表单中。

我为此使用的对象称为mySettings,它是SystemSettings 的对象,SQLite 数据库中的类和数据库表。

到目前为止,我有这段代码,它可以按原样工作。

var db = new SQLiteConnection(dbPath);

Entry txtServer;
txtServer = new Entry { FontSize = 10 };
controlGrid.Children.Add(txtServer, 2, 0);
Grid.SetColumnSpan(txtServer, 4);

SystemSettings mySettings;
mySettings = db.Get<SystemSettings>(0);
txtServer.Text = mySettings.FTPServer;

但是,在我加载值之前,我需要检查SystemSettings 是否包含表中的任何行。

我在网上看过一些指南。

有人说使用类似的东西

SQLiteCommand cmd;
cmd = new SQLiteCommand(db);

...

int result = Convert.ToInt32(db.ExecuteScalar)

但是,我收到一个错误提示

SQLiteCommand 不包含任何包含 x 参数的方法

不管我传入多少(0个或更多)。

db. 中似乎也没有方法

那么在尝试使用不存在的数据之前,如何检查SystemSettings 是否包含任何行?

【问题讨论】:

    标签: c# sqlite xamarin


    【解决方案1】:

    下面的模式应该可以工作。 .ExecuteScalar() 方法实际上是在命令而不是连接上。

        int count;
        using (SQLiteConnection db = new SQLiteConnection("MY_CXN_STRING"))
        using (SQLiteCommand cmd = new SQLiteCommand("SELECT COUNT(*) FROM SystemSettings"))
        {
            db.Open();
            count = (int)cmd.ExecuteScalar();
            db.Close();
        }
    bool hasRows = count != 0;
    

    【讨论】:

    • 'SQLiteCommand':在 using 语句中使用的类型必须隐式转换为 'System.IDisposable' & 第二个错误'无法从用法推断 SQLiteCommand.ExecuteScalar() 的类型参数.尝试隐式指定类型参数'。
    【解决方案2】:

    基本上你要清除

    SystemSettings
    

    尝试只运行一个对数据库不返回任何内容的查询。例如:

    SystemSettings = $"SELECT * FROM TABLE_NAME WHERE COLUMN_NAME IS 'INVALID_EXPRESSIONdjeiq48724rufnjdrandom stuff'";
    

    无论如何都不是最优雅的解决方案,但它确实有效。

    【讨论】:

      【解决方案3】:

      您想要做的是获取SystemSettings 表中的第一行(如果有): 因此,您应该执行以下 Sql 语句(或类似的语句)并检查是否返回结果:

      Select * from SystemSettings LIMIT 1;
      

      您可以像这样执行查询并检查结果:

      public bool DoesTableContainRows(string tableName, SQLiteConnection connection)
      {
          var command = new SQLiteCommand($"Select * from {tableName } LIMIT 1;", connection);
          var resultReader = command.ExecuteReader();
          // check whether or not a row was returned
          bool containRows = resultReader.Read();
          resultReader.Close();
          return containRows;
      }
      

      编辑: 展示如何使用 .NET 和 Microsoft.Data.Sqlite 检查表是否包含行,包括更好地处理资源。

      public bool DoesTableContainRows(string tableName, SqliteConnection connection)
      {
          using (var command = new SqliteCommand($"Select * from {tableName } LIMIT 1;", connection))
          {
              using (var resultReader = command.ExecuteReader())
              {
                   // check whether or not a row was returned
                   bool containRows = resultReader.Read();
                   resultReader.Close();
                   return containRows;
              }
          }
      }
      

      【讨论】:

      • “SQLiteCommand 不包含采用 2 个方法的构造函数”,以及“SQLiteCommand 不包含 ExecuteReader 的定义,并且找不到接受 SQLiteCommand 的第一个参数类型的扩展 ExectuteReader”跨度>
      • 我正在使用 System.Data.SQLite.Core (1.0.109.1),但我假设您可能使用的是 .NET 而不是 .NET Core?如果是这种情况,我将在今晚更新我的答案以反映您的开发环境
      • 我相信是的。
      • @Harambe 我更新了使用 .NET 的答案,请参阅 Edit 部分。请注意,我使用 nuget 包 Microsoft.Data.Sqlite 可以找到 here
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-05
      • 2019-09-21
      • 1970-01-01
      • 2020-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多