2013-03-12 27 views

回答

1

將調用存儲在一個方法中,並從您想調用它的任何位置調用此方法。將參數(字符串,整數,雙精度...)傳遞給此方法,並將這些值存儲在存儲過程的參數中。

這樣你就可以將所有的SP代碼保存在一個地方。

public class CustomerProvider 
{ 
    public int UpdateCustomer(int id, string name, string address) 
    { 
     using(connection = new 
      SqlConnection("Server=localhost;DataBase=Northwind;Integrated Security=SSPI")) 
     { 
      connection.Open(); 

      var command = new SQLCommand("csp_updatecustomer", connection); 
      command.CommandType = CommandType.StoredProcedure; 

      command.Parameters.Add(
      new SqlParameter("@CustomerID", id)); 
      command.Parameters.Add(
       new SqlParameter("@CustomerName", name)); 
      command.Parameters.Add(
       new SqlParameter("@CustomerAddress", address)); 
      var result = command.ExecuteNonQuery(); 
      return result; 
     } 
    } 
} 
+0

謝謝你,因爲即時通訊在C#中的新手,這將有助於如果你能給我如何做到這一點一些示例代碼.. – user2115640 2013-03-12 11:34:47

+0

我添加了一個例子。注意我沒有添加足夠的異常處理。但是這應該給你一個想法。 – 2013-03-12 11:47:55

+0

非常感謝 – user2115640 2013-03-12 12:00:43

相關問題