2017-04-22 69 views
0

我想從Web服務中的SQL Server中檢索多行。當我搜索jo number 1並且我的表名是TestOrderStatus時,我得到2行。在Web服務和xamarin android中從SQL Server檢索多行?

雖然我在搜索結果中只有一行。

感謝所有

public class ReturnOrder 
{ 
    public string Message; 
    public int QtqSlit; 
    public int QtyPcs; 
    public string Design; 
} 

[WebMethod(MessageName = "OrderStatus", Description = "OrderStatus new Order")] 
[System.Xml.Serialization.XmlInclude(typeof(ReturnOrder))] 
public ReturnOrder OrderStatus(string JO) /// get list of notes 
{ 
    int QtqSlit = 0; 
    int QtyPcs = 0; 
    String Design = ""; 
    string Message = ""; 

    SqlDataReader reader; 
    using (SqlConnection connection = new SqlConnection(DBConnection.ConnectionString)) 
    { 
     SqlCommand cmd = new SqlCommand("SELECT QtqSlit,QtyPcs,Design FROM TestOrderStatus where [email protected]"); 
     cmd.CommandType = CommandType.Text; 
     cmd.Connection = connection; 
     cmd.Parameters.AddWithValue("@JO", JO); 

     connection.Open(); 

     reader = cmd.ExecuteReader(); 

     while (reader.Read()) 
     { 
      for (int i = 0; i < reader.FieldCount; i++) 
      { 
       QtqSlit = reader.GetInt32(0); 
       QtyPcs = reader.GetInt32(1); 
       Design = reader.GetString(2); 
      } 
     } 

     if (QtqSlit == 0) 
     { 
      Message = " user name or password is incorrect"; 
     } 

     reader.Close(); 
     connection.Close(); 
    } 

    ReturnOrder rt = new ReturnOrder(); 
    rt.Message = Message; 
    rt.QtqSlit = QtqSlit; 
    rt.QtyPcs = QtyPcs; 
    rt.Design = Design; 

    return rt; 
} 
+0

代碼 公共類ReturnOrder { 公共串消息的其餘部分; public int QtqSlit; public int QtyPcs; 公共字符串設計; } –

+0

首先你不需要循環內部。這將讀取三次當前記錄的三個字段。一個就足夠了。第二個while循環,在第一個循環(第一個記錄)之後,再次運行,替換第一個循環中加載的值(如果有3個記錄,它將再次運行,等等)。所以你最後只有一條記錄,最後一條 – Steve

回答

0

謝謝你現在的web服務很好,但是當他召喚Xamrian的價值時Android並不知道。

公共類Mainlistview:活動 { 保護覆蓋無效的OnCreate(束束) { base.OnCreate(束);

 // Set our view from the "main" layout resource 
     SetContentView(Resource.Layout.Mainlistview); 


     ListView ListView = FindViewById<ListView>(Resource.Id.listView1); 



     Selling.WebServiceDB ws = new Selling.WebServiceDB(); 
     ws.OrderStatusListCompleted += Ws_OrderStatusListCompleted; 
     ws.OrderStatusListAsync(Convert.ToString(1)); 



    } 


    private void Ws_OrderStatusListCompleted(object sender, Selling.OrderStatusListCompletedEventArgs e) 
    { 

     ListView ListView = FindViewById<ListView>(Resource.Id.listView1); 
     string msg = ""; 

     if (e.Result.QtqSlit.ToString().Equals("0")) 
     { 
      msg = e.Result.Message; 
     } 
     else 
     { 

      // full class 
      List<TableItem> tableItems = new List<TableItem>(); 
      tableItems.Add(new TableItem("" + e.Result.QtqSlit, "" + e.Result.QtyPcs, Resource.Drawable.Icon)); 
      ListView.Adapter = new HomeScreenAdapter(this, tableItems); 


     } 

    } 
1

,如果你想不止一個結果你需要多行的列表。所以你的代碼可能是這樣的。

public class ReturnOrder 
{ 
    public string Message; 
    public int QtqSlit; 
    public int QtyPcs; 
    public string Design; 


} 


[WebMethod(MessageName = "OrderStatus", Description = "OrderStatus new Order")] 
[System.Xml.Serialization.XmlInclude(typeof(List<ReturnOrder>))] 
public List<ReturnOrder> OrderStatus(string JO) /// get list of notes 
{ 
    List<ReturnOrder> result=new List<ReturnOrder>(); 
    int QtqSlit = 0; 
    int QtyPcs = 0; 
    String Design = ""; 
    string Message = ""; 

    //try 
    //{ 
     SqlDataReader reader; 
     using (SqlConnection connection = new SqlConnection(DBConnection.ConnectionString)) 
     { 
      SqlCommand cmd = new SqlCommand("SELECT QtqSlit,QtyPcs,Design FROM TestOrderStatus where [email protected]"); 
      cmd.CommandType = CommandType.Text; 
      cmd.Connection = connection; 
      cmd.Parameters.AddWithValue("@JO", JO); 
      connection.Open(); 

      reader = cmd.ExecuteReader(); 
      while (reader.Read()) 
      { 
        QtqSlit = reader.GetInt32(0); 
        QtyPcs = reader.GetInt32(1); 
        Design = reader.GetString(2); 

    ReturnOrder rt = new ReturnOrder(); 
    rt.Message = Message; 
    rt.QtqSlit = QtqSlit; 
    rt.QtyPcs = QtyPcs; 
    rt.Design = Design; 

result.add(rt); 


      } 
      if (QtqSlit == 0) 
      { 
       Message = " user name or password is in correct"; 
      } 
      reader.Close(); 

      connection.Close(); 
     } 

    //} 
    //catch (Exception ex) 
    //{ 
    // Message = " cannot access to the data"; 
    //} 




    return result; 
} 
+0

謝謝你現在web服務很好,但是當他召喚Xamrian的價值時Android並不知道。 –

+0

對不起,對於遲到的回答,但是您是否更改了Xamarian中返回的數據類型列表? – wikiCan

相關問題