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

2

我会重写您上面的代码片段,如下所示:

//underscore is not C# convention for class name
public class ItemElectronic 
{
    //Auto properties simplifies usage and create backing field behind the scene
    public String Id { get; set; }
    public String Type { get; set; }
    public String Manufacturer { get; set; }
    public String Make { get; set; }
    public String ModelNo { get; set; }
    public int Price { get; set; }
    public int Quantity { get; set; }

    public ItemElectronic()
    {
       //It's not appropiate to call DB connection in constructor as it could result in exception
       //connectDB(); 
    }

}

// Create another class to provide DataAccess to your entity class 
public class ItemElectronicDataAccess
{
    OleDbConnection _connection;
    OleDbCommand _command;
    string queryString;

    public void ConnectDb()
    {
        _connection = 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(ItemElectronic 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 + "')";
            _command = new OleDbCommand(queryString, _connection);
            _connection.Open();
            _command.ExecuteNonQuery();
            _connection.Close();
            MessageBox.Show("Test");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            if (_connection != null)
            {
                _connection.Dispose();
            }
        }

    }
}
于 2013-04-21T15:49:02.523 回答
1

我建议您使用自动属性,

采用:

public int MyProperty { get; set; }

代替:

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