如何从 SQL 服务器填充自定义类型列表<>
How to fill a custom typed List<> from SQL Server
我有一个名为 Author.cs
的 class 定义为:
public class Author : Interfaces.INode
{
private List<INode> _targetList;
private List<IAttribute> _attributeObject;
// Author class Constructor
public Author()
{
_targetList = new List<INode>();
}
//implementazion of _TargetObject INode method
public List<INode> _TargetObject
{
get
{
return _targetList;
}
}
//implementazion of _AttributeObject INode method
public List<IAttribute> _AttributeObject
{
get
{
return _attributeObject;
}
}
public int _aID { get; set; }
public string _aName { get; set; }
// 'CoAuthor', 'Venue' and 'Paper' are classes that
// implements an interface i.e. `IAttribute`
public List<CoAuthor> _aCoAuthors { get; set; }
public List<Venue> _aVenue { get; set; }
public List<Paper> _aPapers { get; set; }
public string _aArea { get; set; }
}
它在名为 Interfaces.INode.cs
的 Interfaces
文件夹中实现了一个接口,定义为:
public interface INode
{
List<INode> _TargetObject { get; }
List<IAttribute> _AttributeObject { get; }
}
public interface IAttribute : INode
{}
现在我想填写一个列表,即 List<Author>
即在另一个名为 AuthorCollector.cs
的 class 中
List<Author> _eAthors = new List<Author>();
我试过:
try
{
SqlCommand _myCommand_1 = _con.CreateCommand();
_myCommand_1.CommandText = @"SELECT Author_ID FROM M_DataFull
ORDER BY Author_ID, Year";
var _AuthID = 0;
int _Row_Counter = 0;
using (SqlDataReader _myReader_1 = _myCommand_1.ExecuteReader())
{
while (_myReader_1.Read())
{
_Row_Counter++;
_eAthors.Add(Convert.ToInt32(_myReader_1["Author_ID"]));
}
_myReader_1.Close();
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
错误是:
The best overloaded method match for _eAuthors.Add()
has some invalid arguments.
您尝试将一个整数添加到作者列表中。伪代码:
_eAthors.Add(new Author(Convert.ToInt32(_myReader_1["Author_ID"])));
或
_eAthors.Add(new Author(){_aID =Convert.ToInt32(_myReader_1["Author_ID"]}));
无论如何,我会为此使用 ORM 框架,例如 NHibernate 或 EntityFramework。这比自己做所有 SQL 映射要容易得多...
using (SqlDataReader _myReader_1 = _myCommand_1.ExecuteReader())
{
while (_myReader_1.Read())
{
_Row_Counter++;
Author author = new Author();
author._aId = Convert.ToInt32(_myReader_1["Author_ID"]);
author._aName = Convert.ToString(_myReader_1["Author_Name"]);
//etc...
_eAthors.Add(author);
}
_myReader_1.Close();
}
_eAthors
是 Authors
的集合,但代码试图添加一个 int
值,这在这种情况下会导致错误。
修改这一行
_eAthors.Add(Convert.ToInt32(_myReader_1["Author_ID"]))
到
_eAthors.Add(new Author
{
_aid= Convert.ToInt32(_myReader_1["Author_ID"]),
// add additional properties if you have one.
});
我有一个名为 Author.cs
的 class 定义为:
public class Author : Interfaces.INode
{
private List<INode> _targetList;
private List<IAttribute> _attributeObject;
// Author class Constructor
public Author()
{
_targetList = new List<INode>();
}
//implementazion of _TargetObject INode method
public List<INode> _TargetObject
{
get
{
return _targetList;
}
}
//implementazion of _AttributeObject INode method
public List<IAttribute> _AttributeObject
{
get
{
return _attributeObject;
}
}
public int _aID { get; set; }
public string _aName { get; set; }
// 'CoAuthor', 'Venue' and 'Paper' are classes that
// implements an interface i.e. `IAttribute`
public List<CoAuthor> _aCoAuthors { get; set; }
public List<Venue> _aVenue { get; set; }
public List<Paper> _aPapers { get; set; }
public string _aArea { get; set; }
}
它在名为 Interfaces.INode.cs
的 Interfaces
文件夹中实现了一个接口,定义为:
public interface INode
{
List<INode> _TargetObject { get; }
List<IAttribute> _AttributeObject { get; }
}
public interface IAttribute : INode
{}
现在我想填写一个列表,即 List<Author>
即在另一个名为 AuthorCollector.cs
List<Author> _eAthors = new List<Author>();
我试过:
try
{
SqlCommand _myCommand_1 = _con.CreateCommand();
_myCommand_1.CommandText = @"SELECT Author_ID FROM M_DataFull
ORDER BY Author_ID, Year";
var _AuthID = 0;
int _Row_Counter = 0;
using (SqlDataReader _myReader_1 = _myCommand_1.ExecuteReader())
{
while (_myReader_1.Read())
{
_Row_Counter++;
_eAthors.Add(Convert.ToInt32(_myReader_1["Author_ID"]));
}
_myReader_1.Close();
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
错误是:
The best overloaded method match for
_eAuthors.Add()
has some invalid arguments.
您尝试将一个整数添加到作者列表中。伪代码:
_eAthors.Add(new Author(Convert.ToInt32(_myReader_1["Author_ID"])));
或
_eAthors.Add(new Author(){_aID =Convert.ToInt32(_myReader_1["Author_ID"]}));
无论如何,我会为此使用 ORM 框架,例如 NHibernate 或 EntityFramework。这比自己做所有 SQL 映射要容易得多...
using (SqlDataReader _myReader_1 = _myCommand_1.ExecuteReader())
{
while (_myReader_1.Read())
{
_Row_Counter++;
Author author = new Author();
author._aId = Convert.ToInt32(_myReader_1["Author_ID"]);
author._aName = Convert.ToString(_myReader_1["Author_Name"]);
//etc...
_eAthors.Add(author);
}
_myReader_1.Close();
}
_eAthors
是 Authors
的集合,但代码试图添加一个 int
值,这在这种情况下会导致错误。
修改这一行
_eAthors.Add(Convert.ToInt32(_myReader_1["Author_ID"]))
到
_eAthors.Add(new Author
{
_aid= Convert.ToInt32(_myReader_1["Author_ID"]),
// add additional properties if you have one.
});