Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody,
in a C# WinApp I would to call a stored procedure and to display a results in a listView.I use datareader.

Thanks so much...
Posted

Hi, use below code

C#
SqlCommand command = new SqlCommand();
        connection.Open();
command.Connection = connection;
command.CommandType=CommandType.StoredProcedure;
command.CommandText = "SPName";
        SqlDataReader reader = command.ExecuteReader();

        if (reader.HasRows)
        {
            while (reader.Read())
            {
                Console.WriteLine("{0}\t{1}", reader.GetInt32(0),
                    reader.GetString(1));
            }
        }
        else
        {
            Console.WriteLine("No rows found.");
        }
        reader.Close();
 
Share this answer
 
You can use this..

C#
SqlCommand cmd=new SqlCommand("spMyProc", con);
cmd.CommandType=CommandType.StoredProcedure;
SqlDataReader sdr=new SqlDataReader();
sdr=cmd.ExecuteReader();
while(sdr.read()){
    //Your code goes here.
}
sdr.Dispose();
cmd.Dispose();
con.Close();
 
Share this answer
 
SqlConnection conn = new SqlConnection("connectionString");
SqlCommand cmd = new SqlCommand("storedProcedureName", conn);
cmd.CommandType=CommandType.StoredProcedure;
SqlDataReader rd = cmd.ExecuteReader();
if(rd.HasRows)
{
while(rd.read()){
//Your logic stays here
}
}
 
Share this answer
 
Start here[^].
 
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