Skip to main content

Retrieve SQL Table Column data into a Array

When I was searching how to “Retrieve SQL Table Column data into an Array”, I found many ways to do that. But I was not happy about those solutions. So I searched for it so many times. Finally today I found a way to do that easily. So I thought to share with you all.

 

public static string[] Genre()

{

string Query = "SELECT <Column Name> FROM <Table Name>";

SqlConnection ConnObj = new SqlConnection(Connection());

ConnObj.Open();

SqlCommand CommObj = new SqlCommand(Query, ConnObj);

SqlDataReader Reader = CommObj.ExecuteReader();

string[] TempArray = new string[200];

int Name = Reader.GetOrdinal("Name"); //Gets the column ordinal, given the name of the column.

int i = 0;

while (Reader.Read())

{

TempArray[i] = Reader.GetString(Name);

i++;

}

Reader.Close();

return TempArray;

}

This function will return an array of the values.You can use it anyway you want….

Comments

Unknown said…
really usefull your post,bcoz they are with descriptive way.......

thx...