Problem inserting to MSSQLDB

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Arestas Arestas 1 year, 1 month ago.

  • Author
  • #29178
    Profile photo of Arestas

    Hello all,
    Having a problem inserting to MSSQL server Database.

    $user = ([adsisearcher]"(&(objectClass=user)(samaccountname=$owner))").FindOne().Properties
    $samAccountname = $user.samaccountname
    $UserPrincipalName = $user.userprincipalname
    $DistinguishedName = $user.distinguishedname

    $sqlCmd = $sqlConn.CreateCommand()
    $sqlCmd.Parameters.AddWithValue("@user_login" ,$samAccountname)
    $sqlCmd.Parameters.AddWithValue("@user_domain" ,$DOMAIN)
    $sqlCmd.Parameters.AddWithValue("@distin_name" ,$DistinguishedName)
    $sqlCmd.Parameters.AddWithValue("@update" ,$date)
    $sqlCmd.CommandText = "INSERT INTO [dbo].[USERS] ([USER_LOGIN],[USER_DOMAIN],[DISTIN_NAME],[UPDATE],[USER_PINCIPAL]) VALUES (@user_login,@user_domain,@distin_name,@update,@user_pincipal)";
    $sqlCmd.ExecuteNonQuery(); #Here gives ERROR

    The error it gives is :
    Exception calling "ExecuteNonQuery" with "0" argument(s): "No mapping exists from object type System.DirectoryServices.ResultPropertyValueCollection to a known managed provider native type."
    At line:1 char:1
    + $sqlCmd.ExecuteNonQuery();
    + ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

    Can anyone help me with this?


  • #29180
    Profile photo of Don Jones
    Don Jones

    One of the values you're inserting, from your variables, is actually an object rather than a simple value. SQL doesn't know how to turn the object into a value it understands, like a string or number. Check the contents of your variables – one or more of them doesn't contain what you expected.

  • #29183
    Profile photo of Arestas

    Tansk again Don.

    [string]$samAccountname = $user.samaccountname
    [string]$UserPrincipalName = $user.userprincipalname
    [string]$DistinguishedName = $user.distinguishedname

    Solved my problem.

You must be logged in to reply to this topic.