2011-12-20 8 views
0

私はこのsp'Update_driver_Position 'を'temptbl'(varchar型)というパラメータで指定し、spで他のテーブルのデータを'temptbl 'に選択します。私はビジュアルスタジオでspを実行する方法を知っていますが、私は'temptbl 'からデータを選択する方法を知らない(sqldatareaderにtemptblの日付を取得する)。私はどのようにストアドプロシージャを実行し、Visual Studioのテンポラリテーブルから選択できますか?

これは、これはあなたが私が言うに戻った読者からの列のデータを取得する方法に

 using (SqlCommand cmd = new SqlCommand()) 
     { 

      string text = "Update_driver_Position"; 
      cmd.CommandType = CommandType.StoredProcedure; 

      cmd.CommandText = text; 
      SqlParameter retval = cmd.Parameters.Add("@Temptbl", SqlDbType.VarChar); 
      retval.Direction = ParameterDirection.Output; 


      cmd.Connection = this.GetConnection(); 
      cmd.Connection.Open(); 
      cmd.ExecuteNonQuery(); 

      SqlDataReader reader = (cmd.Parameters["@Temptbl"]); 

      while (reader.Read()) 
      { 
       //... 
      } 
     } 

答えて

0

問題が解決しました。 は、ここに私のコードです:

 using (SqlCommand cmd = new SqlCommand()) 
     { 

      string text = string.Format("Exec dbo.Update_Driver_Position '##Temp' Select * From ##Temp"); 
      cmd.CommandType = CommandType.Text; 

      cmd.CommandText = text; 



      cmd.Connection = this.GetConnection(); 
      cmd.Connection.Open(); 


      SqlDataReader reader = cmd.ExecuteReader(); 

      while (reader.Read()) 
      { 
       //... 
      } 
1

を言っているのVisual Studioで私のコードで私はSQL ServerでSPを実行する方法を

Exec dbo.Update_Driver_Position '##Temp' 
Select * From ##Temp 

ですAV

int x = (int)reader["MyField"];

またはどのようにデータを格納します読者? ExecuteReader()に電話する必要があります。ここで SqlDataReader r = cmdSproc.ExecuteReader();

いくつかのサンプルコードです:あなたは、コマンドオブジェクトがExecuteReader()に適用され、読者を割り当てる必要がありますつまり

using (SqlConnection con = new SqlConnection(connString)) 
      { 
       //use sproc 
       SqlCommand cmd = new SqlCommand("selMyProcedure", con); 
       cmd.CommandType = CommandType.StoredProcedure; 
       //some paramters 
       cmd.Parameters.Add("@VendorName", SqlDbType.VarChar).Value = vn; 
       cmd.Parameters.Add("@ProductTypeName", SqlDbType.VarChar).Value = pt; 
       cmd.Parameters.Add("@ProductName", SqlDbType.VarChar).Value = p; 
       con.Open(); 
        //you need to add below 
       SqlDataReader reader = cmd.ExecuteReader(); 


       while (reader.Read()) 
       { 
        int x = (int)reader["MyCol"]; 
       } 
//...more code 

。 そして、読者にデータがあれば、reader["MyColumn"]で読むことができます。

+0

私はどのように私は「## temptbl」cmd.Parameters.Add(「@のVendorName」、SqlDbType.VarChar)とストアドプロシージャの原因にを渡すことができる、ということを知っている私たちは、パラメータではない一時テーブルを追加します。 –

関連する問題