Database Programming in C#, returning output from Stored Proc

Posted by jpavlov on Stack Overflow See other posts from Stack Overflow or by jpavlov
Published on 2011-01-09T17:48:21Z Indexed on 2011/01/09 17:53 UTC
Read the original article Hit count: 208

Filed under:
|

I am working at gaining an understanding at how to interface stored procedures with applications. My example is simple, but it doesn't display my columns and rows in the command prompt, instead it display System.Data.SqlClient.SqlDataReader. How do I display the rows from my stored procudure?

----Stored Proc--

ALTER PROCEDURE dbo.SelectID
AS
SELECT * FROM tb_User;
-----

Below is the code:

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


namespace ExecuteStoredProc
{
    class Program
    {
        static void Main(string[] args)
        {

            SqlConnection cnnUserMan;
            SqlCommand cmmUser;
            //SqlDataReader drdUser;

            //Instantiate and open the connection
            cnnUserMan = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=c:\\Program Files\\Microsoft SQL Server\\MSSQL10.SQLEXPRESS\\MSSQL\\DATA\\UserDB.mdf; Integrated Security=True;Connect Timeout=30;User Instance=True");
            cnnUserMan.Open();

            //Instantiate and initialize command
            cmmUser = new SqlCommand("SelectID", cnnUserMan);
            cmmUser.CommandType = System.Data.CommandType.StoredProcedure;

          //drdUser = cmmUser.ExecuteReader();
           Console.WriteLine(cmmUser.ExecuteReader());
            Console.ReadLine();
        }
    }
}

Thanks.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET