C# 无法序列化接口System.Collections.Generic.IEnumerable
嗨,每个人,我都有一个巨大的问题,我从来没有遇到过,我在网上搜索了更多,但没有 这是我的错误: 无法序列化接口System.Collections.Generic.IEnumerable`1[[Employee,App_Code.6ohe-rkb,Version=0.0.0.0,Culture=neutral,PublicKeyToken=null]] 我的web服务代码是C# 无法序列化接口System.Collections.Generic.IEnumerable,c#,asp.net,kendo-ui,C#,Asp.net,Kendo Ui,嗨,每个人,我都有一个巨大的问题,我从来没有遇到过,我在网上搜索了更多,但没有 这是我的错误: 无法序列化接口System.Collections.Generic.IEnumerable`1[[Employee,App_Code.6ohe-rkb,Version=0.0.0.0,Culture=neutral,PublicKeyToken=null]] 我的web服务代码是 List<Employee> Employees = new List<Employee>
List<Employee> Employees = new List<Employee>();
public EmployeeService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public DataSourceResult Read()
{
SqlDataReader reader = null;
using (SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\alaa\Desktop\GridTest\App_Data\Employees.mdf;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand("Select * from Employee", con))
{
try
{
con.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
Employee e = new Employee();
e.Id = (int)reader["Id"];
e.FirstName = (string)reader["FirstName"];
e.LastName = (string)reader["LastName"];
e.Title = (string)reader["Title"];
e.Salary = (decimal)reader["Salary"];
Employees.Add(e);
}
}
finally
{
if (reader != null)
reader.Close();
}
}
}
return new DataSourceResult
{
Total = Employees.Count, //number of records
Data = Employees //the data
};
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public IEnumerable<Employee> Create(IEnumerable<Employee> newEmployees)
{
string stmt = string.Empty;
StringBuilder ids = new StringBuilder();
SqlDataReader reader = null;
List<Employee> retrievedEmployees = new List<Employee>();
using (SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\alaa\Desktop\GridTest\App_Data\Employees.mdf;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand("InsertEmployee", con))
{
con.Open();
foreach (Employee e in newEmployees)
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FirstName", e.FirstName);
cmd.Parameters.AddWithValue("@LastName", e.LastName);
cmd.Parameters.AddWithValue("@Title", e.Title);
cmd.Parameters.AddWithValue("@Salary", Convert.ToDecimal(e.Salary));
SqlParameter retValue = cmd.Parameters.Add("@NewId", SqlDbType.Int);
retValue.Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
ids.Append(Convert.ToInt16(retValue.Value)).Append(",");
}
//remove the last comma
ids.Remove(ids.Length - 1, 1);
}
using (SqlCommand cmd = new SqlCommand(
"Select * from Employee where id in (" + ids + ")", con))
{
try
{
reader = cmd.ExecuteReader();
while (reader.Read())
{
Employee e = new Employee();
e.Id = (int)reader["Id"];
e.FirstName = (string)reader["FirstName"];
e.LastName = (string)reader["LastName"];
e.Title = (string)reader["Title"];
e.Salary = (decimal)reader["Salary"];
retrievedEmployees.Add(e);
}
}
finally
{
if (reader != null)
reader.Close();
}
}
}
return retrievedEmployees;
}
[WebMethod]
public void Update(IEnumerable<Employee> editEmployees)
{
string stmt = string.Empty;
using (SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\alaa\Desktop\GridTest\App_Data\Employees.mdf;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand(stmt, con))
{
foreach (Employee e in editEmployees)
{
stmt = "update Employee set FirstName = '" + e.FirstName +
"', LastName = '" + e.LastName +
"', Title = '" + e.Title + "', Salary = " +
e.Salary + "where Id = " + e.Id;
con.Open();
cmd.ExecuteNonQuery();
}
}
}
}
[WebMethod]
public void Destroy(IEnumerable<Employee> deleteEmployees)
{
StringBuilder ids = new StringBuilder();
foreach (Employee e in deleteEmployees)
{
ids.Append(e.Id).Append(",");
}
//remove the last comma
ids.Remove(ids.Length - 1, 1);
string stmt = "delete from Employee where id in (" + ids + ")";
using (SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\alaa\Desktop\GridTest\App_Data\Employees.mdf;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand(stmt, con))
{
con.Open();
cmd.ExecuteNonQuery();
}
}
}
}
这是数据源结果代码
using System.Collections.Generic;
/// <summary>
/// Describes the result of Kendo DataSource read operation.
/// </summary>
public class DataSourceResult
{
/// <summary>
/// Represents a single page of processed data.
/// </summary>
public List<Employee> Data { get; set; }
/// <summary>
/// The total number of records available.
/// </summary>
public int Total { get; set; }
}
错误在哪里?如何解决问题?是否存在内部异常?快速检查:如果返回Employee[]或List而不是IEnumerable,是否有效?代码中的哪个web方法会引发该异常?人们问问题总是让我感到惊讶,大概是因为他们需要答案,然后不响应对必要信息的请求。异常发生在公共数据源结果读取方法web服务工作,但在读取时收到错误消息its System.Data.SqlClient.SqlException:对象名称无效&39;雇员&39;。在System.Data.SqlClient.SqlConnection.OnErrorSqlException异常处,布尔断开连接,操作'1 wrapCloseInAction