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 ,
}
}
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 ,
}
// 指定查询内的有关 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();
}
}
{
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);
}
}
}
}
{
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);
}
}
}
}