ParameterDirection参数类型

 

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);
                    }
                }
            }
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值