0

I have a code a base that iterates through a list of records and does the follwing

'select * from Table to see if fields exist

then later in the interation
'select * from Table to retreive some data

then farther down in the interation
select field1, field2 from Table to get certain pieces of information

I need to do all of these functions for each record. Would the process speed up if I called the query once for each record and hold the data in a datatable and retreive what I need from there? Or is there another more efficient way to not have to make 3 db calls to the same table which will speed up the process?

1
  • Why don't you just SELECT all the fields you're going to need through the entire iteration at the start and then handle the case if they don't exist? Commented Aug 28, 2014 at 21:49

1 Answer 1

2

You can cache query data to System.Data.DataTable. To simplify things I has written CMyDynaset class, which populates DataTable with data from DB. Here how to use it for example with MySQL:

using System;
using System.Data.Common;
using MySql.Data.MySqlClient;

namesapce MyProg
{
    class Program
    {
        private const string strMyConnection = "Host=localhost;Database=mydb;User Id=myuser;Password=mypsw";

        static void Main(string[] args)
        {
            using (MySqlConnection myConnection = new MySqlConnection(strMyConnection))
            {
                using (MyDb.CMyDynaset dyn = new MyDb.CMyDynaset(myConnection, MySqlClientFactory.Instance))
                {
                    // populate dyn.Table (System.Data.DataTable)
                    dyn.Init("select * from Table");
                    dyn.Load();

                    // access fields
                    foreach (DataColumn column in dyn.Table.Columns)
                    {
                        // ...
                    }

                    // get data
                    long nCountAll = dyn.Table.Rows.Count; // rows count
                    foreach (DataRow row in dyn.Table.Rows)
                    {
                        Object val1 = row[1]; // acess by index
                        Object val2 = row["id"]; // acess by name

                        // ...
                    }

                    // update data
                    dyn.Table.Rows[0]["name"] = "ABC";
                    dyn.Update();
                }
            }
        }
    }
}

CMyDynaset class (CMyDynaset.cs):

// CMyDynaset.cs
using System;
using System.Data.Common;

namespace MyDb
{
    /// <summary>
    /// Summary description for CMyDynaset.
    /// </summary>
    public class CMyDynaset : IDisposable
    {
        public System.Data.DataTable Table = null;
        // private
        private DbConnection myConnection = null;
        private DbProviderFactory myFactory = null;
        private DbDataAdapter dataAdap = null;
        private DbCommandBuilder cmdBld = null;
        private bool bIsSchema = false;

        public CMyDynaset(DbConnection conn, DbProviderFactory factory)
        {
            this.myConnection = conn;
            this.myFactory = factory;
        }

        #region IDisposable Members

        public void Dispose()
        {
            if (this.Table != null)
            {
                this.Table.Dispose();
                this.Table = null;
            }
            if (this.cmdBld != null)
            {
                this.cmdBld.Dispose();
                this.cmdBld = null;
            }
            if (this.dataAdap != null)
            {
                this.dataAdap.Dispose();
                this.dataAdap = null;
            }
            // This object will be cleaned up by the Dispose method.
            // Therefore, you should call GC.SupressFinalize to
            // take this object off the finalization queue
            // and prevent finalization code for this object
            // from executing a second time.
            GC.SuppressFinalize(this);
        }

        #endregion

        // Init
        public void Init(string strSelect)
        {
            DbCommand cmdSel = this.myConnection.CreateCommand();
            cmdSel.CommandText = strSelect;

            this.dataAdap = this.myFactory.CreateDataAdapter();
            this.dataAdap.SelectCommand = cmdSel;
            this.cmdBld = this.myFactory.CreateCommandBuilder();
            this.cmdBld.DataAdapter = this.dataAdap;

            this.Table = new System.Data.DataTable();
            // schema
            this.bIsSchema = false;
        }

        public void AddParameter(string name, object value)
        {
            DbParameter param = this.dataAdap.SelectCommand.CreateParameter();
            param.ParameterName = name;
            param.Value = value;
            this.dataAdap.SelectCommand.Parameters.Add(param);
        }

        public void AddParameter(DbParameter param)
        {
            this.dataAdap.SelectCommand.Parameters.Add(param);
        }

        // Open, Close
        private void Open(ref bool bClose)
        {
            if (this.myConnection.State == System.Data.ConnectionState.Closed)
            {
                this.myConnection.Open();
                bClose = true;
            }
            if (!this.bIsSchema)
            {   // schema
                this.dataAdap.FillSchema(this.Table, System.Data.SchemaType.Mapped);
                this.bIsSchema = true;
            }
        }

        private void Close(bool bClose)
        {
            if (bClose)
                this.myConnection.Close();
        }

        // Load, Update
        public void Load()
        {
            bool bClose = false;
            try
            {
                this.Table.Clear();
                this.Open(ref bClose);
                this.dataAdap.Fill(this.Table);
            }
            catch (System.Exception ex) 
            {
                throw ex;
            }
            finally
            {
                this.Close(bClose);
            }
        }

        public void Update()
        {
            bool bClose = false;
            try
            {
                this.Open(ref bClose);
                this.dataAdap.Update(this.Table);
            }
            catch (System.Exception ex) 
            {
                throw ex;
            }
            finally
            {
                this.Close(bClose);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.