2011-04-06 49 views
0

我在ASP.NET中有一個簡單的web服務,但web方法需要5個參數。有沒有其他方式來傳遞參數?像數組或列表?我想限制爲3.將很多參數傳遞給Web服務

我試過SetMethod(字符串參數)和dint的工作。

謝謝。

+2

咦?你的問題沒有意義。 – SLaks 2011-04-06 14:21:16

+0

五個參數並不多。 – 2011-04-06 14:22:19

回答

6

爲什麼不創建一個具有這些參數的類作爲它的屬性,那麼您就可以將該類的一個實例作爲參數傳遞給Web方法。

改變這樣的事情..

[WebMethod] 
public void SomeMethod(string param1, string param2, string param3) 
{ 
    //some code 
} 

到這樣的事情...

[WebMethod] 
public void SomeMethod(SomeClass myClass) 
{ 
    //some code 
} 

public class SomeClass 
{ 
    public string Param1 { get; set; } 
    public string Param2 { get; set; } 
    public string Param3 { get; set; } 
} 

,你會使用它是這樣的...

SomeClass myClass = new SomeClass(); 
myClass.Param1 = "some data"; 
myClass.Param2 = "more data"; 
myClass.Param3 = "even more"; 

// make the webservice call 
someObject.SomeMethod(myClass); 
0
List<Student> list = new List<Student>(); 
    [WebMethod] 
    public string InsertUserDetails(Student userInfo) 
    { 
     list.Add(userInfo); 
     //string Message; 
     SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Sample;Integrated Security=True"); 
     con.Open(); 
     foreach (Student s in list) 
     { 
      SqlCommand cmd = new SqlCommand("insert into userdetail(username,password,country,email) values(@UserName,@Password,@Country,@Email)", con); 
      cmd.Parameters.AddWithValue("@UserName", userInfo.UserName); 
      cmd.Parameters.AddWithValue("@Password", userInfo.Password); 
      cmd.Parameters.AddWithValue("@Country", userInfo.Country); 
      cmd.Parameters.AddWithValue("@Email", userInfo.Email); 

      int result = cmd.ExecuteNonQuery(); 
     } 

      //if (result == 1) 
      //{ 
      // Message = userInfo.UserName + " Details inserted successfully"; 
      //} 
      //else 
      //{ 
      // Message = userInfo.UserName + " Details not inserted successfully"; 
      //} 
      //con.Close(); 

      // return Message; 
     return "done"; 

    } 

你可以這樣做>>>>>>>>>>>>>

+0

請提供一些關於您的解決方案的更多描述 – abarisone 2015-03-19 12:35:54

+1

我想您正在回答其他一些問題。 OP詢問[tag:web-services],然後回答[tag:sql]。 – SiKing 2015-03-19 22:06:21