I have a table called tblDelegates
in my database which I need to populate with some data. I have created a class called Delegates which has various properties to hold the bulk of the information.
I am fairly new to C#, so I wanted to get the answers to a few questions as to why you should be doing things in a particular way and also to get some advice on how I could improve my code.
Please can someone take a look at the questions below and give me a few pointers as to whether this is the correct way to do things.
- Is this the correct way to do this, by first creating an instance of an object and then adding the fields to it before adding the object to the database?
- This will sound like a total noob question (so I apologies), but why bother having to create the delegates class object, why could/should you not just add the fields directly into the database?
- In my Delegates object, should I also add the fields such as
Created_User
Created_Dttm
Updated_User
Updated_Dttm
, or am I ok leaving them out of this object like I have done below. - Is the most efficient/quickest way to do this using LINQ like I have done below, or should I be using a SQL connection, or is it just preference?
- Can anyone tell me if the formatting of my code is correct, or if I should separate out the steps into separate functions perhaps?
Code to add data to database
var del = new Delegates
{
Pending = true,
ManagerAlias = row.Cells[4].Text,
ManagerName = row.Cells[1].Text + " " + row.Cells[2].Text,
DelegateAlias = ADLookup.AD("alias"),
DelegateName = ADLookup.AD("GivenName") + ADLookup.AD("sn"),
DelegateEmail = ADLookup.AD("mail")
};
var dc = new LinqToSqlDataContext();
var Del = new tblDelegate
{
Pending = del.Pending,
Manager_Alias = del.ManagerAlias,
Manager_Name = del.ManagerName,
Delegate_Alias = del.ManagerAlias,
Delegate_Name = del.DelegateName,
Delegate_Email = del.DelegateEmail,
Created_User = ADLookup.ADCurrentUser(),
Created_Dttm = DateTime.Now,
Updated_User = ADLookup.ADCurrentUser(),
Updated_Dttm = DateTime.Now
};
dc.tblDelegates.InsertOnSubmit(Del);
dc.SubmitChanges();
Delegates Class
public class Delegates
{
public bool Pending { get; set; }
public string ManagerAlias {get;set;}
public string ManagerName {get;set;}
public string DelegateAlias {get;set;}
public string DelegateName {get;set;}
public string DelegateEmail {get;set;}
public Delegates()
{
}
public Delegates(bool pending, string manageralias, string managername, string delegatealias, string delegatename, string delegateemail)
{
Pending = pending;
ManagerAlias = manageralias;
ManagerName = managername;
DelegateAlias = delegatealias;
DelegateName = delegatename;
DelegateEmail = delegateemail;
}
}