2016-04-26 18 views
-2

我有以下代码WebService的只是使用SQL queryC#

public class dthvendas: System.Web.Services.WebService { 

public class typVenda 
{ 
    //public string campanha; 
    public string id; 
    public string contact_moment; 
    public string nome; 
    // few more properties 
} 

[WebMethod][SoapDocumentMethod] 

public typVenda getListaVendas(string dt_min, string dt_max) 
{ 
    //venda vendas = new List<venda>();  
    typVenda objVenda = new typVenda();  

    SqlConnection con = new SqlConnection(@"Data Source=server;Initial Catalog=database;User ID=user;password=password");    
    //SqlCommand cmd = new SqlCommand("SELECT * FROM dbo where contact_moment >='" + dt_min + "' AND contact_moment <DATEADD(dd, 1, '" + dt_max + "')", con); 
    SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.vcnosadesoes_getlistavendas", con); 

    con.Open();  
    SqlDataReader dr = cmd.ExecuteReader(); 
    if (dr.HasRows) 
    { 
     while (dr.Read()) 
     { 

      //var objVenda = new typVenda(); 
      //objVenda.campanha = dr["id"].ToString(); 

      objVenda.id = dr["id"].ToString(); 
      objVenda.contact_moment = dr["contact_moment"].ToString(); 
      objVenda.nome = dr["nome"].ToString(); 
      objVenda.pacote = dr["pacote"].ToString(); 
      objVenda.telefone = dr["telefone"].ToString(); 
      objVenda.codigo_wc = dr["codigo_wc"].ToString(); 

      //vendas.Add(objVenda); 
     } 
     dr.Close(); 
    } 
    con.Close();       
    return objVenda; 

    //return vendas.ToArray();        
} 

的问题是,只从表中返回第一行,而不是所有的行返回第一行。什么可能是什么想法的问题?

此外,当我返回它说:“此XML文件似乎没有任何样式信息与它相关联,文档树如下所示。它应该有这样一个标题:

<?xml version="1.0" encoding="UTF‐8" ?> 
+0

这可能是最后一排,我觉得 –

+3

嗯,是的,你的方法被声明为返回'typVenda'这是一个结果。也许你想要返回'List '而不是? (此时,您应该为循环的每次迭代创建一个新实例。)另外,您应该为SqlConnection等使用'using'语句,重命名所有内容以遵循.NET命名约定,并使用参数化SQL。 –

+0

您的问题包含太多无关代码。我没有理会那些看不到的东西。 –

回答

0
public List<typVenda> getListaVendas(string dt_min, string dt_max) 
{ 
    venda vendas = new List<typVenda>();  
    typVenda objVenda = new typVenda();  

    SqlConnection con = new SqlConnection(@"Data Source=server;Initial Catalog=database;User ID=user;password=password");    
    //SqlCommand cmd = new SqlCommand("SELECT * FROM dbo where contact_moment >='" + dt_min + "' AND contact_moment <DATEADD(dd, 1, '" + dt_max + "')", con); 
    SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.vcnosadesoes_getlistavendas", con); 

    con.Open();  
    SqlDataReader dr = cmd.ExecuteReader(); 
    if (dr.HasRows) 
    { 
     while (dr.Read()) 
     { 

      var objVenda = new typVenda(); 
      //objVenda.campanha = dr["id"].ToString(); 

      objVenda.id = dr["id"].ToString(); 
      objVenda.contact_moment = dr["contact_moment"].ToString(); 
      objVenda.nome = dr["nome"].ToString(); 
      objVenda.pacote = dr["pacote"].ToString(); 
      objVenda.telefone = dr["telefone"].ToString(); 
      objVenda.codigo_wc = dr["codigo_wc"].ToString(); 

      vendas.Add(objVenda); 
     } 
     dr.Close(); 
    } 
    con.Close();       

    return vendas; 
} 
2

如果你有读者提供n提取的行,也许你会得到的最后一排,因为创建的对象的属性洁具在中的每个迭代创作的while (dr.Read()),最后将最新值返回给调用方法。您应该重新定义您的方法以返回List<typVenda>,并因此使用每次迭代中构造的对象填充列表。最后在迭代结束时返回列表。

很少有更多的建议,为您提高代码:

  1. 。利用在处理SqlConnectionSqlCommand使用的;因为你不需要担心关闭连接和处理命令等使用将照顾这些东西
  2. 不需要检查读者是否有行(if (dr.HasRows))使用while (dr.Read())不会执行所附的语句,如果没有行。

现在考虑下面的代码:

public List<typVenda> getListaVendas(string dt_min, string dt_max) 
{ 
    List<typVenda> objVendaList = new List<typVenda>(); 

    using (SqlConnection con = new SqlConnection("connection String here")) 
    { 
     using (SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.vcnosadesoes_getlistavendas", con)) 
     { 
      con.Open(); 
      SqlDataReader dr = cmd.ExecuteReader(); 

      while (dr.Read()) 
      { 

       var objVenda = new typVenda(); 

       // Assign the values to the properties here 

       objVendaList.Add(objVenda); 
      } 
      dr.Close(); 
     } 
    } 
    return objVendaList; 
} 
+0

非常感谢:)。输出是否可能是我提到的那个标题的XML字符串? –