System.Data : ParameterDirection参数类型

本文详细介绍了.NET框架中的ParameterDirection枚举类型,包括其四种参数类型:输入(Input)、输出(Output)、输入输出(InputOutput)和返回值(ReturnValue),并提供了一个使用SqlParameter的例子。此外,文章还解释了在调用存储过程时如何正确地使用这些参数类型。

1. System.Data

    The ParameterDirection values are used by the parameter direction properties of OleDbParameter and SqlParameter.

2.  ParameterDirection是一个枚举类型,提供了四种参数类型:

 

using  System;

namespace  System.Data
{
    
//  Summary:
    
//      Specifies the type of a parameter within a query relative to the System.Data.DataSet.
     public   enum  ParameterDirection
    {
        
//  Summary:
        
//      The parameter is an input parameter.
        Input  =   1 ,
        
//
        
//  Summary:
        
//      The parameter is an output parameter.
        Output  =   2 ,
        
//
        
//  Summary:
        
//      The parameter is capable of both input and output.
        InputOutput  =   3 ,
        
//
        
//  Summary:
        
//      The parameter represents a return value from an operation such as a stored
        
//      procedure, built-in function, or user-defined function.
        ReturnValue  =   6 ,
    }
}

 ==

    //  摘要:
    
//  指定查询内的有关 System.Data.DataSet 的参数的类型。
     public   enum  ParameterDirection
    {
        
//  摘要:
        
//      参数是输入参数。
        Input  =   1 ,
        
//
        
//  摘要:
        
//      参数是输出参数。
        Output  =   2 ,
        
//
        
//  摘要:
        
//      参数既能输入,也能输出。
        InputOutput  =   3 ,
        
//
        
//  摘要:
        
//      参数表示诸如存储过程、内置函数或用户定义函数之类的操作的返回值。
        ReturnValue  =   6 ,
    }

 

3.简单说明:

    1). .Net中的参数定义为形式参数 而把存储过程的参数定义为实际参数;

    2). 数据库存储过程的实际参数如果没有默认值则形式参数必须传值给实际参数;

    3). 但是如果形式参数的类型为ParameterDirection.Output 则传给实际参数的永远是空值;

    4). 如果形式参数的类型为ParameterDirection.ReturnValue 则形式参数不会传值给实际参数 实际参数必须有默认值  否则代码会报错;

    5). 如果形式参数类型为ParameterDirection.InputOutput 或者 ParameterDirection.Output 则实际参数必须有output 关键字.


4.Example 实例 :

