Skip to main content
deleted 4 characters in body; edited tags
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I have a table called tblDelegatestblDelegates 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#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.

  1. 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?
  2. 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?
  3. 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.
  4. 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?
  5. Can anyone tell me if the formatting of my code is correct, or if I should separate out the steps into separate functions perhaps?
  1. 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?
  2. 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?
  3. 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.
  4. 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?
  5. 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

Code to add data to database

Delegates Class

Delegates Class

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 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.

  1. 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?
  2. 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?
  3. 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.
  4. 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?
  5. 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

Delegates Class

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.

  1. 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?
  2. 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?
  3. 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.
  4. 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?
  5. 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

Delegates Class

edited title
Link
user3284707
  • 341
  • 6
  • 11

Adding dataobject to database, improving code efficiency and layout, using linq or sql connection and object

Source Link
user3284707
  • 341
  • 6
  • 11

Adding data to database, improving code efficiency and layout, using linq or sql connection and object

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 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.

  1. 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?
  2. 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?
  3. 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.
  4. 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?
  5. 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;
    }
}