web-dev-qa-db-ja.com

SqlCommandのIn-Outパラメーター

SqlCommandには次のパラメーターがあります。ストアドプロシージャのパラメータ値を入力および出力する方法を教えてください。

 SqlCommand mySqlCommand = new SqlCommand("aspInsertZipCode", mySqlConnection);
 mySqlCommand.CommandType = CommandType.StoredProcedure;
 mySqlCommand.Parameters.Add("@DataRows", dataStringToProcess.ToString());
11
LCJ
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;
14
LukeH

SqlParameterにはDirection列挙があります。この値を設定します。

次に、SqlParameterを取る_SqlCommand.Parameters.Add_を使用します。

パラメータの方向:

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

次に、コマンドコレクションからパラメータからExecuteNonQueryを取得することにより、(たとえば)Valueを呼び出した後に値を引き出します。

_myCommand.Parameters["@paramName"].Value_

思い出せませんが、文字列インデクサがあると思います。

または、この1つのライナーがあります:

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

6

SQLコマンドパラメータの属性の1つは、方向です。あなたが使いたいと思うでしょう(メモリを使い果たします)

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