Friday, May 31, 2013

SP run helper

// Get Result from specific SP
int id   = 1;
int type = 0;
DataTable attributes = GetSpResults(conn
    , "[dbo].[sp_GetSomeAttributes]"
    , new SqlParameter("@id", (Int32)id)
    , new SqlParameter("@type", (Int32)type)
    );
   ...
// General request for DataTable
private DataTable GetSpResults(SqlConnection conn, string spName, params SqlParameter[] parms)
{
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = spName;
    foreach(SqlParameter parm in parms)
        cmd.Parameters.Add(parm);
    cmd.Connection = conn;

    SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd);
    DataTable dtable = new DataTable();
    sqlAdapter.Fill(dtable);

    return dtable;
}

No comments:

Post a Comment