Wednesday, February 14, 2007

Drop default constraint

Somebody created table like this:

CREATE TABLE [dbo].[MyTable] (
...
[MyField] [bit] NOT NULL DEFAULT (1),
...
) ON [PRIMARY]

I need to do some changes, but first I should drop constraints and after changes, create again. Oops, I don't know constrain name, because somebody used bad style in creation tables.
I need to execute SQL query like following
ALTER TABLE [dbo].[TreeNodeRelation] DROP CONSTRAINT [DF__MyTable__MyField__2B3F6F97] - but we cannot be sure that name will be same on all installations because it is autogenerated during "create table".

Solution. Following example receives name of default constraint for the specific field, and if it is exists, drop it.


declare @cnstname sysname
declare @sql nvarchar(4000)

select @cnstname = [name] from sysobjects as so
where xtype = 'D' and parent_obj = object_id('dbo.MyTable')
and col_name(parent_obj, info) = 'MyField'

if @cnstname is not null
begin
set @sql = N'alter table dbo.MyTable drop constraint ' + @cnstname
execute sp_executesql @sql
end

Sunday, February 04, 2007

Access common windows folders from C#

I interested to get desktop and programs folder from C# application to create some shortcuts there. .NET allows it in following way:

Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Environment.GetFolderPath(Environment.SpecialFolder.Programs);

But results are directories for the current user, like

c:\document And Settings\LastDon\Destkop.

However I want to place shortcuts for all users, for profile ALLUSERS.
So, I found registry key

HKEY_LOCAL_MACHINE\SOFTWARE\microsoft\windows\currentversion\explorer\shell folders

Not too happy - Windows 2003 is not using this key, and anyway, it is not recommended by microsoft.
So, I come to solution by using API Shell32.dll:



[DllImport("Shell32.DLL")]
public static extern int SHGetSpecialFolderLocation(
IntPtr hwndOwner, int nFolder, out IntPtr ppidl);

[DllImport("Shell32.DLL")]
public static extern int SHGetPathFromIDList(
IntPtr pidl, StringBuilder Path);

const int CSIDL_COMMON_PROGRAMS = 23;
const int CSIDL_COMMON_STARTUP = 24;
const int CSIDL_COMMON_DESKTOPDIRECTORY = 25;

private string GetSpecialFolder(int folderType)
{
IntPtr pResultList;
IntPtr handlePtr = new IntPtr(0);
int result =SHGetSpecialFolderLocation(handlePtr, folderType, out pResultList);
if (result < 0)
{
return null;
}
StringBuilder sb = new StringBuilder (500);
result = SHGetPathFromIDList(pResultList, sb);
if (result == 0) // boolean result
{
return null;
}
return sb.ToString();
}




Get destkop path:
GetSpecialFolder(CSIDL_COMMON_DESKTOPDIRECTORY);
Get programs path:
GetSpecialFolder(CSIDL_COMMON_PROGRAMS);

Following microsoft article helped me to solve it out
http://support.microsoft.com/kb/306285

Saturday, February 03, 2007

Uninstall by installshield IDriver

Again problem with installshield.
Uninstall installshield product (InstallScript & MSI) command line is:
IDriver.exe /M{GUID}
I found this command in the registry in uninstall key under InstallShield_{GUID}.

But I want it uninstall silent so i run
IDriver.exe /M{GUID} /uninst

Nothing. Product is not uninstalling. Macrovision just kidding.
Ok, solution is following.
1. Run setup.exe /r from product media and do uninstall the product
2. Copy setup.iss file from windows folder to somewhere.
3. Now command line to uninstall silent will be:
Idriver.exe /M{GUID} /f1"c:\somewhere\setup.iss"

Thanks God, it works, so I can return back to my C# work.