C # calls the SQL Server parameters procedure pass-through parameter

Source: Internet
Author: User
Tags rowcount

-sql server generates a test environment:

create database Test; gouse [test]goif object_id (' Tab2 ', ' U ') is not nulldrop table Tab2gocreate TABLE [dbo]. [TAB2] ([ID] [int] IDENTITY () not null,[tabid] [int.] not null,[name2] [nvarchar] () NULL) Goset identity_insert [dbo]. [TAB2] On Goinsert [dbo]. [TAB2] ([ID], [TabID], [Name2]) VALUES (1, 245575913, N ' ID ') Goinsert [dbo]. [TAB2] ([ID], [TabID], [Name2]) VALUES (2, 245575913, N ' name ') Goinsert [dbo]. [TAB2] ([ID], [TabID], [Name2]) VALUES (3, 277576027, N ' ID ') Goinsert [dbo]. [TAB2] ([ID], [TabID], [Name2]) VALUES (4, 277576027, N ' Name2 ') Goinsert [dbo]. [TAB2] ([ID], [TabID], [Name2]) VALUES (5, 277576027, N ' TabID ') goset identity_insert [dbo]. [TAB2] Offgoif object_id (' P2 ', ' P ') is not nulldrop procedure P2gocreate procedure P2 (@StartID int, @EndID int, @Rowcount int output ) Asselect * from TAB2 where ID between @StartID and @EndIDset @[email protected] @ROWCOUNTgo 
--Open visual studio-Create Project-Select "Console Application"

#region Using directivesusing system;using system.data;using system.data.sqlclient;using System.Collections.Generic;        Using system.linq;using system.text;using System.Threading.Tasks, #endregionnamespace testexecute{class Program { static void Main (string[] args) {SqlConnection thisconnection = new SqlConnection (@ "server= (Local) ;D atabase=test; User Id=sa;            Password=1 ");            Thisconnection.open ();            SqlCommand Thiscommand = Thisconnection.createcommand ();            Thiscommand.commandtype = CommandType.StoredProcedure;            Thiscommand.commandtext = "P2"; idataparameter[] Parameters = {New SqlParameter ("@StartID", SqlDbType.Int), new SqlParameter ("@EndID", SqlDbType.Int), New SqlParameter ("@Rowcount", SqlDbType.Int), New SqlParameter ("ret            Urn_value ", SqlDbType.Int)}; Parameters[0].            Value = "1"; PARAMETERS[1].            Value = "5"; ParametERS[2].            Direction = ParameterDirection.Output; PARAMETERS[3].            Direction = ParameterDirection.ReturnValue;            ThisCommand.Parameters.AddRange (Parameters);            Thiscommand.executenonquery ();            Thisconnection.close (); Console.WriteLine ("@Rowcount: {0}\nreturn_value:{1}", Parameters[2]. VALUE,PARAMETERS[3].            Value);        Console.readkey (); }    }}

--Press F5 to run the result:



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C # calls the SQL Server parameters procedure pass-through parameter

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.