C# 返回webservices上的数据

C# 返回webservices上的数据,c#,.net,web-services,return,C#,.net,Web Services,Return,我想将字符串传递给我的查询,如果我自己输入WHERE部分,它就能够显示我的所有记录。所以我修改了它,用户可以根据自己的需要搜索查询。。。代码如下: using System; using System.Data; using System.Configuration; using System.Collections.Generic; using System.Web; using System.Web.Services; using System.Data.SqlClient; [WebSe

我想将字符串传递给我的查询,如果我自己输入WHERE部分,它就能够显示我的所有记录。所以我修改了它,用户可以根据自己的需要搜索查询。。。代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class EmployeeWebService : System.Web.Services.WebService
{
    List<Employee> list = new List<Employee>();

    public EmployeeWebService ()
    {
        string cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlConnection conn = null;
        SqlDataReader reader = null;
        try
        {
            conn = new SqlConnection(cs);
            //search the data based on the data key in
            string sql = "SELECT * FROM member WHERE userType = '" + ?? + "'";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                list.Add(new Employee { fullName = reader["fullName"].ToString(), password = reader["password"].ToString(), userType= reader["userType"].ToString() });
            }
        }
        catch (Exception e)
        {
            HttpContext.Current.Trace.Warn("Error", "error in getcustomer()", e);
        }
    }

    [WebMethod]
    public Employee GetEmployeeDetails(string userType)
    {
        //i need to return this data back to the query
        string type = userType.ToString();
        return type;
    }
}

返回多个employee对象的步骤

[WebMethod]
public List<Employee> GetEmployeeDetails(string userType)
{
   //search the member table looking for a matching userType value
   string sql = "SELECT * FROM member WHERE userType = '" + ?? + "'";
   SqlCommand cmd = new SqlCommand(sql, conn);
   conn.Open();
   List<Employee> employeeList = new List<Employee>();
   reader = cmd.ExecuteReader();

   while (reader.Read())
   {
      var emp = new Employee(
          { 
              fullName = reader["fullName"].ToString(), 
              password = reader["password"].ToString(), 
              userType = reader["userType"].ToString()
          });

      employeeList.Add(emp);
   }

   reader.close();
   return employeeList;
}
[WebMethod]
公共列表GetEmployeeDetails(字符串用户类型)
{
//搜索成员表以查找匹配的userType值
string sql=“SELECT*FROM member,其中userType=”+??+”;
SqlCommand cmd=新的SqlCommand(sql,conn);
conn.Open();
List employeeList=新列表();
reader=cmd.ExecuteReader();
while(reader.Read())
{
var emp=新员工(
{ 
fullName=读卡器[“fullName”]。ToString(),
password=reader[“password”]。ToString(),
userType=reader[“userType”].ToString()
});
新增员工名单(emp);
}
reader.close();
返回员工名单;
}

userType是string,再次调用它的ToString()。您到底传递给该方法的是什么?您可以在问题中添加GetEmployeeDetails方法调用吗?不要对查询使用参数使用字符串连接。否则:它;这不是一条记录,这是一个记录列表,预期结果将超过1。我使用连接到sql数据库的连接字符串。您可以使用通用列表返回员工列表。函数返回类型将是List Employee GetEmployeeDetails(字符串用户类型),然后返回列表。您还可以使用Employee[]数组返回Employee对象。“List Employee=GetEmployeeDetails(string userType)”,我应该将其应用于何处?请检查我修改后的答案以获取返回的员工列表。您好,谢谢您的更新,但我得到了全名和密码的错误信息。错误:名称“fullName”在当前上下文中不存在
[WebMethod]
public List<Employee> GetEmployeeDetails(string userType)
{
   //search the member table looking for a matching userType value
   string sql = "SELECT * FROM member WHERE userType = '" + ?? + "'";
   SqlCommand cmd = new SqlCommand(sql, conn);
   conn.Open();
   List<Employee> employeeList = new List<Employee>();
   reader = cmd.ExecuteReader();

   while (reader.Read())
   {
      var emp = new Employee(
          { 
              fullName = reader["fullName"].ToString(), 
              password = reader["password"].ToString(), 
              userType = reader["userType"].ToString()
          });

      employeeList.Add(emp);
   }

   reader.close();
   return employeeList;
}