简体   繁体   English

使用 C# 从 SQL 表中提取特定数据

[英]Using C# to pull specific data from a SQL table

I'm working on a project where I'm required to pull specific data from a table in SQL.我正在从事一个项目,我需要从 SQL 中的表中提取特定数据。 I'm fairly new to SQL and so all the examples given aren't helping my project process.我对 SQL 还很陌生,所以给出的所有例子都没有帮助我的项目过程。

I'm trying to pull only the rows that have four particular values in them but I'm not sure how to do that while in C#.我试图只提取其中有四个特定值的行,但我不确定在 C# 中如何做到这一点。 Do I have to run one long query or can I run multiple and C# will have what it needs to pull what I need for the project?我是否必须运行一个很长的查询,还是可以运行多个查询,而 C# 将拥有它需要的东西来提取我需要的项目?

Here's where I'm at so far.这就是我目前所处的位置。

public static List<string> GetSmoothieFlavors()
{
    List<string> flavors = new List<string>();
    SqlCommand getFlavorsQuery = new SqlCommand("select * from smoothieFlavors", conn);
    try
    {
        conn.Open();
        SqlDataReader reader = getFlavorsQuery.ExecuteReader();
        while (reader.Read())
        {
            flavors.Add(reader.GetString(0));
        }
        conn.Close();
        return flavors;
    }
    catch (Exception ex)
    {
        throw new POSException(ex);
    }
}

一种非常简单的方法是在查询中以这种方式使用where子句

SqlCommand getFlavorsQuery = new SqlCommand("select * from smoothieFlavors where column1 = value1 and column2 = value2", conn)

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

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