• System.Data : 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 == nullthrow 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);
                        }
                    }
                }
            }
  • 相关阅读:
    从12306.cn谈大网站架构与性能优化
    新浪微博的存储思路整理架构分享--微博架构的回顾
    多吃以上食物可以调理内分泌
    脸部护理
    美容实用小知识
    如何把网页或html内容生成图片
    互联网阅读与知识积累流程化实践分享
    怎样与人沟通?
    如何控制情绪
    如何去掉Google搜索的跳转 让你的Google搜索不被reset掉
  • 原文地址:https://www.cnblogs.com/Dlonghow/p/1336406.html
Copyright © 2020-2023  润新知