karthik
karthik

Reputation: 17832

How to know MSSql Server is installed or not in windows XP or 7?

Before going to install MSSQLSERVER, I want to know whether MSSQLSERVER is already installed in the system or not. This should be done programatically in C++ or mfc.

If the MSSQLServer is already installed in the windows then Is there any possible way to get the MSSql Credentials of that server?

If So, Please Explain the way to achieve this?

UPDATE

I tried to install sqlserver 2005 and sql server 2008 in my system but both the servers are installed. I checked the registry path but It contains SOFTWARE\Microsoft\Microsoft SQL Server\90 for MSSQLServer 2005 and SOFTWARE\Microsoft\Microsoft SQL Server\100 for MSSqlServer 2008.Before installing my MSSQLServer How can I check whether any of the MSSql Server version is installed or not?

EDIT

Till Now I silent installed the SQLSERVER programmatically in c++.

I Posted the code below:

SHELLEXECUTEINFO ShExecInfo;
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = NULL;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = L"D:\\Softies\\SQLEXPR.EXE";
ShExecInfo.lpParameters = L"/qn addlocal=all InstanceName=SQLEXPRESS DisableNetworkProtocols=0 SECURITYMODE=SQL SAPWD=root SQLAUTOSTART=1 SQLBROWSERAUTOSTART=1 ENABLERANU=0";
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_MAXIMIZE;
ShExecInfo.hInstApp = NULL;

ShellExecuteEx(&ShExecInfo);

int nResult=0;
nResult = (int )ShExecInfo.hInstApp;
if( nResult >32) 
cout<<"EXE executed successfully"<<endl;
else
cout<<"Reason for failure is" <<nResult<<endl;
return 0;

Upvotes: 0

Views: 1365

Answers (1)

Oleg Dok
Oleg Dok

Reputation: 21756

  • You cannot get the server's credentials, except of brute force

BUT

If you're a local administrator - you can start sql server from the command line with startup key -f and try to play with it - it is not guaranteed, but you can try.

  • I have a code which checks the some defaults for sql server instance, so it can be easily adopted to your needs and c++/

    private void GetSqlDefaultInfo(string ServerName, string InstanceName)
    {
    
        InstanceName = string.IsNullOrEmpty(InstanceName) ? "MSSQLSERVER" : InstanceName;
    
        if (string.IsNullOrEmpty(ServerName))
            ServerName = Environment.MachineName;
        using (var registryKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, ServerName))
        {
            object sqlInstance;
            using (var subKey = registryKey.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL"))
                sqlInstance = subKey.GetValue(InstanceName);
            if (sqlInstance != null && !string.IsNullOrEmpty(sqlInstance.ToString()))
            {
                var sqlPathKey = string.Format(@"SOFTWARE\Microsoft\Microsoft SQL Server\{0}\MSSQLServer",
                                               sqlInstance);
                object defaultData, defaultLog, backupDirectory, sqlPath;
                using (var subKey = registryKey.OpenSubKey(sqlPathKey))
                {
                    defaultData = subKey.GetValue("DefaultData");
                    defaultLog = subKey.GetValue("DefaultLog");
                    backupDirectory = subKey.GetValue("BackupDirectory");
                }
                sqlPathKey = string.Format(@"SOFTWARE\Microsoft\Microsoft SQL Server\{0}\Setup", sqlInstance);
    
                using (var subKey = registryKey.OpenSubKey(sqlPathKey))
                    sqlPath = subKey.GetValue("SQLDataRoot");
                DataFilePath = defaultData != null
                                   ? defaultData.ToString()
                                   : Path.Combine(sqlPath.ToString(), "Data").TrimEnd('\\');
    
                LogFilePath = defaultLog != null
                                  ? defaultLog.ToString()
                                  : Path.Combine(sqlPath.ToString(), "Data").TrimEnd('\\');
                FTSIndexFilePath = DataFilePath;
                ContentFilePath = DataFilePath;
                BackupFilePath = backupDirectory != null
                                     ? backupDirectory.ToString()
                                     : Path.Combine(sqlPath.ToString(), "Backup").TrimEnd('\\');
            }
        }
    }
    

Upvotes: 1

Related Questions