Install MSDE as a part of setup?

Hi,

I need to install MSDE as a part of setup of my application. Setup must check if MSDE/SQL server are installed on local PC. If yes, setup must skip intalling of MSDE, otherwise it will instal MSDE in the background.

How to do that?

Thank's
Alexei

[262 byte] By [Alexei_shk] at [2007-12-16]
# 1

Hi Alexei

There are three ways to check whether or not MSSQL is installed

1. Try connecting to the database via SqlConnection or OleDbConnection.. though this will stillr eturn an error if the server is stopped, so not your best option.

2. Using SQLDMO, this is probably the most preferable out of the three (IMO), as you can do a lot more with SQLDMO than just just check for the existance of MSSQL. Now a word of warning, you will need to install the SQLDMO dll before you try to check, because it will not exist on the machine, unless MSSQL is already installed on it ;)

3. Search the registry; you can search the registry keys for 'HKEY_LOCAL_MACHINE\SOFTWARE\Mi­crosoft\MSSQLServer\Setup' and see if it exists ;)
Ok, now you have determined whether you want to install MSDE or not.

Microsoft used to include the Merge Modules for MSDE in previous versions, so you will have to use setup.exe and if you like your own setup.ini with your custom settings in it.

Ok, sounds simple.. right? Well if you check the newsgroups people ahve had all sorts of trouble executing the MSDE setup.exe from .NET.. with setup.exe just hanging.

Now if I recall correctly, the problem was with .NET waiting for setup.exe to issue an exit code and setup.exe waiting on .NET to go idle... 9I maybe slightly wrong with this).

However a very clever person 9whos name escapes me right now) figured out how to fix this.. with just a few simple lines... that had many developers (myself included) tearing out their hair.

[CODE]

string parameters = @"/settings setup.ini";

Process msde = new Process();

msde.StartInfo.FileName = Application.StartupPath + @"\MSDE_SETUP\setup.exe";

msde.StartInfo.Arguments = parameters;

msde.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

msde.EnableRaisingEvents = true;

msde.Start();

msde.WaitForInputIdle();

msde.WaitForExit();

msde.Close();


[/CODE]
Hope this has been of help to you.

Al

Big_Al at 2007-9-9 > top of Msdn Tech,Windows Forms,ClickOnce and Setup & Deployment Projects...
# 2
Hi,

I have found another way to check if SQL/MSDE is installed on computer. This can be done by ODBC.

There is a code:


[DllImport("odbc32.dll")]

private static extern short SQLAllocHandle(short hType, IntPtr inputHandle, out IntPtr outputHandle);

[DllImport("odbc32.dll")]

private static extern short SQLSetEnvAttr(IntPtr henv, int attribute, IntPtr valuePtr, int strLength);

[DllImport("odbc32.dll")]

private static extern short SQLFreeHandle(short hType, IntPtr handle);

[DllImport("odbc32.dll",CharSet=CharSet.Ansi)]

private static extern short SQLBrowseConnect(IntPtr hconn, StringBuilder inString,

short inStringLength, StringBuilder outString, short outStringLength,

out short outLengthNeeded);

private const short SQL_HANDLE_ENV = 1;

private const short SQL_HANDLE_DBC = 2;

private const int SQL_ATTR_ODBC_VERSION = 200;

private const int SQL_OV_ODBC3 = 3;

private const short SQL_SUCCESS = 0;

private const short SQL_NEED_DATA = 99;

private const short DEFAULT_RESULT_SIZE = 1024;

private const string SQL_DRIVER_STR = "DRIVER=SQL SERVER";

public static string[] GetServers()

{

string[] retval = null;

string txt = string.Empty;

IntPtr henv = IntPtr.Zero;

IntPtr hconn = IntPtr.Zero;

StringBuilder inString = new StringBuilder(SQL_DRIVER_STR);

StringBuilder outString = new StringBuilder(DEFAULT_RESULT_SIZE);

short inStringLength = (short) inString.Length;

short lenNeeded = 0;

try

{

if (SQL_SUCCESS == SQLAllocHandle(SQL_HANDLE_ENV, henv, out henv))

{

if (SQL_SUCCESS == SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(IntPtr)SQL_OV_ODBC3,0))

{

if (SQL_SUCCESS == SQLAllocHandle(SQL_HANDLE_DBC, henv, out hconn))

{

if (SQL_NEED_DATA == SQLBrowseConnect(hconn, inString, inStringLength, outString,

DEFAULT_RESULT_SIZE, out lenNeeded))

{

if (DEFAULT_RESULT_SIZE < lenNeeded)

{

outString.Capacity = lenNeeded;

if (SQL_NEED_DATA != SQLBrowseConnect(hconn, inString, inStringLength, outString,

lenNeeded,out lenNeeded))

{

throw new ApplicationException("Unabled to aquire SQL Servers from ODBC driver.");

}

}

txt = outString.ToString();

int start = txt.IndexOf("{") + 1;

int len = txt.IndexOf("}") - start;

if ((start > 0) && (len > 0))

{

txt = txt.Substring(start,len);

}

else

{

txt = string.Empty;

}

}

}

}

}

}

catch (Exception ex)

{

//Throw away any error if we are not in debug mode

#if (DEBUG)

MessageBox.Show(ex.Message,"Acquire SQL Servier List Error");

#endif

txt = string.Empty;

}

finally

{

if (hconn != IntPtr.Zero)

{

SQLFreeHandle(SQL_HANDLE_DBC,hconn);

}

if (henv != IntPtr.Zero)

{

SQLFreeHandle(SQL_HANDLE_ENV,hconn);

}

}

if (txt.Length > 0)

{

retval = txt.Split(",".ToCharArray());

}

return retval;

}

Now find "(local)" database in this array of names.
If you found, this means that SQL or MSDE is installed.

Thank's
Alexei

Alexei_shk at 2007-9-9 > top of Msdn Tech,Windows Forms,ClickOnce and Setup & Deployment Projects...