In-Out Parameter for SqlCommand

本文关键字:SqlCommand for Parameter In-Out | 更新日期: 2023-09-27 18:03:17

SqlCommand有以下参数。如何在存储过程中同时输入和输出参数值。

 SqlCommand mySqlCommand = new SqlCommand("aspInsertZipCode", mySqlConnection);
 mySqlCommand.CommandType = CommandType.StoredProcedure;
 mySqlCommand.Parameters.Add("@DataRows", dataStringToProcess.ToString());

In-Out Parameter for SqlCommand

var pInOut = mySqlCommand.Parameters.Add("@DataRows", dataStringToProcess.ToString());
pInOut.Direction = ParameterDirection.InputOutput;

然后在执行命令后读取输出值:

// assumes that the parameter is a string and that it could possibly be null
string value = Convert.IsDBNull(pInOut.Value) ? null : (string)pInOut.Value;

SqlParameter有一个Direction枚举。设置该值

则使用占用SqlParameterSqlCommand.Parameters.Add

参数方向:

http://msdn.microsoft.com/en-us/library/system.data.parameterdirection.aspx

然后在调用ExecuteNonQuery(例如)之后,通过从命令集合中获取参数中的Value来提取值:

myCommand.Parameters["@paramName"].Value

不记得了,但我想有一个字符串索引器。

或者,有这样一行:

myCommand.Parameters.AddWithValue("@paramName", value).Direction = ParameterDirection.InputOutput;

SQL命令参数的属性之一是方向。您可能想要使用(离开内存)

SqlCommand mySqlCommand = new SqlCommand("aspInsertZipCode", mySqlConnection);
mySqlCommand.CommandType = CommandType.StoredProcedure;
mySqlCommand.Parameters.Add("@DataRows", dataStringToProcess.ToString());
mySqlCommand.Parameters("@DataRows").Direction = ParameterDirection.InputOutput;
SqlParameter DataRows = new SqlParameter("@DataRows", SqlDbType.Text) 
{ Value = dataStringToProcess.ToString(), Direction = ParameterDirection.InputOutput};
mySqlCommand.Parameters.Add(DataRows);