ASP.NETでのストアド・プロシージャの呼び出し方法

6694 ワード

2つの異なるストレージ・プロシージャ呼び出し方法
新しい方法の長所を際立たせるために、まず紹介する.NETでストアド・プロシージャを呼び出す「公式」メソッド.また、本明細書のサンプル・プログラムはすべてSqlServerデータベースで動作しており、他の場合は同様であり、以降は説明しない.本明細書のすべての例はC#言語を採用している.
アプリケーションでデータベースにアクセスするには、まずデータベース接続SqlConnectionを宣言し、次にSQL文とストレージ・プロシージャを実行するためのデータベースコマンドSqlCommandを宣言します.この2つのオブジェクトがあれば、自分のニーズに応じて異なる実行方法で目的を達成することができます.補足する必要があるのは、ページに次の参照文を追加することを忘れないでください:using System.Data.SqlClient.
ストレージ・プロシージャを実行する場合、最初のストレージ・プロシージャが実行されている場合は、1つのDataAdapterで結果を1つのDataSetに埋め込み、データ・グリッド・コントロールを使用して結果をページに表示できます.2番目と3番目のストレージ・プロシージャが実行されている場合は、このプロシージャは必要ありません.特定の戻りに基づいて、オペレーションが正常に完了したかどうかを判定するだけです.
(1)パラメータのないストアド・プロシージャを実行するコードは以下の通りである.
SqlConnection conn
=
new
 SqlConnection(“connectionString”); SqlDataAdapter da 
=
 
new
 SqlDataAdapter(); da.SelectCommand 
=
 
new
 SqlCommand(); da.SelectCommand.Connection 
=
 conn; da.SelectCommand.CommandText 
=
 
"
NameOfProcedure
"
; da.SelectCommand.CommandType 
=
 CommandType.StoredProcedure;
, 。

(2) ( ExeProcedure(string inputdate)):
SqlConnection conn = new  SqlConnection(“connectionString”);
SqlDataAdapter da 
=   new  SqlDataAdapter();
da.SelectCommand 
=   new  SqlCommand();
da.SelectCommand.Connection 
=  conn;
da.SelectCommand.CommandText 
=   " NameOfProcedure " ;
da.SelectCommand.CommandType 
=  CommandType.StoredProcedure;

( , )

param 
=
 
new
 SqlParameter(
"
@ParameterName
"
, SqlDbType.DateTime); param.Direction 
=
 ParameterDirection.Input; param.Value 
=
 Convert.ToDateTime(inputdate); da.SelectCommand.Parameters.Add(param);
これで パラメータが されます. パラメータを する がある は、 の に います.
param 
=
 
new
 SqlParameter(
"
@ParameterName
"
, SqlDbType.DateTime); param.Direction 
=
 ParameterDirection.Output; param.Value 
=
 Convert.ToDateTime(inputdate); da.SelectCommand.Parameters.Add(param);

param  =   new  SqlParameter( " @ParameterName " , SqlDbType.DateTime);
param.Direction 
=  ParameterDirection.ReturnValue;
param.Value 
=  Convert.ToDateTime(inputdate);
da.SelectCommand.Parameters.Add(param);
, , ; , , 。 , ? , SqlServer “ ( )” , , ?

。 。 :
SqlConnection conn = new  SqlConnection(“connectionString”);
SqlDataAdapter da 
=   new  SqlDataAdapter();
da.SelectCommand 
=   new  SqlCommand();
da.SelectCommand.Connection 
=  conn;
da.SelectCommand.CommandText 
=   " NameOfProcedure(’para1’,’para2’,para3) " ;
da.SelectCommand.CommandType 
=  CommandType.StoredProcedure;


, , 。 , !



   , , , , , 。 , , , 。 , , 。 , “ ”。 , 。

 

  :http://www.cnblogs.com/wengyuli/archive/2008/06/05/1214003.html