Example of calling sqlhelper. CS (multiple parameters are returned in the Stored Procedure)

Source: Internet
Author: User

Sqlconnection connection = NULL;
Try
{
Try
{
Connection = getconnection (txtconnectionstring. Text );
}
Catch
{
MessageBox. Show ("the connection with the database can be established", "application error", messageboxbuttons. OK, messageboxicon. Error );
Return;
}
// Set up parameters (1 input and 3 Output)
Sqlparameter [] arparms = new sqlparameter [4];

// @ Productid input parameter
// Assign = 1
Arparms [0] = new sqlparameter ("@ productid", sqldbtype. INT );
Arparms [0]. = 1;

// @ Productname output parameter
Arparms [1] = new sqlparameter ("@ productname", sqldbtype. nvarchar, 40 );
Arparms [1]. Direction = parameterdirection. output;

// @ Unitprice output parameter
Arparms [2] = new sqlparameter ("@ unitprice", sqldbtype. Money );
Arparms [2]. Direction = parameterdirection. output;

// @ Qtyperunit output parameter
Arparms [3] = new sqlparameter ("@ qtyperunit", sqldbtype. nvarchar, 20 );
Arparms [3]. Direction = parameterdirection. output;

// Call executenonquery static method of sqlhelper class
// We pass in database connection string, command type, stored procedure name and an array of sqlparameter objects
Sqlhelper. executenonquery (connection, commandtype. storedprocedure, "getproductdetails", arparms );

// Display results in text box using the s of output parameters
Txtresults. Text = arparms [1]. + "," + arparms [2]. + "," + arparms [3].;
}
Catch (exception ex)
{
String errmessage = "";
For (exception tempexception = ex; tempexception! = NULL; tempexception = tempexception. innerexception)
{
Errmessage + = tempexception. Message + environment. newline + environment. newline;
}

MessageBox. Show (string. Format ("there are some problems while trying to use the data access application block, please check the following error messages: {0 }"
+ Environment. newline + "This test requires some modifications to the northwind database. please make sure the database has been initialized using the setupdatabase. BAT database, or from the install Quickstart option on the Start Menu. ", errmessage ),
"Application error", messageboxbuttons. OK, messageboxicon. Error );
}
Finally
{
If (connection! = NULL)
Connection. Dispose ();
}

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.