Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Call Stored Proc And Return DataSet
Message
Information générale
Forum:
ASP.NET
Catégorie:
ADO.NET
Versions des environnements
Environment:
C# 3.0
OS:
Vista
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01359938
Message ID:
01359941
Vues:
27
Hi Kevin,

I have the following helper method in a static class to return DataSet
/// <summary>
    /// Executes A Stored Procedure
    /// </summary>
    /// <param name="StoredProcedure">Name of Stored Procedure</param>
    /// <param name="Parameters">The Parameters for the Stored Procedure</param>
    /// <remarks>Parameters can be null if none</remarks>
    /// <returns>DataSet</returns>    
    public static DataSet RunStoredProcToDS(string StoredProcedure,
       params SqlParameter[] Parameters)
    {        
        SqlConnection Conn = new SqlConnection(
            System.Configuration.ConfigurationManager.ConnectionStrings["FCCMSConnectionString"].ConnectionString);
        SqlCommand Command = new SqlCommand(StoredProcedure, Conn);
        
        if (Parameters != null)
        {
            Command.Parameters.Clear();
            Command.Parameters.AddRange(Parameters);
        }
         DataSet ds = new DataSet();
         SqlDataAdapter da = new SqlDataAdapter(StoredProcedure, Conn);

         Command.CommandType = CommandType.StoredProcedure;
         da.SelectCommand = Command;
        try
        {
            Conn.Open();
            //Results = Command.ExecuteNonQuery();
            da.Fill(ds);

        }
        finally
        {
            if (Conn.State == ConnectionState.Open)
                Conn.Close();
            Conn.Dispose();
        }
        return ds;
    }
>I'm using C#/. I want to call a SQL stored procedure, pass parameters, and return a DataSet. Here's what I have so far:
>
>
>SqlCommand oCommand = new SqlCommand("bm_GetProjects", oConn);
>oCommand.CommandType = CommandType.StoredProcedure;
>
>SqlParameter param1 = new SqlParameter();
>param1.DbType = DbType.Int32;
>param1.Direction = ParameterDirection.Input;
>param1.ParameterName = "@AppKey";
>param1.SourceColumn = "AppKey";
>param1.SqlDbType = SqlDbType.Int;
>param1.Value = 87;
>oCommand.Parameters.Add(param1);
>
>
>SqlParameter param2 = new SqlParameter();
>param2.DbType = DbType.Int32;
>param2.Direction = ParameterDirection.Input;
>param2.ParameterName = "@CategoryTypeKey";
>param2.SourceColumn = "CategoryTypeKey";
>param2.SqlDbType = SqlDbType.Int;
>param2.Value = 87;
>oCommand.Parameters.Add(param2);
>
>
>
>SqlParameter param3 = new SqlParameter();
>param3.DbType = DbType.Int32;
>param3.Direction = ParameterDirection.Input;
>param3.ParameterName = "@ProjectStatusCodeKey";
>param3.SourceColumn = "ProjectStatusCodeKey";
>param3.SqlDbType = SqlDbType.Int;
>param3.Value = 87;
>oCommand.Parameters.Add(param3);
>
>
>oDataSet = oCommand.;       // ***  WHAT GOES HERE??? ***
>
>
>
>All the examples I found return a DataReader. How do I get back a data set?
>
>Also, do I need to set all those properties on each parameter object?
>
>Thanks
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform