2012-01-27 7 views
2

私は.NETを使い慣れていますが、少し深いところで泳ぐ必要があります。私は現在、すべてのストアドプロシージャの列を受け取ろうとしている既存の単体テストを拡張することを任されています(db接続とその中のスキーマの完全性をテストする手段として)。目標は、すべてのストアドプロシージャのパラメータを取得し、null値でそれらを実行しようとし、レコードセットが返されないことを確認することです。私はADO.NETでスピードアップしようと壁に向かって頭を打ちのめしていましたが、私の人生ではこれをどうやって行うのか理解できません。これは私が得た限りである(文脈から幾分外されている)。このテストでは、パラメータが渡されていないと文句を言いますが、私は既存のパラメータセットをnullに設定し、単にそれを戻すと考えました。sqlCommand、プログラムでストアドプロシージャのパラメータを取得して設定する

// bind our sql schema gridview 
foreach (SqlSchemaItem item in items) 
    { 
// pull a connection 
using (var sqlConnection = new SqlConnection(connectionString)) 
{ 
    // open connection 
    sqlConnection.Open(); 

    // get schema 
    try 
    { 
     /* 
     * Part 1 - test that the schema is ok... 
     */ 
      var sqlCommand = new SqlCommand(item.ObjectName, sqlConnection); 
      sqlCommand.CommandType = CommandType.StoredProcedure; 
      SqlCommandBuilder.DeriveParameters(sqlCommand); 
      sqlCommand.ExecuteReader(CommandBehavior.SchemaOnly); 

      // success to console 
      Console.WriteLine(item.ObjectName + " is ok."); 

     /* 
     * Part 2 - test that the stored procedure does not return any data. 
     */ 

     // set all the parameters to NULL 
     foreach (SqlParameter parameter in sqlCommand.Parameters) 
     { 
      if (parameter.Direction == ParameterDirection.Input || parameter.Direction == ParameterDirection.InputOutput) 
      { 
       parameter.Value = null; 
       Console.WriteLine("Parameter {0} set to null", parameter.ParameterName, parameter.Value); 
      } 
     } 

     var temp = sqlCommand.ExecuteReader(CommandBehavior.SingleResult); 
     if (temp.HasRows) { 
      Console.WriteLine(string.Format("A record was returned in {0} with value {0}", temp.GetName(0), temp.GetString(0))); 
     } 
     else { 
      Console.WriteLine("No result was returned"); 
      } 
     Console.WriteLine(" "); 
     } 
    catch ... 
    finally ... 
    etc. 
+1

nullの代わりにDbNull.Valueを試しましたか? – Rytmis

+0

よろしくお願いします。 (下記の私の回答を参照してください) – DuncanMack

答えて

2

利用DBNull.Valueの代わりnull

+0

ああ、ビンゴ。私は構文(またはアプローチ)が間違っていると思っていました。私のnull値が実際にはnullでないかもしれないということは私には起こりませんでした。 – DuncanMack

関連する問題