Click here to Skip to main content
15,921,577 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
C#
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
[no name] 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.
[no name] 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?
[no name] 13-Jan-19 6:30am    
See solution 1

1 solution

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.
 
Share this answer
 
Comments
Wendelius 13-Jan-19 5:37am    
Good point, a 5.
[no name] 13-Jan-19 5:40am    
Thank you very much.
Maciej Los 14-Jan-19 6:50am    
5ed!
[no name] 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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900