Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Please help me to improve ADO.NET code by keeping the core structure unchanged.

Last post 07-28-2008 6:52 PM by edurazee. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 07-20-2008 10:59 AM

    • edurazee
    • Not Ranked
    • Joined on 12-18-2007
    • Bangladesh
    • New Member
    • Points 15

    Please help me to improve ADO.NET code by keeping the core structure unchanged.

    Normal 0 MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman";}

    This is the code which is intended to be used for connection to, manipulating database content.
    Please try to improve the code.
    Keep the core structural concept unchanged.

    ---------------------------------------------------------------------------
    using
    System;
    using System.Data;
    using System.Data.SqlClient;

    namespace MyDatabaseManupulationNamespace
    {
        // static clss for manipulating Database
        public static class Database
        {
            private static string myConnectionString;
            private static string mySelectQueryString;
            private static string myGeneralQueryString;
            private static string myHost;
            private static string myDatabase;
            private static string myUser;
            private static string myPassword;
            private static string myTableName;
            
            private static SqlConnection myConnection;
            private static SqlCommand myCommand;
            private static SqlDataAdapter myDataAdapter;
            private static DataSet myDataSet;

            private static int rows = 0;

            private static bool successful = true;

            // property for setting new query
            public static string Query
            {
                set
                {
                    myGeneralQueryString = value;
                }
                get
                {
                    return myGeneralQueryString;
                }
            }
           
            // number of affected rows
            public static int AffectedRows
            {
                get
                {
                    return rows;
                }
            }

            // gets the DataSet
            public static DataSet DataSetValue
            {
                get
                {
                    return myDataSet;
                }
            }

            // gets the DataAdapter
            public static SqlDataAdapter DataAdapter
            {
                get
                {
                    return myDataAdapter;
                }
            }

            // static constructor for initializing
            // static attributes

            static Database()
            {
                mySelectQueryString = @"SELECT * FROM ";

                myConnection = new SqlConnection();
                myCommand = new SqlCommand();
                myDataAdapter = new SqlDataAdapter();
                
                myDataSet = new DataSet();
            }

            // Methosd for establishing connection
            public static bool EstablishConnection(string host, string database, string user, string password, string table)
            {
                myHost = host;
                myDatabase = database;
                myUser = user;
                myPassword = password;
                myTableName = table;

                myConnectionString = "Data Source=" + myHost + ";Initial Catalog=" + myDatabase + ";User ID=" + myUser + ";Password=" + myPassword + ";Integrated Security=True";
                
                mySelectQueryString += myTableName;
                
                myGeneralQueryString = mySelectQueryString;

                try
                {
                    myConnection.ConnectionString = myConnectionString;
                    myConnection.Open();

                    myCommand.Connection = myConnection;
                    myCommand.CommandText = myGeneralQueryString;

                    myDataAdapter.SelectCommand = myCommand;

                    rows = myDataAdapter.Fill(myDataSet, myTableName);

                    successful = true;
                }
                catch
                {
                    successful = false;
                    
                    throw new DatabaseException("Connection Failed!");
                }
                finally
                {
                    myConnection.Close();
                }

                return successful;
            }

            // Method for restoring select query
            public static void RestoreDefaultQuery()
            {
                myGeneralQueryString = mySelectQueryString;
                
                Console.WriteLine(myGeneralQueryString);
            }
           
            // Method for executing query

            public static bool ExecuteQuery()
            {
                try
                {
                    myConnection.Open();

                    myCommand.CommandText = myGeneralQueryString;

                    myDataSet.Reset();
                    
                    rows = myDataAdapter.Fill(myDataSet, myTableName);

                    successful = true;
                }
                catch
                {
                    successful = false;
                    
                    throw new DatabaseException("SQL Query Failed!");
                }
                finally
                {
                    myConnection.Close();
                }

                return successful;
            }       
        }
    }

    • Post Points: 10
  • Advertisement

    • Red Gate Software

    Advertisement

    Want to boost your .NET application performance?

    Some developers always seem to write efficient and lightening-fast code. What is their secret? It’s ANTS Profiler. “We improved the performance of the application up to 10 times” Dan Ports, Intrigma.

    Try it for yourself now.

  • 07-26-2008 11:50 AM In reply to

    Re: Please help me to improve ADO.NET code by keeping the core structure unchanged.

     I suggest you better to use some DAL generator for this purpose which uses data readers. That code will much more efficent and you will save palenty of your time

    Share your experiences with others
    Check my Blog...
    • Post Points: 10
  • 07-28-2008 6:52 PM In reply to

    • edurazee
    • Not Ranked
    • Joined on 12-18-2007
    • Bangladesh
    • New Member
    • Points 15

    Re: Please help me to improve ADO.NET code by keeping the core structure unchanged.

    Thanks a lot for the suggestion.

    • Post Points: 5
Page 1 of 1 (3 items)