コード例 #1
0
ファイル: SQLConfiguration.cs プロジェクト: antonyn/SlpsTest
        //[CustomAction]
        //public static ActionResult GetLocalSqlInstances(Session session)
        //{
        //    try
        //    {
        //        LogWriter.WriteToLog("Started GetLocalSqlInstances method");
        //        var dataSources = SqlDataSourceEnumerator.Instance.GetDataSources();
        //        var isInstanceAvailable = false;
        //        foreach (DataRow row in dataSources.Rows)
        //        {
        //            if (!row["ServerName"].Equals(Environment.MachineName))
        //                continue;
        //            isInstanceAvailable = true;
        //            LogWriter.WriteToLog(@".\{0}", row["InstanceName"]);
        //        }
        //        if (isInstanceAvailable)
        //        {
        //            LogWriter.WriteToLog("SQL installation found");
        //            session["ISSQLINSTALLED"] = "1";
        //        }
        //        else
        //        {
        //            LogWriter.WriteToLog("SQL installation not found");
        //            session["ISSQLINSTALLED"] = "0";
        //        }
        //        return ActionResult.Success;
        //    }
        //    catch (Exception ex)
        //    {
        //        LogWriter.WriteToLog("Error in GetLocalSqlInstances method. EXCEPTION: {0}", ex.Message);
        //        return ActionResult.Failure;
        //    }
        //    finally
        //    {
        //        LogWriter.WriteToLog("Finished GetLocalSqlInstances method");
        //    }
        //}
        //[CustomAction]
        //public static ActionResult IsSqlAvailable(Session session)
        //{
        //    try
        //    {
        //        LogWriter.WriteToLog("Started IsSqlAvailable method");
        //        RegistryKey RK = Registry.CurrentUser.OpenSubKey(@"HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\Microsoft SQL Server");
        //        if(RK != null)
        //        {
        //            session["ISSQLINSTALLED"] = "1";
        //        }
        //        else
        //        {
        //            session["ISSQLINSTALLED"] = "0";
        //        }
        //        //SqlDataSourceEnumerator sqldatasourceenumerator1 = SqlDataSourceEnumerator.Instance;
        //        //var datatable1 = sqldatasourceenumerator1.GetDataSources();
        //        //foreach (DataRow row in datatable1.Rows)
        //        //{
        //        //    LogWriter.WriteToLog("Server Name:" + row["ServerName"]);
        //        //    LogWriter.WriteToLog("Instance Name:" + row["InstanceName"]);
        //        //    LogWriter.WriteToLog("Is Clustered:" + row["IsClustered"]);
        //        //    LogWriter.WriteToLog("Version:" + row["Version"]);
        //        //}
        //        //var getSql =
        //        //    new ManagementObjectSearcher(
        //        //        "root\\Microsoft\\SqlServer\\ComputerManagement",
        //        //        "select * from SqlServiceAdvancedProperty where SQLServiceType = 1");
        //        //if (getSql.Get().Count > 0)
        //        //{
        //        //    LogWriter.WriteToLog("SQL installation found");
        //        //    session["ISSQLINSTALLED"] = "1";
        //        //}
        //        //else
        //        //{
        //        //    LogWriter.WriteToLog("SQL installation not found");
        //        //    session["ISSQLINSTALLED"] = "0";
        //        //}
        //        return ActionResult.Success;
        //    }
        //    catch (Exception ex)
        //    {
        //        LogWriter.WriteToLog("Error in IsSqlAvailable method. EXCEPTION: {0}", ex.Message);
        //        return ActionResult.Failure;
        //    }
        //    finally
        //    {
        //        LogWriter.WriteToLog("Finished IsSqlAvailable method");
        //    }
        //}
        private static bool DoesDatabaseExist(Database database, SqlConnection connection)
        {
            bool databaseFound = false;

            string query = string.Format("select * from sys.databases where name = '{0}'", database.ToString());

            using (SqlCommand command = new SqlCommand(query, connection))
            {
                object result = command.ExecuteScalar();

                if (result != null && result.ToString() == database.ToString())
                {
                    databaseFound = true;
                    LogWriter.WriteToLog("{0} database exists", database.ToString());
                }
                else
                {
                    LogWriter.WriteToLog("{0} database does not exist", database.ToString());
                }
            }

            return databaseFound;
        }