Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WebService仅使用sql查询返回第一行_C#_Sql_Web Services_Reader - Fatal编程技术网

C# WebService仅使用sql查询返回第一行

C# WebService仅使用sql查询返回第一行,c#,sql,web-services,reader,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][SoapDocu

我有以下代码

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" ?>
如果在读卡器中有n个可用的抓取行,则可能会得到最后一行,因为创建的对象的属性在while dr.Read的每次迭代中都会被重写,并最终将最新的值返回给调用方法。您应该重新定义返回列表的方法,从而使用在每次迭代中构造的对象填充列表。最后在迭代结束时返回列表

对于改进代码,您还有几点建议:

在处理SqlConnection和SqlCommand时使用using;由于您无需担心紧密连接和处理命令等问题,因此使用将解决这些问题 如果dr.HasRows使用,则无需检查读取器是否有行;如果没有行,则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;
}

它可能是最后一行,我想是的,您的方法被声明为返回typVenda,这是一个单一的结果。也许您想返回列表?此时,您应该为循环的每个迭代创建一个新实例。此外,您应该为您的SqlConnection等使用语句,重命名所有内容以遵循.NET命名约定,并使用参数化SQL。您的问题包含太多不相关的代码。我没有费心去读那些看不到的东西。它非常有效。谢谢:。输出是否可能是带有我提到的标题的XML字符串?
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;
}