C # Passing parameters when calling SQL Server Parameters
-Test environment generated by SQL SERVER:
Create database Test; goUSE [Test]GOif OBJECT_ID('Tab2','U') is not nulldrop table Tab2goCREATE TABLE [dbo].[Tab2]([ID] [int] IDENTITY(1,1) NOT NULL,[TabID] [int] NOT NULL,[Name2] [nvarchar](50) 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 @Rowcount=@@ROWCOUNTgo
-- Open Visual Studio-create a 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);Database=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(return_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}Return_value:{1},parameters[2].Value,parameters[3].Value); Console.ReadKey(); } }}
-- Press F5 to run the result: