C # How to execute a stored procedure

Source: Internet
Author: User
Tags oracleconnection

 

Taking Oracle as an example, other databases only need to change database objects. The following is a parameter. If there is no parameter, paramete is null.

 

Parameter correspondence

 

 

Public static int UploadStartCloseSoftInfo (string [] args)

{

OracleParameter parameter1 = new OracleParameter (parameter 1, OracleType. type );

Parameter1.Value = type conversion (args [0]);

OracleParameter parameter2 = new OracleParameter (parameter name 2, OracleType. type );

Parameter2.Value = type conversion (args [1]);

......

OracleParameter parametern = new OracleParameter (parameter name n, OracleType. type );

Parametern. Value = type conversion (args [n-1]);

OracleParameter [] parameter

= {Parameter1, parameter2,..., parametern };

Return SDProvider. RunProcedure (stored procedure name, parameter );

}

}

}

Public static int UploadStartCloseSoftInfo (string [] args)

{

OracleParameter parameter1 = new OracleParameter (parameter 1, OracleType. type );

Parameter1.Value = type conversion (args [0]);

OracleParameter parameter2 = new OracleParameter (parameter name 2, OracleType. type );

Parameter2.Value = type conversion (args [1]);

......

OracleParameter parametern = new OracleParameter (parameter name n, OracleType. type );

Parametern. Value = type conversion (args [n-1]);

OracleParameter [] parameter

= {Parameter1, parameter2,..., parametern };

 

Return SDProvider. RunProcedure (stored procedure name, parameter );

}

}

}

 

Run

 

 

Public class SDProvider

{

Public static string GetConnectionString ()

{

String conStr = connection string;

Return conStr;

}

Public static int RunProcedure (string prName, IDataParameter [] parameters)

{

Try

{

Using (OracleConnection connection =

New OracleConnection (GetConnectionString ()))

{

Connection. Open ();

OracleCommand command =

BuildQueryCommand (connection, storedProcName, parameters );

Return command. ExecuteNonQuery ();

}

}

Catch

{

}

Return 0;

}

Private static OracleCommand BuildQueryCommand

(OracleConnection connection,

String storedProcName,

IDataParameter [] parameters)

{

OracleCommand command = new OracleCommand (storedProcName, connection );

Command. CommandType = CommandType. StoredProcedure;

If (parameters! = Null)

{

Foreach (OracleParameter parameter in parameters)

{

Command. Parameters. Add (parameter );

}

}

Return command;

}

}

Public class SDProvider

{

Public static string GetConnectionString ()

{

String conStr = connection string;

Return conStr;

}

Public static int RunProcedure (string prName, IDataParameter [] parameters)

{

Try

{

Using (OracleConnection connection =

New OracleConnection (GetConnectionString ()))

{

Connection. Open ();

OracleCommand command =

BuildQueryCommand (connection, storedProcName, parameters );

 

Return command. ExecuteNonQuery ();

}

}

Catch

{

}

Return 0;

}

Private static OracleCommand BuildQueryCommand

(OracleConnection connection,

String storedProcName,

IDataParameter [] parameters)

{

OracleCommand command = new OracleCommand (storedProcName, connection );

Command. CommandType = CommandType. StoredProcedure;

If (parameters! = Null)

{

Foreach (OracleParameter parameter in parameters)

{

Command. Parameters. Add (parameter );

}

}

Return command;

}

}

 

Note:

 

1. The stored procedure name is correct.

 

2. Parameter 1 and parameter 2. These names must be the same as the parameter names in the stored procedure. The order and type must be consistent.

From Poplar

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.