private void SaveNewLogsheet01Record()
{
try
{
Logsheet01 Header = new Logsheet01();
Header.DrNO = drNO.Text;
Header.DocDate = dtPicker.Value;
Header.RecNum = RecNum.Value;
Header.DocuTitle = DocumentHeader.Text;
SaveRec01(Header);
}
catch (Exception) { }
}
private void SaveNewLogsheet02Record()
{
try
{
Logsheet02 Details = new Logsheet02();
Details.RecNum = RecNum.Value;
Details.DataFile01 = GlobalVar.DataRec01;
Details.DataFile02 = GlobalVar.DataRec02;
Details.DataFile03 = GlobalVar.DataRec03;
Details.UserName = GlobalVar.UserInfo;
SaveRec02(Details);
}
catch (Exception) { }
}
private void SaveRec01(Logsheet01 Header) <-- is this necessary in c#?
{
try
{
using (DBDataContext DB = new DBDataContext())
{
DB.Delivery_HeaderRECs.InsertOnSubmit(Header);
DB.SubmitChanges();
DB.Connection.Close();
}
}
catch (Exception) { }
}
private void SaveRec02(Logsheet02 Header)
{
try
{
using (DBDataContext DB = new DBDataContext())
{
DB.Logsheet0.InsertOnSubmit(Header);
DB.SubmitChanges();
DB.Connection.Close();
}
}
catch (Exception) { }
}
i just want to find a new way on how to insert a record on database make my codes cleaner and shorter now if i have a form with multiple insert on tables ex.: Tables like "Logsheet01" and "Logsheet02" , "Logsheet03" and i want my codes to be shorten. is there a way i can put in the saveRec01 Function into one function for 3 tables?
is there a way i could make like this:
private void SaveRec01(Logsheet01 Header)
{
if(Saving == "Logsheet01") {
using(DBDatacontex DB = new DBDatacontex) {
DB.Logsheet01.InsertOnSubmit(Header);
DB.SubmitChanges();
DB.Connection.Close();
}
}elseif (Saving == "Logsheet02") {
using(DBDatacontex DB = new DBDatacontex) {
DB.Logsheet02.InsertOnSubmit(Header);
DB.SubmitChanges();
DB.Connection.Close();
}
} //etc..
}
or maybe a new lambda expression of insert a record list?
You can write:
Logsheet01 Header = new Logsheet01({
DrNO = drNO.Text,
DocDate = dtPicker.Value,
RecNum = RecNum.Value,
DocuTitle = DocumentHeader.Text
});
Header.SaveRecord();
Create a function like this one:
private void SaveRecord()
{
using(DBDatacontex DB = new DBDatacontex) {
//I used this because the SaveRecord function is in the same class as the object used to create the record
DB.Delivery_HeaderRECs.InsertOnSubmit(this);
DB.SubmitChanges(); }
}
}
If you wish to get rid of the repetitive code and only specify the "meat" of the database operation each time, you can move all your boilerplate code into a method which accepts an Action
that defines the variable component of your operation like so:
// Wrapper for our database operation.
private void PerformDbOperationAndSubmit(Action<DBDataContext> action)
{
using (DBDataContext DB = new DBDataContext())
{
// Invoke our arbitrary action over the data context.
action(DB);
DB.SubmitChanges();
DB.Connection.Close();
}
}
// Object creation (modified for the sake of brevity).
private void SaveNewLogsheet01Record()
{
Logsheet01 Header = new Logsheet01();
// Fill Header properties here.
PerformDbOperationAndSubmit(dx => dx.Delivery_HeaderRECs.InsertOnSubmit(Header));
}
private void SaveNewLogsheet02Record()
{
Logsheet02 Details = new Logsheet02();
// Fill Details properties here.
PerformDbOperationAndSubmit(dx => dx.Logsheet0.InsertOnSubmit(Details));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.