Click here to Skip to main content
15,884,099 members
Articles / Database Development / SQL Server / SQL Server 2008

A simple example of SQL server admin tools.

Rate me:
Please Sign up or sign in to vote.
4.90/5 (43 votes)
13 Sep 2009CPOL8 min read 109.4K   5K   115  
A simple example of SQL server admin tools.
using System;
using System.Data;
using System.Data.SqlClient;
using DAL.Interface;

namespace DAL
{
    public class ClsSQLHelper : IFacade
    {
        SqlConnection ObjSqlConnection;
        SqlCommand ObjSqlCommand;
        SqlDataAdapter ObjSqlDataAdapter;
        DataSet ObjDataSet;

        #region "Public functions"

        public DataSet GetRecordSet(string strConnectionString
                                  , string strSQL)
        {

            ObjSqlConnection = new SqlConnection(strConnectionString.Trim());

            ObjDataSet = new DataSet();

            try
            {
                ObjSqlCommand = new SqlCommand(strSQL, ObjSqlConnection);
                ObjSqlDataAdapter = new SqlDataAdapter();
                ObjSqlDataAdapter.SelectCommand = ObjSqlCommand;
                ObjSqlDataAdapter.Fill(ObjDataSet);

            }
            catch (Exception ex)
            {
                //MessageBox.Show ( ex.Message);  
            }
            finally
            {
                ObjSqlDataAdapter = null;
                ObjSqlCommand = null;
                ObjSqlConnection = null;
                strSQL = null;
            }
            return ObjDataSet;
        }

        public string GetScript(string strConnectionString
                              , string strObject
                              , int ObjType)
        {
            string strScript = null;
            int intCounter = 0;
            if (ObjType != 0)
            {
                ObjSqlConnection = new SqlConnection(strConnectionString.Trim());

                try
                {
                    ObjDataSet = new DataSet();
                    ObjSqlCommand = new SqlCommand("exec sp_helptext [" + strObject + "]", ObjSqlConnection);
                    ObjSqlDataAdapter = new SqlDataAdapter();
                    ObjSqlDataAdapter.SelectCommand = ObjSqlCommand;
                    ObjSqlDataAdapter.Fill(ObjDataSet);

                    foreach (DataRow ObjDataRow in ObjDataSet.Tables[0].Rows)
                    {
                        strScript += Convert.ToString(ObjDataSet.Tables[0].Rows[intCounter][0]);
                        intCounter++;
                    }


                }
                catch (Exception ex)
                {
                    strScript = ex.Message.ToString();
                }
                finally
                {
                    ObjSqlDataAdapter = null;
                    ObjSqlCommand = null;
                    ObjSqlConnection = null;
                }
            }

            return strScript;

        }

        #endregion

        public bool ExamineConnection(string strConnectionString)
        {
            bool Result = false;

            try
            {

                ObjSqlConnection = new SqlConnection(strConnectionString.Trim());
                ObjSqlConnection.Open();

                if (ObjSqlConnection.State == ConnectionState.Open)
                {
                    ObjSqlConnection.Close();
                    Result = true;
                }
            }
            catch
            {
                Result = false;
            }


            return Result;

        }


        public string ExecuteScalar(string strConnectionSring, string strScript)
        {
            SqlConnection ObjSqlConnection = null;
            SqlCommand ObjSqlCommand = null;
            ObjSqlConnection = new SqlConnection(strConnectionSring);
            string strMessage = null;

            try
            {
                ObjSqlCommand = new SqlCommand(strScript, ObjSqlConnection);
                ObjSqlConnection.Open();
                ObjSqlCommand.ExecuteScalar();
                strMessage = "Succes";
            }
            catch (Exception ex)
            {

                strMessage = ex.Message.ToString();
            }
            finally
            {
                ObjSqlConnection.Close();
                ObjSqlCommand = null;
                ObjSqlConnection = null;
                strScript = null;

            }
            return strMessage;
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions