WebService仅使用sql queryC#返回第一行

时间:2016-04-26 12:37:11

标签: c# sql web-services reader

我有以下代码

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();                              
}

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

此外,当我返回它时说&#34;此XML文件似乎没有与之关联的任何样式信息。文档树如下所示。&#34;它应该有这样的标题:

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

2 个答案:

答案 0 :(得分:2)

如果您在阅读器中有n个可用行,那么您可能会获得最后一行,因为创建的对象的属性在while (dr.Read())的每次迭代中都会被写入并最终将最新值返回给调用方法。您应该重新定义您的方法以返回List<typVenda>,从而使用在每次迭代中构造的对象填充列表。最后在迭代结束时返回列表。

更多建议为了改进代码:

  1. 在处理SqlConnectionSqlCommand时使用;因为您无需担心关闭连接和处理命令等使用将照顾这些事情
  2. 无需检查读者是否有行(if (dr.HasRows))如果没有行,则while (dr.Read())将不会执行所附的语句。
  3. 现在考虑以下代码:

    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;
    }
    

答案 1 :(得分: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;
}