Link to home
Start Free TrialLog in
Avatar of fifo123
fifo123

asked on

Connection objects and Threads ...... please help..

Hello All,
I'm creating a multithreaded C# service application that reads files from
a directory and insert data into multiple SQL Server tables.  I understand that
it's probably a good idea that each thread maintains its own connection object.
The way the code is structured, I have created classes that represent each database
table.  I also have a DB class that handles the database transactions.

For example.....

//Function called by each thread...
public void StartAdding()
{

  Student x = new Student();
  x.StudentID = //value read from the file;
  x.Name = //value read from the file
  x.Insert();

  Teacher Y = new Teacher();
  y.TeacherID = //value read from file;
  y.Name = //Value read from file
  y.Insert
}

public class student
{
   private int iStudentID
   private string sName
   
   public StudentID
   {
     { get.... }
     { set.... }
   }

   public Name
   {
     { get.... }
     { set.... }
   }

   public void Insert()
   {
      ......
      ......
      DB.LoadStudent(_params)
   }
}

public class DB
{
   public static void LoadStudent(SqlParameter[] p)
   {
      //insert into the DB
   }
   public static void LoadTeacher(SqlParameter[] p)
   {
             //insert into the DB
   }
}

Now, since I a thread pool setup, multiple threads are going to be running this code.  Where can
I specify a separate connection string for each thread.  Should I do something something like this inside
the function "StartAdding()", and pass the connection object around...

SqlConnection conn = new SqlConnection("some connection string")
conn.open()

Student x = new Student();
x.StudentID = //value read from the file;
x.Name = //value read from the file
x.Insert(conn);

Somehow, it seems like there could be an easier way to do this...
ASKER CERTIFIED SOLUTION
Avatar of _kiwi_
_kiwi_
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial