So I'm working in Powershell (yeah, bringing my Powershell questions over here because we have long left the realm of system administration) and trying to mass modify Access databases. After much questing, I have found how to query Access databases in Powershell.
I've had to chop up a bunch of the code so it obviously wouldn't just work if you copy/pasted it. In any case, here's the deal. This code works when my query is simply "select * from known_good_table" So something's wrong with my query, right? Right. Wrong. Query's fine. You see there I'm outputting $query1, and I copy that output directly in to Access and it works as expected. Same thing when I output $cmd1.CommandText, just to make sure something didn't get beat up in translation.
The exact error I'm getting is,
Value is very much not null. Best I can figure is something has a problem with the where clause in my query, but it's not Access. Oh, also the where clause will return data, so it's not an issue of it not handling no data returned (or at least it shouldn't be). Thoughts and opinions?
Code:
$conn = new-object data.odbc.odbcconnection
$conn.ConnectionString = "dsn=$connectionname"
$conn.open()
$query1 = "select * from known_good_table where known_good_field = `"" + $myobject.SomeValue + "`""
write-host $query1
$cmd1 = $conn.CreateCommand()
$cmd1.CommandText = $query1
$data1 = $cmd1.ExecuteReader()
$result1 = new-object "System.Data.DataTable"
$result1.Load($data1)
I've had to chop up a bunch of the code so it obviously wouldn't just work if you copy/pasted it. In any case, here's the deal. This code works when my query is simply "select * from known_good_table" So something's wrong with my query, right? Right. Wrong. Query's fine. You see there I'm outputting $query1, and I copy that output directly in to Access and it works as expected. Same thing when I output $cmd1.CommandText, just to make sure something didn't get beat up in translation.
The exact error I'm getting is,
Code:
Exception calling "ExecuteReader" with "0" argument(s): "ERROR [07002] [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1."
At D:\My\Path\MyScript.ps1:39 char:5
+ $data1 = $cmd1.ExecuteReader()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : OdbcException
Exception calling "Load" with "1" argument(s): "Value cannot be null.
Parameter name: dataReader"
At D:\My\Path\MyScript.ps1:41 char:5
+ $result1.Load($data1)
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
Value is very much not null. Best I can figure is something has a problem with the where clause in my query, but it's not Access. Oh, also the where clause will return data, so it's not an issue of it not handling no data returned (or at least it shouldn't be). Thoughts and opinions?