【问题标题】:WebService only returning first row using a sql queryC#Web 服务仅使用 sql 查询返回第一行#
【发布时间】:2016-08-20 07:06:00
【问题描述】:

我有以下代码

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" ?>

【问题讨论】:

  • 我认为可能是最后一行
  • 嗯,是的,你的方法被声明为返回typVenda,这是一个单一的结果。也许您想返回List&lt;typVenda&gt;? (此时,您应该为循环的每次迭代创建一个新实例。)此外,您应该为 SqlConnection 等使用 using 语句,重命名所有内容以遵循 .NET 命名约定,并使用参数化 SQL。跨度>
  • 您的问题包含太多无关代码。我没有费心去阅读那些不立即可见的东西。

标签: c# sql web-services reader


【解决方案1】:

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

关于改进代码的更多建议:

  1. 在处理SqlConnectionSqlCommand时使用using;因为您不必担心关闭连接和处理命令等。使用将处理这些事情
  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;
}

【讨论】:

  • 效果很好,谢谢:)。输出是否可能是带有我提到的标头的 XML 字符串?
【解决方案2】:
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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 2012-08-30
    • 1970-01-01
    • 2021-08-18
    • 2015-08-03
    相关资源
    最近更新 更多