C#调用SQL Server参数过程传参

SQL SERVER生成测试环境:

  1. Create database Test;  
  2. go 
  3. USE [Test] 
  4. GO 
  5. if OBJECT_ID('Tab2','U') is not null 
  6.     drop table Tab2 
  7. go 
  8. CREATE TABLE [dbo].[Tab2]( 
  9.     [ID] [int] IDENTITY(1,1) NOT NULL
  10.     [TabID] [int] NOT NULL
  11.     [Name2] [nvarchar](50) NULL 
  12. )  
  13. GO 
  14. SET IDENTITY_INSERT [dbo].[Tab2] ON  
  15. GO 
  16. INSERT [dbo].[Tab2] ([ID], [TabID], [Name2]) VALUES (1, 245575913, N'ID'
  17. GO 
  18. INSERT [dbo].[Tab2] ([ID], [TabID], [Name2]) VALUES (2, 245575913, N'name'
  19. GO 
  20. INSERT [dbo].[Tab2] ([ID], [TabID], [Name2]) VALUES (3, 277576027, N'ID'
  21. GO 
  22. INSERT [dbo].[Tab2] ([ID], [TabID], [Name2]) VALUES (4, 277576027, N'Name2'
  23. GO 
  24. INSERT [dbo].[Tab2] ([ID], [TabID], [Name2]) VALUES (5, 277576027, N'TabID'
  25. GO 
  26. SET IDENTITY_INSERT [dbo].[Tab2] OFF 
  27. GO 
  28. if OBJECT_ID('P2','P') is not null 
  29.     drop procedure P2 
  30. go 
  31. Create procedure P2 
  32. @StartID int
  33. @EndID int
  34. @Rowcount int output 
  35. as 
  36. select * from Tab2 where ID between @StartID and @EndID 
  37. set @Rowcount=@@ROWCOUNT 
  38. go 
Create database Test; 
go
USE [Test]
GO
if OBJECT_ID('Tab2','U') is not null
	drop table Tab2
go
CREATE TABLE [dbo].[Tab2](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[TabID] [int] NOT NULL,
	[Name2] [nvarchar](50) NULL
) 
GO
SET IDENTITY_INSERT [dbo].[Tab2] ON 
GO
INSERT [dbo].[Tab2] ([ID], [TabID], [Name2]) VALUES (1, 245575913, N'ID')
GO
INSERT [dbo].[Tab2] ([ID], [TabID], [Name2]) VALUES (2, 245575913, N'name')
GO
INSERT [dbo].[Tab2] ([ID], [TabID], [Name2]) VALUES (3, 277576027, N'ID')
GO
INSERT [dbo].[Tab2] ([ID], [TabID], [Name2]) VALUES (4, 277576027, N'Name2')
GO
INSERT [dbo].[Tab2] ([ID], [TabID], [Name2]) VALUES (5, 277576027, N'TabID')
GO
SET IDENTITY_INSERT [dbo].[Tab2] OFF
GO
if OBJECT_ID('P2','P') is not null
	drop procedure P2
go
Create procedure P2
(
@StartID int,
@EndID int,
@Rowcount int output
)
as
select * from Tab2 where ID between @StartID and @EndID
set @Rowcount=@@ROWCOUNT
go
--打开Visual Studio—创建项目—选择【控制台应用程序】

  1. #region Using Directives 
  2. using System; 
  3. using System.Data; 
  4. using System.Data.SqlClient; 
  5. using System.Collections.Generic; 
  6. using System.Linq; 
  7. using System.Text; 
  8. using System.Threading.Tasks; 
  9. #endregion 
  10.  
  11. namespace TestExecute 
  12.     class Program 
  13.     { 
  14.         static void Main(string[] args) 
  15.         { 
  16.             SqlConnection thisConnection = new SqlConnection(@"Server=(Local);Database=Test;User ID=sa;Password=1"); 
  17.             thisConnection.Open(); 
  18.             SqlCommand thisCommand = thisConnection.CreateCommand(); 
  19.             thisCommand.CommandType = CommandType.StoredProcedure; 
  20.             thisCommand.CommandText = "P2"
  21.             IDataParameter[] parameters = { 
  22.                 new SqlParameter("@StartID",SqlDbType.Int), 
  23.                 new SqlParameter("@EndID",SqlDbType.Int), 
  24.                 new SqlParameter("@Rowcount",SqlDbType.Int), 
  25.                 new SqlParameter("return_value",SqlDbType.Int) 
  26.             }; 
  27.             parameters[0].Value = "1"
  28.             parameters[1].Value = "5"
  29.             parameters[2].Direction = ParameterDirection.Output; 
  30.             parameters[3].Direction = ParameterDirection.ReturnValue; 
  31.             thisCommand.Parameters.AddRange(parameters); 
  32.             thisCommand.ExecuteNonQuery(); 
  33.             thisConnection.Close(); 
  34.             Console.WriteLine("@Rowcount:{0}\nReturn_value:{1}",parameters[2].Value,parameters[3].Value); 
  35.             Console.ReadKey(); 
  36.         } 
  37.     } 
#region Using Directives
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#endregion

namespace 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}\nReturn_value:{1}",parameters[2].Value,parameters[3].Value);
            Console.ReadKey();
        }
    }
}

--按F5运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值