Database Existence Checking

Posted by imomins on March 29, 2011 at 12:43 AM

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using net.ERP.Constants;

using System.Data;

using System.Data.Sql;

using System.Data.SqlClient;

using System.Windows.Forms;

 

namespace net.ERP.DatabaseSchema.DBExistanceChecking

{

    class DatabaseExistanceChecking:CommonConstants

    {

        public bool mpubbIsDatabaseExists(string pstrServerName)

        {

            SqlConnection lprisqlconnSQLConnection = new SqlConnection("Server=" + pstrServerName + ";Integrated security=SSPI;database=master");

            try

            {

                lprisqlconnSQLConnection.Open();

                SqlCommand lsqlcommSQLCommand = lprisqlconnSQLConnection.CreateCommand();

                lsqlcommSQLCommand.CommandText = "SELECT COUNT(*) AS TOTAL_RECORDS FROM master.dbo.sysdatabases WHERE NAME = 'NET_ERP_REMS'";

                SqlDataReader lsqldrSqlDataReader = lsqlcommSQLCommand.ExecuteReader();

 

                if (lsqldrSqlDataReader.Read())

                {

                    int liTotalDatabases = (int)lsqldrSqlDataReader["TOTAL_RECORDS"];

                    lsqldrSqlDataReader.Close();

                    if (liTotalDatabases > 0)

                    {

                        return true;

                    }

                    else

                    {

                        return false;

                    }

                }

                return true;

            }

            catch (Exception pexpDBExistance)

            {

                MessageBox.Show(pexpDBExistance.ToString(), gmpubcstrUserConfirmation, MessageBoxButtons.OK, MessageBoxIcon.Information);

                return false;

            }

            finally

            {

                if (lprisqlconnSQLConnection.State == ConnectionState.Open)

                {

                    lprisqlconnSQLConnection.Close();

                }

            }

        }

    }

}

 


Categories: C#, SQL, Personal

Post a Comment

Oops!

Oops, you forgot something.

Oops!

The words you entered did not match the given text. Please try again.

You must be a member to comment on this page. Sign In or Register

0 Comments