0
class item_Electronic
{

    string eID;
    public string ID
    {
        get { return eID; }
        set { eID = value; }
    }

    string eType;
    public string Type
    {
        get { return eType; }
        set { eType = value; }
    }

    string eManufacturer;
    public string Manufacturer
    {
        get { return eManufacturer; }
        set { eManufacturer = value; }
    }

    string eMake;
    public string Make
    {
        get { return eMake; }
        set { eMake = value; }
    }

    string eModelNo;
    public string ModelNo
    {
        get { return eModelNo; }
        set { eModelNo = value; }
    }

    int ePrice;
    public int Price
    {
        get { return ePrice; }
        set { ePrice = value; }
    }

    int eQuantity;
    public int Quantity
    {
        get { return eQuantity; }
        set { eQuantity = value; }
    }

    //customized Constructor which will enable itx connection to database
    public item_Electronic()
    {
        connectDB();
    }

    //Properties supporting Methods of Object
    OleDbConnection myConn;
    OleDbCommand myComm;
    string queryString;

    public void connectDB()
    {
        myConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\rummykhan\MCS\Spring 2013\Object Oriented Programming\My Apps\MyProject\c Projects\My Class Project\Projects Database\IMSDatabase.accdb");
    }

    public void insert(item_Electronic product)
    {
        try
        {
            queryString = "Insert INTO Electronics (eID,eType,eManufacturer,eMake,eModelNo,ePrice,eQuantity) Values('" + product.ID + "','" + product.Type
                + "','" + product.Manufacturer + "','" + product.Make + "','" + product.ModelNo + "','" + product.Price
                + "','" + product.Quantity + "')";
            myComm = new OleDbCommand(queryString, myConn);
            myConn.Open();
            myComm.ExecuteNonQuery();
            MessageBox.Show("Test");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            if (myConn!=null)
            {
                myConn.Close();

            }
        }

    }
}

私はアプリケーションに取り組んでおり、クラスのすべてのフィールドをカプセル化しました。これは良い習慣なのかどうか疑問に思います。事前に感謝

4

2 に答える 2

1

自動プロパティを使用することをお勧めします。

使用する:

public int MyProperty { get; set; }

それ以外の:

int _MyProperty;
public int MyProperty 
{
    get { return _MyProperty; }
    set { _MyProperty = value; }
}
于 2013-04-21T15:44:06.223 に答える