1

文字列をクエリに渡したかったのですが、自分で「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;
    }
}

エラーメッセージ

Error: Cannot implicitly convert type 'string' to 'Employee'
4

1 に答える 1

0

複数の従業員オブジェクトを返すには

[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;
}
于 2012-05-16T11:39:32.833 に答える