2009-10-29 184 views
1

總之,我在哪裏可以找到C#/ VB客戶端示例代碼,該代碼使用一些argumnet [如sqlxml數據]調用CLR存儲過程並接收數據讀取器或其他形式的結果?調用CLR存儲過程

而且我怎麼定期收到來自通過SQlContext.Pipe.Send()方法發送的運行CLR存儲過程的信息?

回答

0

我知道我通常創建一個普通的T-SQL存儲過程,它調用我的CLR函數或存儲的特效。然後他們可以像所有其他存儲過程一樣對待。

1
// run a stored procedure that takes a parameter 
    public void RunStoredProcParams() 
    { 
     SqlConnection conn = null; 
     SqlDataReader rdr = null; 

     // typically obtained from user 
     // input, but we take a short cut 
     string custId = "FURIB"; 

     Console.WriteLine("\nCustomer Order History:\n"); 

     try 
     { 
      // create and open a connection object 
      conn = new 
       SqlConnection("Server=(local);DataBase=Northwind;Integrated Security=SSPI"); 
      conn.Open(); 

      // 1. create a command object identifying 
      //  the stored procedure 
      SqlCommand cmd = new SqlCommand(
       "dbo.CustOrderHist", conn); 

      // 2. set the command object so it knows 
      // to execute a stored procedure 
      cmd.CommandType = CommandType.StoredProcedure; 

      // 3. add parameter to command, which 
      // will be passed to the stored procedure 
      cmd.Parameters.Add(
       new SqlParameter("@CustomerID", custId)); 

      // execute the command 
      rdr = cmd.ExecuteReader(); 

      // iterate through results, printing each to console 
      while (rdr.Read()) 
      { 
       Console.WriteLine(
        "Product: {0,-35} Total: {1,2}", 
        rdr["ProductName"], 
        rdr["Total"]); 
      } 
     } 
     finally 
     { 
      if (conn != null) 
      { 
       conn.Close(); 
      } 
      if (rdr != null) 
      { 
       rdr.Close(); 
      } 
     } 
    } 
} 

    enter code here 
+0

感謝。這與我想要的非常接近。仍然有一個問題,我如何定期從存儲過程接收更新?例如,我有一個CLR存儲過程是進入一個循環,並使用「SqlContext.Pipe.Send(%完成)」通知客戶端上的進度。我如何在客戶端收到這些信息? – captonssj 2009-10-30 14:33:53

0
// Create a record object that represents an individual row, including it's metadata. 
     SqlDataRecord record = new SqlDataRecord(new SqlMetaData("stringcol", SqlDbType.NVarChar, 128)); 

     // Populate the record. 
     record.SetSqlString(0,("Hello World!" + System.DateTime.Now)); 

     // Send the record to the client. 
     SqlContext.Pipe.Send(record); 
+1

是的,我知道這一點。我如何收到此infomration在客戶端上,如果我有一個循環內SqlContext.Pipe.Send(記錄)? 我想要一個客戶端示例代碼。 – captonssj 2009-11-02 14:36:34

1
string connectionString = ConfigurationManager.AppSettings["ConnectDB"]; 
     SqlConnection sn = new SqlConnection(connectionString); 
     SqlParameter[] sqlParameters = new SqlParameter[1]; 
     sn.Open(); 
     SqlCommand dCmd = new SqlCommand("dbo.HelloWorld", sn); 
     dCmd.CommandType = CommandType.StoredProcedure; 
     SqlDataReader rdr = null; 
     rdr = dCmd.ExecuteReader(); 
     while (rdr.Read()) 
      { 
      for (int i = 0; i < rdr.FieldCount; i++) 
       Response.Write(rdr[i]); 
      } 
     sn.Close(); 
     } 
+0

@captonssj hav u check this? – 2009-11-07 06:22:06