VB.net call Stored Procedure

Source: Internet
Author: User

1. In ASP. NetTheStored ProcedureAndOutput Parameters (Only executenonquery ())
Both parameters and direct return can receive

String sconnectionstring = user id = zdp; Pwd = zdp; initial catalog = aaa; Data Source = (local );
Sqlconnection conn = new sqlconnection (sconnectionstring );
Conn. open ();
Sqlcommand CM = new sqlcommand (aaaa, Conn );
Cm. commandtype = commandtype. storedprocedure;
Cm. Parameters. Add (@ ID, sqldbtype. Int, 4 );
Cm. Parameters. Add (@ test, sqldbtype. Int, 4 );
Cm. Parameters. Add (@ ouid, sqldbtype. Int, 4 );
Cm. Parameters [@ ID]. value = convert. toint32 (newid. Text. tostring ());
Cm. Parameters [@ test]. Direction = system. Data. parameterdirection. returnvalue;
Cm. Parameters [@ ouid]. Direction = system. Data. parameterdirection. output;
Cm. executenonquery ();
// Outret isStored ProcedureMediumOutput ParametersValue assigned to the outret variable
String outret = cm. Parameters [@ ouid]. value. tostring ();
// Returnvalue isStored ProcedureValue assigned to the return value variable returnvalue
String returnvalue = cm. Parameters [@ test]. value. tostring ();
// Mes, RET is the ID of two labels
Mes. Text = outret. tostring ();
Ret. Text = returnvalue. tostring ();

//-----------------------------------Stored Procedure

Create procedure AAAA
@ ID int,
@ Ouid int output
As
Set nocount on
Begin
If exists (select * From YG where id = @ ID)
Begin
Set @ ouid = @ ID
Return 0
End
Set @ ouid = @ ID
Return 1
End
Go

@@@

VBCallingStored ProcedureThe return value is not received. To receive the return value, you must setOutput Parameters

Public Function checkdatapor () as Boolean
Dim cmd as ADODB. Command
Dim Param as ADODB. Parameter
Dim iniinfo as clsclientini
Dim connstr as string

Set cmd = new ADODB. Command
Set iniinfo = new clsclientini
Connstr = iniinfo. connectstring ()
Cmd. activeconnection = connstr
Cmd. commandtext = "checkdatapor"
Cmd. commandtype = adcmdstoredproc
Set Param = cmd. createparameter ("m_pdc", adinteger, adparaminput, 4, m_pdc)
Cmd. Parameters. append Param
Set Param = cmd. createparameter ("errorcode", adinteger, adparamoutput)
Cmd. Parameters. append Param
Cmd. Execute

If cmd. parameters ("errorcode"). value = 1 then
Checkdatapor = false
Else
Checkdatapor = true
End if
Set cmd = nothing
Set iniinfo = nothing
End Function

VB.netCallStored ProcedureAnd getReturn ValueExample

//Stored ProcedureCode
Create procedure [addnewinvoiceinfo] (@ suserid int, @ sclaimmethod varchar (50), @ sinvoicenum varchar (50), @ sinvoicedate datetime, @ sbuycity varchar (50)
Insert into smb_invoiceinfo (memberid, claimmethod, invoicenum, invoicedate, invoicecity) values (@ suserid, @ sclaimmethod, @ sinvoicenum, @ sinvoicedate, @ sbuycity)
Return @ identity
Go

// CallStored ProcedureCode
Dim cn as new sqlconnection ()
Dim invoiceid as string
Try
CN. connectionstring = dbconstr
CN. open ()
Dim cmd as sqlcommand
Cmd = new sqlcommand ("addnewinvoiceinfo", CN)
Cmd. commandtype = commandtype. storedprocedure

Cmd. Parameters. Add (New sqlparameter ("@ suserid", sqldbtype. INT ))
Cmd. parameters ("@ suserid"). value = CINT (userid. Text)

Cmd. Parameters. Add (New sqlparameter ("@ sclaimmethod", sqldbtype. varchar, 50 ))
Cmd. parameters ("@ sclaimmethod"). value = transname. Text

Cmd. Parameters. Add (New sqlparameter ("@ sinvoicenum", sqldbtype. varchar, 50 ))
Cmd. parameters ("@ sinvoicenum"). value = invoicenum. Text

Cmd. Parameters. Add (New sqlparameter ("@ sinvoicedate", sqldbtype. datetime ))
Cmd. parameters ("@ sinvoicedate"). value = invoicedate. selecteddate. Date

Cmd. Parameters. Add (New sqlparameter ("@ sbuycity", sqldbtype. varchar, 50 ))
Cmd. parameters ("@ sbuycity"). value = buycity. Text

Cmd. Parameters. Add (New sqlparameter ("@ sinvoiceid", sqldbtype. INT ))
Cmd. parameters ("@ sinvoiceid"). Direction = parameterdirection. returnvalue

Cmd. executenonquery ()
Invoiceid = cmd. parameters ("@ sinvoiceid"). value. tostring ()
Cmd. Dispose ()
Catch ex as exception
Response. Write (ex. tostring ())
Finally
CN. Close ()
End try

 

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.