Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
I am not sure how the whole Array thing works in C#.

Could someone give me a quick tutorial on how it works.
C#
protected void Button1_Click(object sender, EventArgs e)
   {
       Int64 n;
       dataFrom data = new dataFrom();
       String connStr = ConfigurationManager.ConnectionStrings["From"].ConnectionString;
       String cmdStr = "SELECT * FROM [Table2];";
       try
       {
           using (SqlConnection conn = new SqlConnection(connStr))
           {
               using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
               {
                   conn.Open();
                   using (SqlDataReader dr = cmd.ExecuteReader())
                   {
                       dr.Read();
                       n = n + 1;
                       //Array0[n]=dr[0];
                       //Array1[n]=dr[1];
                       //Array2[n]=dr[2];
                       //Array3[n]=dr[3];
                       //Array4[n]=dr[4];                            conn.Close();
                       cmd.Dispose();
                       conn.Dispose();
                   }
               }
           }
       }
       catch (Exception ex)
       {
           TextBox1.Text = ex.Message;
       }
   }
Posted
Updated 5-Sep-14 20:44pm
v4
Comments
[no name] 6-Sep-14 2:41am    
what do u want ?
What u did in top inside Executereader u r using cmd.ExecuteNonQuery();
Teledextri 6-Sep-14 2:51am    
cmd.ExecuteNonQuery(); was a typo.
I want an array with 1000 variables one for each column in the datareader.
[no name] 6-Sep-14 2:54am    
where is the problem. You want to read all the values after receive from database. Use List<string> getAllvalues=new List<string>();
getAllvalues.Add(dr[0]) something this way
Teledextri 6-Sep-14 2:58am    
Once you get them into a list how do you retrieve them?
Dilan Shaminda 6-Sep-14 2:56am    
use a Datatble to keep your values

Refer this sample to read data from SqlDataReader using C# & SQL Server Database:

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

namespace Sample
{
	class ReaderDemo
	{
		static void Main()
		{
			ReaderDemo rd = new ReaderDemo();
			rd.SimpleRead();
		}

		public void SimpleRead()
		{
			// declare the SqlDataReader, which is used in
			// both the try block and the finally block
			SqlDataReader rdr = null;

			// create a connection object
			SqlConnection conn = new SqlConnection(
"Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");

			// create a command object
			SqlCommand cmd  = new SqlCommand(
				"select * from Customers", conn);

			try
			{
				// open the connection
				conn.Open();

				// 1. get an instance of the SqlDataReader
				rdr = cmd.ExecuteReader();

				// print a set of column headers
				Console.WriteLine(
"Contact Name             City                Company Name");
				Console.WriteLine(
"------------             ------------        ------------");

				// 2. print necessary columns of each
                    record
				while (rdr.Read())
				{
					// get the results of each column
					string contact = (string)rdr["ContactName"];
					string company = (string)rdr["CompanyName"];
					string city    = (string)rdr["City"];

					// print out the results
					Console.Write("{0,-25}", contact);
					Console.Write("{0,-20}", city);
					Console.Write("{0,-25}", company);
					Console.WriteLine();
				}
			}
			finally
			{
				// 3. close the reader
				if (rdr != null)
				{
					rdr.Close();
				}

				// close the connection
				if (conn != null)
				{
					conn.Close();
				}
			}	
		}
	}
}
 
Share this answer
 
C#
using System;
namespace ArrayApplication
{
   class MyArray
   {
      static void Main(string[] args)
      {
         int []  n = new int[10]; /* n is an array of 10 integers */
         int i,j;


         /* initialize elements of array n */
         for ( i = 0; i < 10; i++ )
         {
            n[ i ] = i + 100;
         }

         /* output each array element's value */
         for (j = 0; j < 10; j++ )
         {
            Console.WriteLine("Element[{0}] = {1}", j, n[j]);
         }
         Console.ReadKey();
      }
   }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900