Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
C# 2.0 Return DataReader object
Message
 
 
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 2.0
Miscellaneous
Thread ID:
01537785
Message ID:
01537931
Views:
23
>>>>>Forget that hungarian crap that Michel uses.... program to the interface...
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>using System;
>>>>>using System.Data;
>>>>>using System.Data.Common;
>>>>>using System.Data.OleDb;
>>>>>using System.Data.SqlClient;
>>>>>using Microsoft.Data.Odbc;
>>>>>
>>>>>namespace GenericDataAccessApp
>>>>>{
>>>>>    public class GenericAdoNetComp
>>>>>    {
>>>>>        private IDbConnection idbConn = null;
>>>>>        private IDbDataAdapter idbAdapter = null;
>>>>>        private DbDataAdapter dbAdapter = null;
>>>>>        private IDataReader iReader = null;
>>>>>
>>>>>        public GenericAdoNetComp()
>>>>>        {
>>>>>        }
>>>>>
>>>>>        // GetConnection returns IDbConnection
>>>>>        public IDbConnection GetConnection(int connType,
>>>>>        string connString)
>>>>>        {
>>>>>            switch (connType)
>>>>>            {
>>>>>                case 1: // OleDb Data Provider
>>>>>                    idbConn = new OleDbConnection(connString);
>>>>>                    break;
>>>>>                case 2: // Sql Data Provider
>>>>>                    idbConn = new SqlConnection(connString);
>>>>>                    break;
>>>>>                case 3: // ODBC Data Provider
>>>>>                    idbConn = new OdbcConnection(connString);
>>>>>                    break;
>>>>>                // case 3: // Add your custom data provider
>>>>>                default:
>>>>>                    break;
>>>>>            }
>>>>>            return idbConn;
>>>>>        }
>>>>>
>>>>>        // GetDataAdapter returns IDbDataAdapter
>>>>>        public IDbDataAdapter GetDataAdapter(int connType,
>>>>>        string connString, string sql)
>>>>>        {
>>>>>            switch (connType)
>>>>>            {
>>>>>                case 1: // OleDb Data Provider
>>>>>                    idbAdapter = new OleDbDataAdapter(sql, connString);
>>>>>                    break;
>>>>>                case 2: // Sql Data Provider
>>>>>                    idbAdapter = new SqlDataAdapter(sql, connString);
>>>>>                    break;
>>>>>                case 3: // ODBC Data Provider
>>>>>                    idbAdapter = new OdbcDataAdapter(sql, connString);
>>>>>                    break;
>>>>>                // case 3: // Add your custom data provider
>>>>>                default:
>>>>>                    break;
>>>>>            }
>>>>>            return idbAdapter;
>>>>>        }
>>>>>    }
>>>>>
>>>>>
>>>>>}
>>>>>
>>>>>
>>>>>
>>>>>public class Client
>>>>>{
>>>>>    private void ConnectBtn_Click(object sender, System.EventArgs e)
>>>>>    {
>>>>>        GenericAdoNetComp genDP = new GenericAdoNetComp();
>>>>>        sql = "SELECT * FROM Employees";
>>>>>
>>>>>        if (radioButton1.Checked)
>>>>>        {
>>>>>            connString =
>>>>>            "Provider=Microsoft.Jet.OLEDB.4.0; Data
>>>>>Source=c:\\Northwind.mdb";
>>>>>            conn = genDP.GetConnection(1, connString);
>>>>>            adapter = genDP.GetDataAdapter(1, connString, sql);
>>>>>        }
>>>>>        else if (radioButton2.Checked)
>>>>>        {
>>>>>            connString =
>>>>>            "Data Source=MCB;Initial Catalog=Northwind;user
>>>>>id=sa;password=;";
>>>>>            conn = genDP.GetConnection(2, connString);
>>>>>            adapter = genDP.GetDataAdapter(2, connString, sql);
>>>>>        }
>>>>>        else if (radioButton3.Checked)
>>>>>        {
>>>>>            // Construct your connection string here
>>>>>            conn = genDP.GetConnection(3, connString);
>>>>>            adapter = genDP.GetDataAdapter(3, connString, sql);
>>>>>        }
>>>>>
>>>>>        try
>>>>>        {
>>>>>            conn.Open();
>>>>>            // Fill a DataSet
>>>>>            DataSet ds = new DataSet();
>>>>>            adapter.Fill(ds);
>>>>>            dataGrid1.DataSource = ds.Tables[0].DefaultView;
>>>>>        }
>>>>>        catch (Exception exp)
>>>>>        {
>>>>>            MessageBox.Show(exp.Message);
>>>>>        }
>>>>>        finally
>>>>>        {
>>>>>            conn.Close();
>>>>>        }
>>>>>    }
>>>>>
>>>>>}
>>>>>
>>>>>
>>>>
>>>>Can someone tell me if in the above code (on the Client side) where the DataSet is filled, does the connection "conn" have to be closed? Or the Adapter object after it fills the dataset automatically closes the connection?
>>>
>>>If the connection is open before Fill() is called (as in this example) then it will remain open. If it is *not* open the Fill() will automatically open and close it....
>>
>>Thank you for your reply. Then, following your reply, does it make sense for me to "not" open the connection but rather just call Fill() and open and close connection at once?
>>
>>UPDATE. As soon as I posted the message I understood why the connection may need to be opened prior and closed after. If the Client needs to fill more than one DataSet.
>
>In that case it may make sense to keep it open. But even if you didn't, in practice, the DataAdapter would probably just be retrieving the same connection from the pool so the overhead would be minimal...

Thank you for clarifying. I am trying to modify the data access class that John wrote such that the connection string is "enclosed" within the data access class and not in the Client. That is, to make it as simple as possible.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform