C # Calling stored procedures

Source: Internet
Author: User

Code highlighting produced by Actipro Codehighlighter (freeware) http://www.CodeHighlighter.com/-->/Database connection strings (Web. config to configure), you can dynamically change connectionstring to support multiple databases.  Public Static stringconnectionString = system.configuration.configurationmanager.connectionstrings["ConnectionString"].        ConnectionString; /// <summary>        ///executes the stored procedure and returns SqlDataReader (note: After calling the method, be sure to close the SqlDataReader)/// </summary>        /// <param name= "Storedprocname" >Stored Procedure name</param>        /// <param name= "Parameters" >Stored Procedure Parameters</param>        /// <returns>SqlDataReader</returns>         Public StaticSqlDataReader Runprocedure (stringstoredprocname, idataparameter[] parameters) {SqlConnection Connection=NewSqlConnection (connectionString);            SqlDataReader Returnreader; Connection.            Open (); SqlCommand Command=Buildquerycommand (connection, storedprocname, parameters); Command.commandtype=CommandType.StoredProcedure; Returnreader=command.            ExecuteReader (commandbehavior.closeconnection); returnReturnreader; }        /// <summary>        ///Executing stored procedures/// </summary>        /// <param name= "Storedprocname" >Stored Procedure name</param>        /// <param name= "Parameters" >Stored Procedure Parameters</param>        /// <param name= "TableName" >table names in dataset results</param>        /// <returns>DataSet</returns>         Public StaticDataSet Runprocedure (stringStoredprocname, idataparameter[] parameters,stringtableName) {            using(SqlConnection connection =NewSqlConnection (connectionString)) {DataSet DataSet=NewDataSet (); Connection.                Open (); SqlDataAdapter SqlDA=NewSqlDataAdapter (); Sqlda.selectcommand=Buildquerycommand (connection, storedprocname, parameters);                Sqlda.fill (DataSet, tableName); Connection.                Close (); returnDataSet; }        }         Public StaticDataSet Runprocedure (stringStoredprocname, idataparameter[] parameters,stringTableName,intTimes ) {            using(SqlConnection connection =NewSqlConnection (connectionString)) {DataSet DataSet=NewDataSet (); Connection.                Open (); SqlDataAdapter SqlDA=NewSqlDataAdapter (); Sqlda.selectcommand=Buildquerycommand (connection, storedprocname, parameters); SqlDA.SelectCommand.CommandTimeout=Times ;                Sqlda.fill (DataSet, tableName); Connection.                Close (); returnDataSet; }        }        /// <summary>        ///Building a SqlCommand object (used to return a result set instead of an integer value)/// </summary>        /// <param name= "Connection" >Database Connection</param>        /// <param name= "Storedprocname" >Stored Procedure name</param>        /// <param name= "Parameters" >Stored Procedure Parameters</param>        /// <returns>SqlCommand</returns>        Private StaticSqlCommand Buildquerycommand (SqlConnection connection,stringstoredprocname, idataparameter[] parameters) {SqlCommand command=NewSqlCommand (storedprocname, connection); Command.commandtype=CommandType.StoredProcedure; foreach(SqlParameter parameterinchparameters) {                if(Parameter! =NULL)                {                    //Check the output parameters for unassigned values and assign them to DBNull.Value.                    if(parameter. Direction = = Parameterdirection.inputoutput | | Parameter. Direction = = ParameterDirection.Input) &&(parameter. Value==NULL) {parameter. Value=DBNull.Value; } command.                Parameters.Add (parameter); }            }            returncommand; }        /// <summary>        ///executes a stored procedure that returns output parameter values/// </summary>        /// <param name= "Storedprocname" >Stored Procedure name</param>        /// <param name= "Parameters" >Stored Procedure Parameters</param>        /// <returns>Object</returns>         Public Static ObjectRunprocedure (stringStoredprocname, idataparameter[] paramenters) {            using(SqlConnection connection =NewSqlConnection (connectionString)) {connection.                Open (); SqlCommand Command=Buildquerycommand (Connection, Storedprocname, paramenters); Command.                ExecuteNonQuery (); ObjectObj=command. parameters["@Output_Value"]. Value;//@Output_Value corresponding to the specific stored procedure parameters                if(Object.Equals (obj,NULL)) ||(Object.Equals (obj, System.DBNull.Value))) {                    return NULL; }                Else                {                    returnobj; }            }        }        /// <summary>        ///executes the stored procedure, returning the number of rows affected/// </summary>        /// <param name= "Storedprocname" >Stored Procedure name</param>        /// <param name= "Parameters" >Stored Procedure Parameters</param>        /// <param name= "rowsaffected" >number of rows affected</param>        /// <returns></returns>         Public Static intRunprocedure (stringStoredprocname, idataparameter[] parameters, out introwsaffected) {            using(SqlConnection connection =NewSqlConnection (connectionString)) {                intresult; Connection.                Open (); SqlCommand Command=Buildintcommand (connection, storedprocname, parameters); Rowsaffected=command.                ExecuteNonQuery (); Result= (int) command. parameters["returnvalue"].                Value; //connection.close ();                returnresult; }        }        /// <summary>        ///Create an SqlCommand object instance (to return an integer value)/// </summary>        /// <param name= "Storedprocname" >Stored Procedure name</param>        /// <param name= "Parameters" >Stored Procedure Parameters</param>        /// <returns>SqlCommand Object Instance</returns>        Private StaticSqlCommand Buildintcommand (SqlConnection connection,stringstoredprocname, idataparameter[] parameters) {SqlCommand command=Buildquerycommand (connection, storedprocname, parameters); Command. Parameters.Add (NewSqlParameter ("returnvalue", SqlDbType.Int,4, ParameterDirection.ReturnValue,false,0,0,string. Empty, Datarowversion.default,NULL)); returncommand; }

C # calling stored procedures

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.