【问题标题】:How can i use values that come out of a select statement using c#? [closed]如何使用 c# 中的 select 语句产生的值? [关闭]
【发布时间】:2013-01-05 10:49:30
【问题描述】:

我有一个 SQL Server 2012 数据库,我希望使用 select 语句对其进行查询,并将出现的每个结果放入任何类型的集合类型中,例如:列表

问题是我从来没有做过 SQL Queering,我真的需要你们的帮助,

我需要在名为“yt2016”的表中的标记列中搜索值...您可以通过编写代码来帮助我,我将不胜感激,在此先感谢。

【问题讨论】:

  • 你没有表现出任何努力,基本上是在要求某人为你做这项工作..你已经用与 SQL 无关的标签来标记它..
  • “具体一点”是在你开始提问时写的。
  • 您需要自己付出一些努力。如果你在谷歌上搜索c#ado.netselect,你的第一个命中应该是msdn.microsoft.com/en-us/library/dw70f090.aspx。该页面有很多示例可以帮助您入门。

标签: c# sql database select collections


【解决方案1】:
public class Foo
{
    // some properties
    public int Prop1 { get; set; }
    public string Prop2 { get; set; }
    // ...
}

public static IEnumerable<Foo> GetAllFoos(String columnFilter = null)
{

    const string sql = @"SELECT 
                    Columns ...
                 FROM 
                    dbo.TableFoo
                 WHERE
                    @SomeColumn IS NULL OR SomeColumn = @SomeColumn
                 ORDER BY
                     OrderColumn1 ASC, OrderColumn2 DESC";

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        using (SqlCommand cmd = new SqlCommand(sql, connection))
        {
            if (columnFilter == null)
                cmd.Parameters.AddWithValue("@SomeColumn", DBNull.Value);
            else
                cmd.Parameters.AddWithValue("@SomeColumn", columnFilter);
            connection.Open();
            using (var rdr = cmd.ExecuteReader())
            {
                var list = new List<Foo>();
                while (rdr.Read())
                {
                    var foo = new Foo();
                    foo.Prop1 = rdr.GetInt32(0);
                    foo.Prop2 = rdr.GetString(0);
                    list.Add(foo);
                }
                return list;
            }
        }
    }
}

一个更适合的例子可能是

using System;
using System.Data;
using System.Data.SqlClient;

public class DataAccess
{

    // This should be specific to your database.
    const string ConnectionString = 
        "Data Source=(local);Initial Catalog=Northwind;Integrated Security=true";

    const string TokenQuery @=
        "SELECT tokens FROM dbo.yt2016;"

    static IEnumerable<object> GetTokens()
    {
        using (var connection = new SqlConnection(connectionString))
        {
        // Create the Command and Parameter objects.
        var command = new SqlCommand(TokenQuery, connection);

        // Create and execute the DataReader, 
        // casting and yielding to the caller.
        connection.Open();
        using (var reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                yield return reader[0];
            }
        }
    }
}

【讨论】:

  • @Jodrell:感谢您的编辑。但是,reader[0] 不能转换为 TToken 如果那是一个自定义类。即使使用yield,我也会继续使用using,或者你认为这是一个问题吗?
  • 谢谢大家,也谢谢那些给我建议让我先努力学习的人。
  • @user1999584:你可能会接受答案。
猜你喜欢
  • 2023-01-19
  • 2013-08-11
  • 1970-01-01
  • 2015-09-17
  • 2013-10-13
  • 2015-02-23
  • 1970-01-01
  • 2013-09-17
  • 2021-06-07
相关资源
最近更新 更多