简体   繁体   English

将变量的数量存储在表中的行数中,其中列在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

I have a Windows Forms application and a SQL Server Compact 3.5 database. 我有一个Windows Forms应用程序和一个SQL Server Compact 3.5数据库。 I want to store in a variable the number of rows from a table where a column has a certain value. 我想将一个列具有特定值的表中的行数存储在变量中。 I have something like this to count all the rows from a table: 我有这样的事情来计数表中的所有行:

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

How can I get the information I need? 我如何获得所需的信息?

First you'll want to write your SQL command which returns the number of rows in the query (using the count(*) operator). 首先,您需要编写SQL命令,该命令返回查询中的行数(使用count(*)运算符)。 The where clause of the SQL statement is where you can filter what specific Cars you want (eg Model = 'Ford') 您可以在SQL语句的where子句中过滤所需的特定汽车(例如Model ='Ford')

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

Now create a SqlCommand object which we will execute on your SqlCeConnection. 现在创建一个SqlCommand对象,该对象将在您的SqlCeConnection上执行。 You'll need to open a SqlCeConnection to your local Sql Compact Edition database. 您需要打开到本地Sql Compact Edition数据库的SqlCeConnection。

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

Execute the command which returns the count(*) number of rows and stores in cars variable 执行返回count(*)行数并将其存储在cars变量中的命令

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

Use/print the result of the query 使用/打印查询结果

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

您可以像这样获得它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM