Wixsharp: How to add Condition to ODBCDataSource

Created on 4 Aug 2020  路  13Comments  路  Source: oleg-shilo/wixsharp

Hi,
I have 2 scenarios , 1 - to pick from existing odbc source or 2 - to create new with 'new ODBCDataSource' , but i'm little noob, and still can't figure out how to implement, there is no Condition property for ODBCDataSource .
I am building ManagedProject.

Done / Released enhancement question

All 13 comments

Yes, it is a problem. This is because MSI/WiX does not support conditions for ODBCDataSource element.

Though you can have a condition associated with the parent component. Similar problem (but for RegValue element) is discussed here:

C# regVal.AttributesDefinition += $";Component:Condition={yourCondition}";

I read that but i don't understand it well, i need more complete sample :)

My code is like this:
C# project = new ManagedProject("Setup", new Dir($@"%ProgramFiles%\{myFolder}", new File( binaries, @"Files\app.exe" ,new FileShortcut(binaries, "MyApp", $@"%ProgramMenu%\{myFolder}") ,new FileShortcut(binaries, "MyApp", @"%Desktop%") ) ,new DirFiles(binaries, @"Files\*.dll") ,new Dir("Documentation", new DirFiles(docs, @"Files\docs\*.*") ) ) , new Property("INSTALLODBC", "no") );

Where to put ODBC , and how to add AttributesDefinition

:)

You should use triple ` to create code block
image

I have updated your code sample

Have a look at the ODBC sample:
https://github.com/oleg-shilo/wixsharp/blob/6007f95ec3391969563da4ed4163ce5430cde814/Source/src/WixSharp.Samples/Wix%23%20Samples/ODBCDataSource/setup.cs

Basically you will need:

C# new ODBCDataSource(...) { AttributesDefinition = $";Component:Condition={yourCondition}"; }

```C#
project = new ManagedProject("Setup",
new Dir($@"%ProgramFiles%{myFolder}",
new File( binaries, @"Files\app.exe"
,new FileShortcut(binaries, "MyApp", $@"%ProgramMenu%{myFolder}")
,new FileShortcut(binaries, "MyApp", @"%Desktop%")
)
,new DirFiles(binaries, @"Files*.dll")
,new Dir("Documentation", new DirFiles(docs, @"Files\docs*.*")
,new ODBCDataSource("[ODBC_DSN]", "[ODBC_DRIVER]", true, true,
new Property("Database", "[ODBC_DATABASE]"),
new Property("Server", "[ODBC_SERVER]")
)
{
AttributesDefinition = $";Component:Condition={new Condition("INSTALLODBC=\"yes\"")}"
}

                            )
                        )
                        , new Property("INSTALLODBC", "no")
                    );

`` error CNDL0004: The Component element contains an unexpected attribute 'Condition'. (

my bad, component does not have a _condition_ as an attribute but as an element.
will have a look at it. you will need to use XML-injection then

OK. this is how you can do it:
```C#
project.WixSourceGenerated +=
doc => doc.FindFirst("ODBCDataSource")
.Parent("Component")
.AddElement(new XElement("Condition", "INSTALLODBC = \"yes\""));

```
I will have a look and provide a moreconvenient way of defining component conditions.

This way is working .
Now i need to find how to change ODBCDataSource( source_name and driver dinamically, with session[source_name] doesn't work 馃憤

Done. In next release you will be able to use:

C# new ODBCDataSource("DsnName", ...) { ComponentCondition = "INSTALLODBC = \"yes\"" }

Any clue how to set ODBC( name , driver ,.. ) from session, they are strings ?
i cane get in msi_BeforeInstall
...
e.Session.Components["ODBCDataSource"] , and thats all :)

if it is possible ofcourse...

I am not sure it is possible.
The documentations does not indicate that these attributes can be determined dynamically for session properties:
https://wixtoolset.org/documentation/manual/v3/xsd/wix/odbcdatasource.html

:(

Np, so i will search other ways, like set registry keys, or post install app, that will make that kind of changes, or set odbc, and after install change name,driver , i wasn't familiar with limits of wix, and persisted, beg your pardon 馃憤
But still, wix# have great possibilities than VS installer project :)

Way founded:
```C#
[DllImport("ODBCCP32.DLL", CharSet = CharSet.Unicode, SetLastError = true)]
static extern bool SQLConfigDataSourceW(IntPtr hwndParent, RequestFlags fRequest, string lpszDriver, string lpszAttributes);

    enum RequestFlags : ushort
    {

        ODBC_ADD_DSN = 1,
        ODBC_CONFIG_DSN = 2,
        ODBC_REMOVE_DSN = 3,
        ODBC_ADD_SYS_DSN = 4,
        ODBC_CONFIG_SYS_DSN = 5,
        ODBC_REMOVE_SYS_DSN = 6,
        ODBC_REMOVE_DEFAULT_DSN = 7
    };
    public static bool AddDSN(string name, string dbn, string eng,string host="")
    {
        return SQLConfigDataSourceW(
            (IntPtr)null, RequestFlags.ODBC_ADD_DSN
            , Driver
            , $"DSN={name}\0DBN={dbn}\0ENG={eng}\0LINKS=TCPIP(HOST={host})\0"
            );
    }

```
PS: lpszDriver, and lpszAttributes are for my case

Was this page helpful?
0 / 5 - 0 ratings

Related issues

empz picture empz  路  3Comments

kain64 picture kain64  路  4Comments

ju2pom picture ju2pom  路  5Comments

meytes picture meytes  路  3Comments

yfnfif picture yfnfif  路  3Comments