Monday, September 29, 2008

Using Sql Parameters :ASP.NET & C#

//How to use sql parameters to fetch records using dataset (C#).
//Sql Parameters with Dataset

public DataSet GetDataSet(string strConnect, string ProcName,Array parameters)
{
//**************************************
//* Purpose: Getting DataSet for the given Procedure if we have to pass parameters
//* Input parameters:
//*strConnect----Connection string
//*ProcName ---StoredProcedures name
//* Returns :
//*Dataset contains data
//* ************************************
string strCommandText = ProcName;
DataSet ds = new DataSet();

SqlConnection objConnect = new SqlConnection(strConnect);

SqlCommand objCommand = new SqlCommand(strCommandText, objConnect);
objCommand.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter param in parameters)
{
objCommand.Parameters.Add(param);
}

objConnect.Open();
SqlDataAdapter objDataAdapter = new SqlDataAdapter();
objDataAdapter.SelectCommand = objCommand;
objDataAdapter.Fill(ds);
objConnect.Close();
return ds;




}

'Please rate this if you like it

2 comments:

Rituraj Vyas said...

hey! ..thnx a lot for sharing the info, I was actually looking for sth like this- my project is stuck in mid. But thnx to you. Keep posting such articles.

Sky said...

thats true... this code will give you the freedom that you dont need to worry about how many parameters are there in your SP.

nice function... thanx for sharing.