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();
}
}
}
}
我正在处理应用程序并且我在课堂上封装了我的每个领域,我想这是一个好的做法。 Thanx In Advance
答案 0 :(得分: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();
}
}
}
}
答案 1 :(得分:1)
我建议您使用自动属性
使用:
public int MyProperty { get; set; }
而不是:
int _MyProperty;
public int MyProperty
{
get { return _MyProperty; }
set { _MyProperty = value; }
}