How to save SELECT sql query results in an array in C# Asp.net

后端 未结 6 568
时光取名叫无心
时光取名叫无心 2020-12-14 17:36

I have wrote this query to get some results, if I want to save the results in an array what I have to do? I want to use the values which are in col1 and col2 in an IF state

6条回答
  •  执念已碎
    2020-12-14 18:18

    Use a SQL DATA READER:

    In this example i use a List instead an array.

    try
    {
        SqlCommand comm = new SqlCommand("SELECT CategoryID, CategoryName FROM Categories;",connection);
        connection.Open();
    
        SqlDataReader reader = comm.ExecuteReader();
        List str = new List();
        int i=0;
        while (reader.Read())
        {
            str.Add( reader.GetValue(i).ToString() );
            i++;
        }
        reader.Close();
    }
    catch (Exception)
    {
        throw;
    }
    finally
    {
        connection.Close();
    }
    

提交回复
热议问题