Database Programming in C#, returning output from Stored Proc
- by jpavlov
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.