我有以下代码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" ?>
这可能是最后一排,我觉得 –
嗯,是的,你的方法被声明为返回'typVenda'这是一个结果。也许你想要返回'List'而不是? (此时,您应该为循环的每次迭代创建一个新实例。)另外,您应该为SqlConnection等使用'using'语句,重命名所有内容以遵循.NET命名约定,并使用参数化SQL。 –
您的问题包含太多无关代码。我没有理会那些看不到的东西。 –