-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