在webservices返回数据(return the data at webservices)

编程入门 行业动态 更新时间:2024-10-25 12:18:11
在webservices返回数据(return the data at webservices)

我想将字符串传递给我的查询,它能够显示我的所有记录,如果我自己键入'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'

i wanted to pass the string to my query, it able to display all my record if i type in the 'WHERE' part by my self. so i modified it so that user can search the query based on their need... code as below:

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 Message

Error: Cannot implicitly convert type 'string' to '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; }

To return multiple employee objects

[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; }

更多推荐

本文发布于:2023-08-08 00:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466755.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   webservices   data   return

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!