在变量中存储来自 table 的行数,其中列在 SQL Server Compact 中具有特定值

Store in a variable the number of rows from a table where a column has a certain value in SQL Server Compact

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

中的所有行
CarsDataSet carsData = new CarsDataSet();
int nrCars = carsData.CarName.Rows.Count;

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

这样就可以了

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

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

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

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

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

执行 returns count(*) 行数并存储在 cars 变量中的命令

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

Use/print查询结果

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