【问题标题】:Store in a variable the number of rows from a table where a column has a certain value in SQL Server Compact将表中的行数存储在变量中,其中列在 SQL Server Compact 中具有特定值
【发布时间】:2017-06-18 05:02:02
【问题描述】:

我有一个 Windows 窗体应用程序和一个 SQL Server Compact 3.5 数据库。我想将列具有特定值的表中的行数存储在变量中。我有这样的东西来计算表中的所有行:

CarsDataSet carsData = new CarsDataSet();
int nrCars = carsData.CarName.Rows.Count;

我怎样才能得到我需要的信息?

【问题讨论】:

    标签: c# sql-server winforms compact-database


    【解决方案1】:

    你可以这样得到它。

    dcarsData.CarName.Rows.Select("CompanyName Like 'SomeString%'").Count()
    

    【讨论】:

      【解决方案2】:

      首先,您需要编写返回查询中行数的 SQL 命令(使用 count(*) 运算符)。 SQL 语句的 where 子句是您可以过滤您想要的特定汽车的地方(例如 Model = 'Ford')

      string sql = "select count(*) from Cars where SomeColumn='{put_your_filter_here}'";
      

      现在创建一个 SqlCommand 对象,我们将在您的 SqlCeConnection 上执行该对象。您需要打开到本地 Sql Compact Edition 数据库的 SqlCeConnection。

      SqlCeCommand cmd = new SqlCeCommand(sql, DbConnection.ceConnection);
      

      执行返回 count(*) 行数并存储在汽车变量中的命令

      int cars = (int)cmd.ExecuteScalar();
      

      使用/打印查询结果

      Console.WriteLine("Number of cars: " + cars);
      

      【讨论】:

      • @Xufox 现在好点了吗?
      • @Mangist 好多了!
      猜你喜欢
      • 1970-01-01
      • 2023-01-19
      • 2017-07-04
      • 1970-01-01
      • 1970-01-01
      • 2022-12-09
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多