Results 1 to 14 of 14

Thread: OleDbConnection being used in different forms, possible?

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Location
    Black Hole
    Posts
    7

    Question OleDbConnection being used in different forms, possible?

    hi all,
    i am developing an application that has a couple of forms. say, if i have a connection to a MS Access database at form2, can i make use of that connection at form3 or do i have to create a new connection? many apologies if this question has been asked many times, as i've got no idea how to go about searching for solutions for this problem.

    thanks,
    ashrobo

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Yes it's possible . In my case , I declared shared variable as oleconnection and call it anywhere BUT you have to close the connection after every use .

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Location
    Black Hole
    Posts
    7
    ah, okay.. i'm currently using untyped datasets, so where do i "close the connection after every use"? do i do it after
    VB Code:
    1. OleDbDataAdapter1.Fill(DataSet1)
    ?

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Location
    Black Hole
    Posts
    7
    i am having trouble setting the oledbconnection as a shared variable. is it advisable to create a oledbconnection for every form?

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by ashrobo
    ah, okay.. i'm currently using untyped datasets, so where do i "close the connection after every use"? do i do it after
    VB Code:
    1. OleDbDataAdapter1.Fill(DataSet1)
    ?
    Well , it depends on the transaction you're doing, for example after you fill your dataset then you can close your connection since the same data was filled from source as you can work offline.

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by ashrobo
    i am having trouble setting the oledbconnection as a shared variable. is it advisable to create a oledbconnection for every form?
    No . Did you read my earlier posts above ? Declare one shared variable as oleconnection in a class file then use it while working in other forms

  7. #7
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    I think a 'Fill' statement of dataadapter goes through an open and close of the corresponding connection. So you may not have to explicitly close your connection if you have not opened it explicitly.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    MS always recommends of closing connections after every use !

  9. #9
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Yes, Close but not OVERCLOSE

    If you check the connection.State after the Fill statement it should be 'Closed'.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  10. #10
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Since DataSets are disconnected, the connection is closed after its filled. But you should use try catch blocks.

    Code:
    try
    {
        myDataAdapter.Fill(myDataSet);
    }
    Catch(Exeception ex)
    {
        MessageBox.Show(ex.Message);
    {
    Finally
    {
        myConn.Close;
    }
    Dont gain the world and lose your soul

  11. #11
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Also, it helps to create a data access class. Then, you can inherit it to make other data classes more specific to what you need. It would also help you later when you want to add data access capabilities to another project, you don't have to keep rewriting the methods.

    Example (in C#. I got most of it out of a good book I have called ASP.NET Website Programming: wrox):
    PHP Code:
    public class DBObject
    {
    protected 
    SqlConnection Connection;
    private 
    string connectionString;

    public 
    DBObject(string newConnectionString)
    {
       
    connectionString newConnectionString;
       
    Connection = new SqlConnection(connectionString);
    }

    protected 
    string ConnectionString
    {
       
    get
       
    {
          return 
    connectionString;
        }
    }

    private 
    SqlCommand BuildIntCommand(string storedProcNameIDataParameter[] parameters)
    {
       
    SqlCommand command BuildQueryCommand(storedProcNameparameters);

       
    command.Parameters.Add(new SqlParameter("ReturnValue"SqlDbType.Int4/* Size */
       
    ParameterDirection.ReturnValuefalse/* is nullable */ 
       
    0/* byte precision */
       
    0/* byte scale */
       
    string.Empty, 
       
    DataRowVersion.Default, null));

       return 
    command;
    }

    private 
    SqlCommand BuildStringCommand(string storedProcNameIDataParameter[] parameters)
    {
       
    SqlCommand command BuildQueryCommand(storedProcNameparameters);

       
    command.Parameters.Add(new SqlParameter("ReturnValue",
          
    SqlDbType.VarChar75
          
    ParameterDirection.ReturnValuefalse,
          
    0,
          
    0,
          
    string.Empty, 
          
    DataRowVersion.Default, null));

       return 
    command;
    }

    private 
    SqlCommand BuildQueryCommand(string storedProcNameIDataParameter[] parameters)
    {
       
    SqlCommand command = new SqlCommand(storedProcNameConnection);
       
    command.CommandType CommandType.StoredProcedure;

       foreach(
    SqlParameter parameter in parameters)
       {
           
    command.Parameters.Add(parameter);
        }
                
        return 
    command;
    }

    private 
    SqlCommand BuildQueryCommand(string storedProcName)
    {
       
    SqlCommand command = new SqlCommand(storedProcNameConnection);
       
    command.CommandType CommandType.StoredProcedure;
       return 
    command;
    }

    protected 
    int RunProcedure(string storedProcNameIDataParameter[] parametersout int rowsAffected)
    {
       
    int result;
       
    Connection.Open();
       
    SqlCommand command BuildIntCommand(storedProcNameparameters);
       
    rowsAffected command.ExecuteNonQuery();
       
    result = (int)command.Parameters["ReturnValue"].Value;
       
    Connection.Close();
       return 
    result;
    }

    protected 
    SqlDataReader RunProcedure(string storedProcNameIDataParameter[] parameters)
    {
       
    SqlDataReader returnReader;
       
    Connection.Open();
       
    SqlCommand command BuildQueryCommand(storedProcNameparameters);
       
    command.CommandType CommandType.StoredProcedure;
       
    returnReader command.ExecuteReader(CommandBehavior.CloseConnection);
       return 
    returnReader;
    }

    protected 
    DataSet RunProcedure(string storedProcNameIDataParameter[] parametersstring tableName)
    {
       
    DataSet dataSet = new DataSet();
       
    Connection.Open();
       
    SqlDataAdapter sqlDA = new SqlDataAdapter();
       
    sqlDA.SelectCommand BuildQueryCommand(storedProcNameparameters);
       
    sqlDA.Fill(dataSettableName);
       
    Connection.Close();
       return 
    dataSet;
    }


  12. #12

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Location
    Black Hole
    Posts
    7
    thanks for the input guys and gals!

    hellswraith: do you happen to have a similar code for VB .NET?

  13. #13
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Here is the code from the VB version of the book. You should find the class in there:
    http://web.wrox.com/download/code/pr...63_code_v7.zip

    It is in the core folder named dbobject.vb

  14. #14

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Location
    Black Hole
    Posts
    7
    thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width