从数据库

时间:2015-04-22 15:54:02

标签: c# sql datagridview

我有以下内容:

 using (SqlCommand cmd2 = new SqlCommand("", dbCon))
                {
                    cmd2.CommandText = @"
                        SELECT [PCMF04_PCBPocketName_N]
                        ,[PCMF01_PcbConnectorName_N]
                        ,[PCMF03_PCBHeaderName_N]
                        ,[PCMF04_KeyIndex_C]
                        ,[PCMF04_SortOrder_R]
                        FROM [ArdSqlDev].[dbo].[WPCMF04_PCBPocket]
                        WHERE 
                            PCMF03_PCBHeaderName_N = @PCMF03_PCBHeaderName_N
                         Order By 
                            PCMF04_PCBPocketName_N";

                    cmd2.Parameters.Add("@PCMF03_PCBHeaderName_N", SqlDbType.NVarChar).Value = header.HeaderName;

                    using (SqlDataReader dr = cmd2.ExecuteReader())
                    {

                        while (dr.Read())
                        {
                            var pocket = new PcbPocket(dr.GetString(0));
                            pocket.PocketName = dr.GetString(0);
                            pocket.PcbConnector.ConnectorName = dr.GetString(1);
                            pocket.KeyIndex = dr.GetString(3);
                            pocket.SortOrder = dr.GetInt32(4);
                            header._PcbHeaderPockets.Add(pocket);
                        }

                        return header;
                    }
                }

我在网上收到错误:

pocket.PcbConnector.ConnectorName = dr.GetString(1);

错误:

  

错误2属性或索引器' PcbConnector.ConnectorName'无法分配 - 它是只读的

口袋类

  public partial class PcbPocket : IEquatable<PcbPocket>, IComparable<PcbPocket>, IComparable
        {
        public PcbPocket(string PocketName)
        {
            _PocketName = PocketName;            
        }

        private string _PocketName = string.Empty;
        public string PocketName
        {
            get { return _PocketName; }
            set { _PocketName = value; }
        }

        private string _KeyIndex = string.Empty;
        public string KeyIndex
        {
            get { return _KeyIndex; }
            set { _KeyIndex = value; }
        }

        private int _SortOrder = 0;
        public int SortOrder
        {
            get { return _SortOrder; }
            set { _SortOrder = value; }
        }

        private PcbConnector _PcbConnector = null;
        public PcbConnector PcbConnector
        {
            get { return _PcbConnector; }
            set { _PcbConnector = value; }
        }

连接器类

public partial class PcbConnector:IEquatable,IComparable,IComparable     {

    private Collections.PcbConnectorPinList _PcbConnectorPins = new Collections.PcbConnectorPinList();
    public Collections.PcbConnectorPinList PcbConnectorPins
    {
        get
        {
            return _PcbConnectorPins;
        }
        set
        {
            _PcbConnectorPins = value;
        }
    }

    public PcbConnector(string connectorName)
    {
        _ConnectorName = connectorName;       
    }

    public PcbConnector(PcbConnector pcbConnector)
    {
        _ConnectorName = pcbConnector.ConnectorName;
        _Height = pcbConnector.Height;
        _Width = pcbConnector.Width;
        _Active = pcbConnector.Active;          
        _Supplier = pcbConnector.Supplier;
    }


    private string _ConnectorName = string.Empty;
    public string ConnectorName
    {
        get { return _ConnectorName; }
    }

    private string _Supplier = string.Empty;
    public string Supplier
    {
        get { return _Supplier; }
        set { _Supplier = value; }
    }


    private float _Height = 0;
    public float Height
    {
        get { return _Height; }
        set { _Height = value; }
    }

    private float _Width = 0;
    public float Width
    {
        get { return _Width; }
        set { _Width = value; }
    }

    private bool _Active = true;
    public bool Active
    {
        get { return _Active; }
        set { _Active = value; }
    }

PcbPocket和PbcConnector是类。 ConnectorName是PbcConnector类的属性。如果我忽略这一行,那么该值将为null,这不是数据库中保存的值,并且会给我一个对象引用而不是设置错误。有没有办法可以检索我的ConnectorName值以显示在我的datagridview中(以不同的文件/形式)?

2 个答案:

答案 0 :(得分:0)

查看PcbConnector课程。像这样改变

class PcbConnector
{
    // Now
    public string ConnectorName {get; private set;}

    // Update this to
    public string ConnectorName {get; set;} // makes the set public
}

答案 1 :(得分:0)

您的PcbConnector上没有设置器:

private string _ConnectorName = string.Empty;
public string ConnectorName
{
    get { return _ConnectorName; }
}

你想要:

private string _ConnectorName = string.Empty;
public string ConnectorName
{
    get { return _ConnectorName; }
    set { _ConnectorName = value; }
}

如上所述,您应该注意,如果您不关心其默认值,则可以自动实施属性。例如,您有:

private int _SortOrder = 0;
public int SortOrder
{
    get { return _SortOrder; }
    set { _SortOrder = value; }
}

这与

相同
public int SortOrder { get; set; }

int的默认值为0