Click here to Skip to main content
14,328,723 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,
I want to iterate through all the rows (total =11) in a sqlserver2012 data table.I am getting only one row. Code is mentioned below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace sqlserver_database_ex_cp2
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = "Data Source=DESKTOP-F49I231;Initial Catalog=company;Integrated Security=True";

                conn.Open();
                //create the command

                SqlCommand command = new SqlCommand("select * from dbo.Employees where EMP_No =@param1", conn);

                //add the parameters
                

                command.Parameters.Add(new SqlParameter("param1", 1));
            
                /* get the rows and display on the screen
                 *This section of the code has the basic code
                 * that will the display the content from the database table.
                 * on the screen using the sqlDataReader */
                 
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    Console.WriteLine("EMP_NO\tEMP_NAME\tEMP_JOIN_DATE\tEMP_DEPT");

                    while(reader.Read())
                    {
                        Console.Write(string.Format("{0}\t| {1} \t | {2} \t | {3}", reader[0],
                            reader[1], reader[2], reader[3]));


                    }
                }

               
                        

            



            }
        }
    }
} 
I want all the rows to be displayed on the console. Please, help me.

What I have tried:

Whatever I have tried, mentioned in the code.
Posted
Updated 12-Jan-19 23:23pm
Comments
0x01AA 13-Jan-19 4:41am
   
Looks like where EMP_No =@param1 does Limit the result to one row...
Richard MacCutchan 13-Jan-19 5:00am
   
Good answer, but I cannot upvote a comment.
0x01AA 13-Jan-19 6:03am
   
Thanks for the motivation boost.
hemal p.shah 13-Jan-19 5:19am
   
What should be the code for more than one row then?
0x01AA 13-Jan-19 6:30am
   
See solution 1
Wendelius 13-Jan-19 5:21am
   
You should post this as an answer as it's most likely the reason.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

My guess is, that where EMP_No =@param1 does Limit the result to one row.
So simply Change your SQL to
select * from dbo.Employees
and remove (or comment out) the line
command.Parameters.Add(new SqlParameter("param1", 1));
I hope it helps.
   
Comments
Wendelius 13-Jan-19 5:37am
   
Good point, a 5.
0x01AA 13-Jan-19 5:40am
   
Thank you very much.
Maciej Los 14-Jan-19 6:50am
   
5ed!
0x01AA 14-Jan-19 6:51am
   
Thank you very much Maciej.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100