尝试将对象添加到列表时获取 NotSupportedException
Get a NotSupportedException when trying to add an object to a List
我试图从中提取数据的 SQL 服务器中的 table 具有以下列:
名字 |性别 |工资 |部门
在下面的代码中,我要做的就是从 table 中提取员工信息,根据该信息创建一个员工对象,然后将该对象添加到员工列表中。
这是代码:
namespace LINQQueriesPart2
{
class Program
{
List<Employee> whatever = EmployeeDB.getEmployees();
IEnumerable<Employee> query = from y in whatever
where y.name[0] == 'M'
select y;
foreach (Employee x in query)
{
Console.WriteLine(x.name);
}
}
}
namespace ConnectingToSql
{
public static class EmployeeDB
{
public static List<Employee> getEmployees()
{
List<Employee> returnList = new List<Employee>();
String connectionString = "server=DESKTOP-T5KDKOP;"
+ "database=MCP_Exam_70-483_Prep;"
+ "Trusted_Connection=yes";
if (SQLConnectingTools.CheckConnection(connectionString))
{
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand command = new SqlCommand("SELECT * FROM employees", connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
returnList.Add(
new Employee
{
name = reader.GetString(0),
gender = reader.GetChar(1),
salary = reader.GetInt16(2),
department = reader.GetString(3)
}
);
}
reader.Close();
connection.Close();
}
else
{
Console.WriteLine("SQL connection is not successful.");
}
return returnList;
}
}
public class Employee
{
public String name { get; set; }
public char gender { get; set; }
public int salary { get; set; }
public String department { get; set; }
public Employee() { }
}
}
当我 运行 在调试模式下(在 Visual Studio 中)上面的代码时,代码中断并且以下代码突出显示为黄色:
returnList.Add(
new Employee
{
name = reader.GetString(0),
gender = reader.GetChar(1),
salary = reader.GetInt16(2),
department = reader.GetString(3)
}
);'
首先你应该检查结果中是否存在任何行。
使用此代码,您可以检查:
if (reader.HasRows)
如果 employee table 的设计与您的 Employee 对象不同并且您尝试了无效的强制转换,或者您的结果中没有数据,您可能会得到 System.NotSupportedException 或 System.InvalidCastException会得到 System.NotSupportedException.
我对你的代码做了一些改进,这应该可以工作:
using (SqlConnection cn = new SqlConnection(connectionString))
{
string commandText = "SELECT * FROM employees";
using (SqlCommand cmd = new SqlCommand(commandText, cn))
{
cmd.CommandText = commandText;
cn.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
var employeeColumns = new object[4];
var colCount = reader.GetSqlValues(employeeColumns);
returnList.Add(
new Employee
{
name = employeeColumns[0].ToString(),
gender = employeeColumns[1].ToString()[0],
salary = Convert.ToInt16(employeeColumns[2].ToString()),
department = employeeColumns[3].ToString()
}
);
}
}
}
}
只需将上面的代码复制到您的 if 块(此行之后):
if (SQLConnectingTools.CheckConnection(connectionString))
我试图从中提取数据的 SQL 服务器中的 table 具有以下列:
名字 |性别 |工资 |部门
在下面的代码中,我要做的就是从 table 中提取员工信息,根据该信息创建一个员工对象,然后将该对象添加到员工列表中。 这是代码:
namespace LINQQueriesPart2
{
class Program
{
List<Employee> whatever = EmployeeDB.getEmployees();
IEnumerable<Employee> query = from y in whatever
where y.name[0] == 'M'
select y;
foreach (Employee x in query)
{
Console.WriteLine(x.name);
}
}
}
namespace ConnectingToSql
{
public static class EmployeeDB
{
public static List<Employee> getEmployees()
{
List<Employee> returnList = new List<Employee>();
String connectionString = "server=DESKTOP-T5KDKOP;"
+ "database=MCP_Exam_70-483_Prep;"
+ "Trusted_Connection=yes";
if (SQLConnectingTools.CheckConnection(connectionString))
{
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand command = new SqlCommand("SELECT * FROM employees", connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
returnList.Add(
new Employee
{
name = reader.GetString(0),
gender = reader.GetChar(1),
salary = reader.GetInt16(2),
department = reader.GetString(3)
}
);
}
reader.Close();
connection.Close();
}
else
{
Console.WriteLine("SQL connection is not successful.");
}
return returnList;
}
}
public class Employee
{
public String name { get; set; }
public char gender { get; set; }
public int salary { get; set; }
public String department { get; set; }
public Employee() { }
}
}
当我 运行 在调试模式下(在 Visual Studio 中)上面的代码时,代码中断并且以下代码突出显示为黄色:
returnList.Add(
new Employee
{
name = reader.GetString(0),
gender = reader.GetChar(1),
salary = reader.GetInt16(2),
department = reader.GetString(3)
}
);'
首先你应该检查结果中是否存在任何行。 使用此代码,您可以检查:
if (reader.HasRows)
如果 employee table 的设计与您的 Employee 对象不同并且您尝试了无效的强制转换,或者您的结果中没有数据,您可能会得到 System.NotSupportedException 或 System.InvalidCastException会得到 System.NotSupportedException.
我对你的代码做了一些改进,这应该可以工作:
using (SqlConnection cn = new SqlConnection(connectionString))
{
string commandText = "SELECT * FROM employees";
using (SqlCommand cmd = new SqlCommand(commandText, cn))
{
cmd.CommandText = commandText;
cn.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
var employeeColumns = new object[4];
var colCount = reader.GetSqlValues(employeeColumns);
returnList.Add(
new Employee
{
name = employeeColumns[0].ToString(),
gender = employeeColumns[1].ToString()[0],
salary = Convert.ToInt16(employeeColumns[2].ToString()),
department = employeeColumns[3].ToString()
}
);
}
}
}
}
只需将上面的代码复制到您的 if 块(此行之后):
if (SQLConnectingTools.CheckConnection(connectionString))