.Net Basic

This blogs gives introduction to C#.NET programming for beginners. This blogs assumes that you have no programming experience whatsoever. It's a lot easier than you think, and can be a very rewarding hobby!after Refer this blog

26 June 2009

Call SELECT Stored Procedure in c#.net

Main

String strStoreProcName = "Store Procedure Name";
SqlCommand cmd = new SqlCommand(strStoreProcName);
//Add number of Paramaters
cmd.Parameters.AddWithValue("@Parameter Name in datebase", Parameter value);
datatable = GetData(cmd);

Function

public DataTable GetData(SqlCommand cmd)
{

DataTable dt = new DataTable();

String strConnString= System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;


SqlConnection con = new SqlConnection(strConnString);

cmd.Connection = con ;

SqlDataAdapter sda = new SqlDataAdapter();

cmd.CommandType = CommandType.StoredProcedure;

try
{

con.Open();

sda.SelectCommand = cmd;

sda.Fill(dt);

}

catch (Exception ex)
{

return dt;

}

finally
{

con.Close();

cmd.Dispose();

sda.Dispose();

}

return dt;

}

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home