如何填写自定义类型列表<>来自SQL Server

时间:2016-05-09 11:43:13

标签: c# sql-server list class user-defined

我有一个名为Author.cs的类定义为:

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的{​​{1}}文件夹中实现了一个定义为:

的接口
Interfaces.INode.cs

现在我要填写一个列表,即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); } 的最佳重载方法匹配有一些无效的参数。

3 个答案:

答案 0 :(得分:1)

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();
}

答案 1 :(得分:0)

您尝试将int添加到作者列表中。伪代码:

long

_eAthors.Add(new Author(Convert.ToInt32(_myReader_1["Author_ID"])));

无论如何,我会像NHibernate或EntityFramework一样使用ORM框架。这比自己做所有的SQL Mapping要容易得多......

答案 2 :(得分:0)

_eAthorsAuthors的集合,但代码正在尝试添加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.                     
             });