I'm trying to work with the SQLiteConnectionStringBuilder
class in the ADO.NET SQLite data provider library found here. The class definition has a property called DataSource.
public sealed class SQLiteConnectionStringBuilder : DbConnectionStringBuilder
{
public SQLiteConnectionStringBuilder();
public SQLiteConnectionStringBuilder(string connectionString);
public string DataSource { get; set; }
}
which I'm trying to set in my powershell script, viz:
$csb = new-object -TypeName System.Data.SQLite.SQLiteConnectionStringBuilder
$csb.DataSource = "C:\data\mydb.db";
The problem I'm having is that SQLiteConnectionStringBuilder
inherits from DbConnectionStringBuilder
which implements the following interfaces
public class DbConnectionStringBuilder : IDictionary, ICollection, IEnumerable, ....
Because the class implements these interfaces Powershell is NOT setting the property DataSource on the class but is treating $csb
as a hash table and adding a key value pair to it i.e. key="DataSource", value="C:\data\mydb.db"
. Needless to say this is not what I want. So what do I do now ? How to force Powershell set the property and not treat $csb to be a hash table?
Thanks!
The Set-ItemProperty cmdlet changes the value of the property of the specified item. You can use the cmdlet to establish or change the properties of items. For example, you can use Set-ItemProperty to set the value of the IsReadOnly property of a file object to $True .
From the about_Operators help topic: :: Static member operator Calls the static properties operator and methods of a .NET Framework class. To find the static properties and methods of an object, use the Static parameter of the Get-Member cmdlet. [
@{} in PowerShell defines a hashtable, a data structure for mapping unique keys to values (in other languages this data structure is called "dictionary" or "associative array"). @{} on its own defines an empty hashtable, that can then be filled with values, e.g. like this: $h = @{} $h['a'] = 'foo' $h['b'] = 'bar'
You can create an ordered dictionary by adding an object of the OrderedDictionary type, but the easiest way to create an ordered dictionary is use the [ordered] attribute. The [ordered] attribute is introduced in PowerShell 3.0. Place the attribute immediately before the "@" symbol.
If you are trying to set this when you are creating the object it is possible using the Property
parameter of New-Object
:
$csb = New-Object -TypeName System.Data.SQLite.SQLiteConnectionStringBuilder -Property @{ DataSource = "C:\data\mydb.db" }
However if you are trying to set the property at some later time, the only way I have found is somewhat clumsy, using the .NET Reflection API:
$csb.GetType().GetProperty("DataSource").SetValue($csb, "C:\data\mydb.db")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With