Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Executing Stored Procs With Params
Message
Information générale
Forum:
ASP.NET
Catégorie:
Bases de données
Divers
Thread ID:
00979533
Message ID:
00979623
Vues:
15
Kevin,

I'd like to create a function in my data access class that
runs stored procedures and returns the result.

It would need to recieve the procedure name as well as
any parameters.

Problem is, I'm not sure how to put this together. How would
I receive the parameters to pass to SQL as parameters in
this function?

How would I account for the various types of return values?

Thanks!



>Hey, Kevin,
>
>There are a number of ways you can do this. If you have a SQL back-end and want to return the result into a dataset, the following example can work:
>
>
>string cConnectStr	= "user id=sa;password=;initial catalog=Mydatabase;data source=MyServer";
>SqlConnection oSqlConn = new SqlConnection(cConnectStr);
>DataSet DsMyData = new DataSet();
>SqlDataAdapter oSqlAdapter = new SqlDataAdapter(cMyStoredProcName, oSqlConn);
>
>SqlParameter parmID = new SqlParameter("@nCustID",SqlDbType.Int);    // ncustID is the actual parameter name in the SP
>parmID.Value = nCustId;    // nCustID is the integer containing the value you want to pass
>
>oSqlAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
>oSqlAdapter.SelectCommand.Parameters.Add(parmID);
>oSqlAdapter.Fill( DsMyData,"MyDataSet");
>oSqlConn.Close();
>
>
>Again, there are many different ways, and you'd want to abstract out the connection object handling and other things, but hopefully that should give you an idea. If you had two parameters, you'd create a second parameter object and add it.
>
>Hope that helps,
>Kevin
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform