Problem inserting to MSSQLDB

Welcome Forums General PowerShell Q&A Problem inserting to MSSQLDB

This topic contains 2 replies, has 2 voices, and was last updated by

3 years, 4 months ago.

  • Author
  • #29178

    Points: 0
    Rank: Member

    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

    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    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

    Points: 0
    Rank: Member

    Tansk again Don.

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

    Solved my problem.

The topic ‘Problem inserting to MSSQLDB’ is closed to new replies.