static   void  GetSalesByCategory( string  connectionString,  string  categoryName)
{
    
using  (SqlConnection connection  =   new  SqlConnection(connectionString))
    {
        
//  Create the command and set its properties.
        SqlCommand command  =   new  SqlCommand();
        command.Connection 
=  connection;
        command.CommandText 
=   " SalesByCategory " ;
        command.CommandType 
=  CommandType.StoredProcedure;

        
//  Add the input parameter and set its properties.
        SqlParameter parameter  =   new  SqlParameter();
        parameter.ParameterName 
=   " @CategoryName " ;
        parameter.SqlDbType 
=  SqlDbType.VarChar;
        parameter.Direction 
=  ParameterDirection.Input;
        parameter.Value 
=  categoryName;

        
//  Add the parameter to the Parameters collection. 
        command.Parameters.Add(parameter);

        
//  Open the connection and execute the reader.
        connection.Open();
        SqlDataReader reader 
=  command.ExecuteReader();

        
if  (reader.HasRows)
        {
            
while  (reader.Read())
            {
                Console.WriteLine(
" {0}: {1:C} " , reader[ 0 ], reader[ 1 ]);
            }
        }
        
else
        {
            Console.WriteLine(
" No rows found. " );
        }
        reader.Close();
    }
}

 

    另外需要注意的是在.net中 System.DBNull.Value表示数据库参数为空值 而不是null.

 

         private   static   void  AttachParameters(SqlCommand command, SqlParameter[] commandParameters)
        {
            
if  (command  ==   null throw   new  ArgumentNullException( " command " );
            
if  (commandParameters  !=   null )
            {
                
foreach  (SqlParameter p  in  commandParameters)
                {
                    
if  (p  !=   null )
                    {
                        
//  Check for derived output value with no value assigned
                         if  ((p.Direction  ==  ParameterDirection.InputOutput  ||
                            p.Direction 
==  ParameterDirection.Input)  &&
                            (p.Value 
==   null ))
                        {
                            p.Value 
=  DBNull.Value;
                        }
                        command.Parameters.Add(p);
                    }
                }
            }
        }

转载于:https://www.cnblogs.com/Dlonghow/archive/2008/11/19/1336406.html

现在有.net6开发的api接口 代码如下using Microsoft.AspNetCore.Mvc; using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; using NHisApi.Models; using System.Data; namespace NHisApi.Controllers { [ApiController] [Route("api/[controller]")] public class NewClinicLabController : ControllerBase { private readonly HisContext _context; public NewClinicLabController(HisContext context) { _context = context; } public class sqd { public string patid { get; set;} public string ysdm { get; set;} public string ghks { get; set;} public string jzhm { get; set;} public string ztbh { get; set;} public string ztmc { get; set;} public string icd { get; set;} public string jbzd { get; set;} public string? jybs { get; set;} public string? jcmd { get; set;} public string? jczysx { get; set;} public string? bblx { get; set;} } [HttpPost("")] public async Task<IActionResult> NewClinicLab([FromBody] List<sqd> request) { try { var outputParam = new SqlParameter { ParameterName = "@output", SqlDbType = SqlDbType.VarChar, Size = 50, Direction = ParameterDirection.Output }; var checkIdParam = new SqlParameter { ParameterName = "@checkid", SqlDbType = SqlDbType.VarChar, Size = 50, Direction = ParameterDirection.Output }; foreach (var sqd in request) { var parameters = new[] { new SqlParameter("@patid",sqd.patid), new SqlParameter("@ysdm",sqd.ysdm), new SqlParameter("@ghks",sqd.ghks), new SqlParameter("@jzxh",sqd.jzhm), new SqlParameter("@ztbh",sqd.ztbh), new SqlParameter("@ztmc",sqd.ztmc), new SqlParameter("@icd",sqd.icd), new SqlParameter("@jbzd",sqd.jbzd), new SqlParameter("@jybs",sqd.jybs), new SqlParameter("@jcmd",sqd.jcmd), new SqlParameter("@jczysx",sqd.jczysx), new SqlParameter("@bblx",sqd.bblx), outputParam, checkIdParam }; await _context.Database.ExecuteSqlRawAsync("exec Clinic_lab_Insert_Nhis @patid,@ysdm,@ghks,@jzxh,@ztbh,@ztmc,@icd,@jbzd,@jybs,@jcmd,@jczysx,@bblx,@output output,@checkid output", parameters); } return Ok(new { resultcode = 200, message = "保存成功" }); } catch (Exception ex) { return StatusCode(500, new { resultcode = 500, message = "失败", error = ex.Message }); } } } } 我需要在执行Clinic_lab_Insert_Nhis这个存储过程后调用api接口 地址为 http://10.0.4.173:5000/api/addlis 入参为{ "jzhm":"", "doccode":"" }
03-08
现在有.net6开发的api接口 代码如下using Microsoft.AspNetCore.Mvc; using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; using NHisApi.Models; using System.Data; namespace NHisApi.Controllers { [ApiController] [Route(“api/[controller]”)] public class NewClinicLabController : ControllerBase { private readonly HisContext _context; public NewClinicLabController(HisContext context) { _context = context; } public class sqd { public string patid { get; set;} public string ysdm { get; set;} public string ghks { get; set;} public string jzhm { get; set;} public string ztbh { get; set;} public string ztmc { get; set;} public string icd { get; set;} public string jbzd { get; set;} public string? jybs { get; set;} public string? jcmd { get; set;} public string? jczysx { get; set;} public string? bblx { get; set;} } [HttpPost(“”)] public async Task<IActionResult> NewClinicLab([FromBody] List<sqd> request) { try { var outputParam = new SqlParameter { ParameterName = "@output", SqlDbType = SqlDbType.VarChar, Size = 50, Direction = ParameterDirection.Output }; var checkIdParam = new SqlParameter { ParameterName = "@checkid", SqlDbType = SqlDbType.VarChar, Size = 50, Direction = ParameterDirection.Output }; foreach (var sqd in request) { var parameters = new[] { new SqlParameter("@patid",sqd.patid), new SqlParameter("@ysdm",sqd.ysdm), new SqlParameter("@ghks",sqd.ghks), new SqlParameter("@jzxh",sqd.jzhm), new SqlParameter("@ztbh",sqd.ztbh), new SqlParameter("@ztmc",sqd.ztmc), new SqlParameter("@icd",sqd.icd), new SqlParameter("@jbzd",sqd.jbzd), new SqlParameter("@jybs",sqd.jybs), new SqlParameter("@jcmd",sqd.jcmd), new SqlParameter("@jczysx",sqd.jczysx), new SqlParameter("@bblx",sqd.bblx), outputParam, checkIdParam }; await _context.Database.ExecuteSqlRawAsync("exec Clinic_lab_Insert_Nhis @patid,@ysdm,@ghks,@jzxh,@ztbh,@ztmc,@icd,@jbzd,@jybs,@jcmd,@jczysx,@bblx,@output output,@checkid output", parameters); } return Ok(new { resultcode = 200, message = "保存成功" }); } catch (Exception ex) { return StatusCode(500, new { resultcode = 500, message = "失败", error = ex.Message }); } } } } 我需要在foreach后调用api接口 地址为 http://10.0.4.173:5000/api/addlis 入参为{ “jzhm”:“”, “ysdm”:“” } sqd为list 里面的jzhm和 ysdm 都为同一个 该怎么处理
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值