c# - Fill generic list from SqlDataReader

前端 未结 5 1856
轮回少年
轮回少年 2021-02-14 02:23

How can I add values that a SqlDataReader returns to a generic List? I have a method where I use SqlDataReader to get CategoryID from a

相关标签:
5条回答
  • 2021-02-14 02:42

    AS BrokenGlass explained this is the demonstration

    SqlConnection connection = null;
            SqlDataReader dr= null;
            SqlCommand cmd = null;
    List<int> catID = new List<int>();
            try
            {
                connection = new SqlConnection(connectionString);
                cmd = new SqlCommand("select CategoryID from Categories", connection );
    
                connection.Open();
    
    
    
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    catID.Add(Convert.ToInt32(dr["CategoryID"].ToString()));
                }
    
    
            }
            finally
            {
                if (connection  != null)
                    connection.Close();
            }
            return catID;
    

    as well as you change the declaration

    SqlDataReader reader = null;
    

    to

    SqlDataReader dr= null; // Because you are using dr in the code not reader
    
    0 讨论(0)
  • 2021-02-14 02:50

    This should work but I suggest you to use using with your connections

        SqlConnection connection = null;
        SqlDataReader reader = null;
        SqlCommand cmd = null;
        List<int> catID = new List<int>();
        try
        {
            connection = new SqlConnection(connectionString);
            cmd = new SqlCommand("select CategoryID from Categories", connection );
    
            connection.Open();
    
    
    
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {   
                catID.Add(Convert.ToInt32(dr["CategoryID"].ToString()));
            }
    
    
        }
        finally
        {
            if (connection  != null)
                connection.Close();
        }
        return catID;
    
    0 讨论(0)
  • 2021-02-14 02:54

    Try like this, it's better, safer, uses lazy loading, less code, working, ...:

    public IEnumerable<int> GetIds()
    {
        using (var connection = new SqlConnection(connectionString))
        using (var cmd = connection.CreateCommand())
        {
            connection.Open();
            cmd.CommandText = "select CategoryID from Categories";
            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    yield return reader.GetInt32(reader.GetOrdinal("CategoryID"));
                }
            }
        }
    }
    

    and then:

    List<int> catIds = GetIds().ToList();
    
    0 讨论(0)
  • 2021-02-14 02:54

    Your current code should work, assuming catID is really declared before the try block, otherwise this won't compile.

    0 讨论(0)
  • 2021-02-14 02:56
            List<int> s = new List<int>();
            conn.Open();
            SqlCommand command2 = conn.CreateCommand();
            command2.CommandText = ("select turn from Vehicle where Pagged='YES'");
            command2.CommandType = CommandType.Text;
            SqlDataReader reader4 = command2.ExecuteReader();
            while (reader4.Read())
            {
                s.Add(Convert.ToInt32((reader4["turn"]).ToString()));
            }
            conn.Close();
    
    0 讨论(0)
提交回复
热议问题