0

I would like to know how can I store data in an array. The reason is because I am required to loop through the array, in order to detect any multiple data.

I have the code and tested it to run through for any multiple data in array and it works, but that array does not contain data from database. Right now I really need to store the column data in the database. This is my code.

double[] array1; // store the sql column data here.

int count=0;
bool maxreached=false;
for(int i=0;i<array1.Length;i++)
{
   if(array1[i]==text)
   count++;
   if(count>1)
   {
      maxreached=true;
      break;
   }
}   

if(maxreached)
{
}
else
{
}
  • Your array does not have any data in it because you are not putting any data in it. You are simply declaring an array and then checking the empty array. – PhoenixReborn Dec 05 '13 at 11:35
  • I'm pretty sure he knows this, he is asking how to get the data from the database and put it in the array. IMO, this is more worthy of a search on google than a question on stackoverflow. – Tobberoth Dec 05 '13 at 11:41

2 Answers2

1

If you want to get the data from the sql-table, you should look at this: Read SQL Table into C# DataTable. once you have all your data in the datatable you can use a for loop to go through all the rows and columns using Datatable.Row[i].Column[j] where i and j are either numbers or strings(i will most likely be a number and j a string) and save the contents in your array

Community
  • 1
  • 1
Rooxo
  • 118
  • 8
0

You do not need to loop over the array data. You can use the Contains method to search for a specific value.

// Load the data
double[] array1 = new double[] {
    1d,
    2d,
    3d,
    4d,
    5d
};

double searchValue = 5;

if (array1.Contains(searchValue))
{
    Console.WriteLine("Found " + searchValue);
}
else
{
    Console.WriteLine("*NOT* Found " + searchValue);
}
Kami
  • 19,134
  • 4
  • 51
  • 63