1. System.Data
The ParameterDirection values are used by the parameter direction properties of OleDbParameter and SqlParameter.
2. ParameterDirection是一个枚举类型,提供了四种参数类型:
usingSystem;
namespaceSystem.Data
{
//Summary:
//SpecifiesthetypeofaparameterwithinaqueryrelativetotheSystem.Data.DataSet.
publicenumParameterDirection
{
//Summary:
//Theparameterisaninputparameter.
Input=1,
//
//Summary:
//Theparameterisanoutputparameter.
Output=2,
//
//Summary:
//Theparameteriscapableofbothinputandoutput.
InputOutput=3,
//
//Summary:
//Theparameterrepresentsareturnvaluefromanoperationsuchasastored
//procedure,built-infunction,oruser-definedfunction.
ReturnValue=6,
}
==
//摘要:
//指定查询内的有关System.Data.DataSet的参数的类型。
publicenumParameterDirection
{
//摘要:
//参数是输入参数。
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 实例:
staticvoidGetSalesByCategory(stringconnectionString, stringcategoryName)
{
using(SqlConnectionconnection=newSqlConnection(connectionString))
{
//Createthecommandandsetitsproperties.
SqlCommandcommand=newSqlCommand();
command.Connection=connection;
command.CommandText="SalesByCategory";
command.CommandType=CommandType.StoredProcedure;
//Addtheinputparameterandsetitsproperties.
SqlParameterparameter=newSqlParameter();
parameter.ParameterName="@CategoryName";
parameter.SqlDbType=SqlDbType.VarChar;
parameter.Direction=ParameterDirection.Input;
parameter.Value=categoryName;
//AddtheparametertotheParameterscollection.
command.Parameters.Add(parameter);
//Opentheconnectionandexecutethereader.
connection.Open();
SqlDataReaderreader=command.ExecuteReader();
if(reader.HasRows)
{
while(reader.Read())
{
Console.WriteLine("{0}:{1:C}",reader[0],reader[1]);
}
}
else
{
Console.WriteLine("Norowsfound.");
}
reader.Close();
}
}
另外需要注意的是在.net中 System.DBNull.Value表示数据库参数为空值 而不是null.
privatestaticvoidAttachParameters(SqlCommandcommand,Sql(巴黎世家是什么梗?2020年的七夕时,Balenciaga依靠“土味广告”成功出圈,迅速在时尚圈掀起了一场流量与话题的狂欢,当时无一不在诟病七夕限定的“土味”。之后又凭借巴黎世家的经典字母袜再次火了起来,短视频平台的播放量一路飙升,让印有 Logo 的黑丝愈发受宠。)Parameter[]commandParameters)
{
if(command==null)thrownewArgumentNullException("command");
if(commandParameters!=null)
{
foreach(SqlParameterpincommandParameters)
{
if(p!=null)
{
//Checkforderivedoutputvaluewithnovalueassigned
if((p.Direction==ParameterDirection.InputOutput||
p.Direction==ParameterDirection.Input)
(p.Value==null))
{
p.Value=DBNull.Value;
}
command.Parameters.Add(p);
}
}
}
}