| 1 | <html xmlns="http://www.w3.org/1999/xhtml"> |
| 2 | <head> |
| 3 | <title>Working with MSI Databases</title> |
| 4 | <link rel="stylesheet" type="text/css" href="../styles/presentation.css" /> |
| 5 | <link rel="stylesheet" type="text/css" href="ms-help://Hx/HxRuntime/HxLink.css" /> |
| 6 | </head> |
| 7 | |
| 8 | <body> |
| 9 | |
| 10 | <div id="control"> |
| 11 | <span class="productTitle">Deployment Tools Foundation</span><br /> |
| 12 | <span class="topicTitle">Working with MSI Databases</span><br /> |
| 13 | <div id="toolbar"> |
| 14 | <span id="chickenFeet"> |
| 15 | <a href="using.htm">Development Guide</a> > |
| 16 | <span class="nolink">MSI Databases</span> |
| 17 | </span> |
| 18 | </div> |
| 19 | </div> |
| 20 | <div id="main"> |
| 21 | <div id="header"> |
| 22 | </div> |
| 23 | <div class="summary"> |
| 24 | |
| 25 | <h3>Querying a database</h3> |
| 26 | <pre><font face="Consolas, Courier New"> <font color=blue>using</font> (Database db = <font color=blue>new</font> Database(<font color="purple">"product.msi"</font>, DatabaseOpenMode.ReadOnly)) |
| 27 | { |
| 28 | <font color=blue>string</font> value = (<font color=blue>string</font>) db.ExecuteScalar( |
| 29 | <font color="purple">"SELECT `Value` FROM `Property` WHERE `Property` = '{0}'"</font>, propName); |
| 30 | }</font></pre><br /> |
| 31 | <p>1. Create a <a href="DTFAPI.chm::/html/Overload_Microsoft_Deployment_WindowsInstaller_Database__ctor.htm">new Database</a> |
| 32 | instance referring to the location of the .msi or .msm file.</p> |
| 33 | <p>2. Execute the query:</p><ul> |
| 34 | <li>The <a href="DTFAPI.chm::/html/Overload_Microsoft_Deployment_WindowsInstaller_Database_ExecuteScalar.htm">ExecuteScalar</a> |
| 35 | method is a shortcut for opening a view, executing the view, and fetching a single value.</li> |
| 36 | <li>The <a href="DTFAPI.chm::/html/Overload_Microsoft_Deployment_WindowsInstaller_Database_ExecuteQuery.htm">ExecuteQuery</a> |
| 37 | method is a shortcut for opening a view, executing the view, and fetching all values.</li> |
| 38 | <li>Or do it all manually with <a href="DTFAPI.chm::/html/M_Microsoft_Deployment_WindowsInstaller_Database_OpenView.htm">Database.OpenView</a>, |
| 39 | <a href="DTFAPI.chm::/html/Overload_Microsoft_Deployment_WindowsInstaller_View_Execute.htm">View.Execute</a>, and |
| 40 | <a href="DTFAPI.chm::/html/M_Microsoft_Deployment_WindowsInstaller_View_Fetch.htm">View.Fetch</a>.</li> |
| 41 | </ul> |
| 42 | |
| 43 | <p><br/></p> |
| 44 | <h3>Updating a binary</h3> |
| 45 | <pre><font face="Consolas, Courier New"> Database db = <font color=blue>null</font>; |
| 46 | View view = <font color=blue>null</font>; |
| 47 | Record rec = <font color=blue>null</font>; |
| 48 | <font color=blue>try</font> |
| 49 | { |
| 50 | db = <font color=blue>new</font> Database(<font color="purple">"product.msi"</font>, DatabaseOpenMode.Direct); |
| 51 | view = db.OpenView(<font color="purple">"UPDATE `Binary` SET `Data` = ? WHERE `Name` = '{0}'"</font>, binName)) |
| 52 | rec = <font color=blue>new</font> Record(1); |
| 53 | rec.SetStream(1, binFile); |
| 54 | view.Execute(rec); |
| 55 | db.Commit(); |
| 56 | } |
| 57 | <font color=blue>finally</font> |
| 58 | { |
| 59 | <font color=blue>if</font> (rec != <font color=blue>null</font>) rec.Close(); |
| 60 | <font color=blue>if</font> (view != <font color=blue>null</font>) view.Close(); |
| 61 | <font color=blue>if</font> (db != <font color=blue>null</font>) db.Close(); |
| 62 | }</font></pre><br /> |
| 63 | <p>1. Create a <a href="DTFAPI.chm::/html/Overload_Microsoft_Deployment_WindowsInstaller_Database__ctor.htm">new Database</a> |
| 64 | instance referring to the location of the .msi or .msm file.</p> |
| 65 | <p>2. Open a view by calling one of the <a href="DTFAPI.chm::/html/M_Microsoft_Deployment_WindowsInstaller_Database_OpenView.htm">Database.OpenView</a> overloads.</p><ul> |
| 66 | <li>Parameters can be substituted in the SQL string using the String.Format syntax.</li> |
| 67 | </ul> |
| 68 | <p>3. Create a record with one field containing the new binary value.</p> |
| 69 | <p>4. Execute the view by calling one of the <a href="DTFAPI.chm::/html/Overload_Microsoft_Deployment_WindowsInstaller_View_Execute.htm">View.Execute</a> overloads.</p><ul> |
| 70 | <li>A record can be supplied for substitution of field tokens (?) in the query.</li> |
| 71 | </ul> |
| 72 | <p>5. <a href="DTFAPI.chm::/html/M_Microsoft_Deployment_WindowsInstaller_Database_Commit.htm">Commit</a> the Database.</p> |
| 73 | <p>6. <a href="DTFAPI.chm::/html/M_Microsoft_Deployment_WindowsInstaller_InstallerHandle_Close.htm">Close</a> the handles.</p> |
| 74 | |
| 75 | <p><br/></p> |
| 76 | <h3>About handles</h3> |
| 77 | <p>Handle objects (Database, View, Record, SummaryInfo, Session) will remain open until |
| 78 | they are explicitly closed or until the objects are collected by the GC. So for the tightest |
| 79 | code, handle objects should be explicitly closed when they are no longer needed, |
| 80 | since closing them can release significant resources, and too many unnecessary |
| 81 | open handles can degrade performance. This is especially important within a loop |
| 82 | construct: for example when iterating over all the Records in a table, it is much cleaner |
| 83 | and faster to close each Record after it is used.</p> |
| 84 | <p>The handle classes in the managed library all extend the |
| 85 | <a href="DTFAPI.chm::/html/T_Microsoft_Deployment_WindowsInstaller_InstallerHandle.htm">InstallerHandle</a> |
| 86 | class, which implements the IDisposable interface. This makes them easily managed with C#'s |
| 87 | using statement. Alternatively, they can be closed in a finally block.</p> |
| 88 | <p>As a general rule, <i>methods</i> in the library return new handle objects that should be managed |
| 89 | and closed by the calling code, while <i>properties</i> only return a reference to a prexisting handle |
| 90 | object.</p> |
| 91 | |
| 92 | <p><br/></p> |
| 93 | <p><b>See also:</b></p> |
| 94 | <ul> |
| 95 | <li><a href="powerdiff.htm">MSI Diff Sample Tool</a></li> |
| 96 | <li><a href="DTFAPI.chm::/html/T_Microsoft_Deployment_WindowsInstaller_Database.htm">Database Class</a></li> |
| 97 | </ul> |
| 98 | <p><br/></p> |
| 99 | |
| 100 | </div> |
| 101 | |
| 102 | <div id="footer"> |
| 103 | <p /> |
| 104 | Send comments on this topic to <a id="HT_MailLink" href="mailto:wix-users%40lists.sourceforge.net?Subject=Deployment Tools Foundation Documentation"> |
| 105 | wix-users@lists.sourceforge.net</a> |
| 106 | |
| 107 | <script type="text/javascript"> |
| 108 | var HT_mailLink = document.getElementById("HT_MailLink"); |
| 109 | var HT_mailLinkText = HT_mailLink.innerHTML; |
| 110 | HT_mailLink.href += ": " + document.title; |
| 111 | HT_mailLink.innerHTML = HT_mailLinkText; |
| 112 | </script> |
| 113 | |
| 114 | <p /> |
| 115 | |
| 116 | </div> |
| 117 | </div> |
| 118 | |
| 119 | </body> |
| 120 | </html> |