MS SQL and Powershell Studio

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 1 day old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
swehner
Posts: 7
Last visit: Wed Jun 23, 2021 11:17 pm

MS SQL and Powershell Studio

Post by swehner »

Hi all,

i have a Problem using SQL and PowerShell-Studio lates Version 64Bit.
in PowerShell-Studio the Code doesn't work. If i use
PowerShell-ISE the Code works fine.
  1. $sqlCon = New-Object System.Data.SqlClient.SqlConnection
  2. $sqlCon.ConnectionString = "Data Source=OFWHSQT1\INFRASTRUKTUR; Integrated Security=True; Initial Catalog=Anmeldedatenbank"
  3. $sqlCon.open()
  4.  
  5. $sqlCmd = New-Object System.Data.SqlClient.SqlCommand
  6. $sqlCmd.Connection = $sqlCon
  7. $strCmd = "Select * from Usermappings where username = '$env:USERNAME' and MappingType ='P'"
  8. $sqlCmd.CommandText = $strCmd
  9.  
  10. $sqlReader = $sqlCmd.ExecuteReader()
  11. # +++ Datensätze einlesen
  12. # F_WriteLogFile ("Folgende Drucker wurden in der Datenbank gefunden!")
  13. $intLoopcount = 0
  14.  
  15. [color=#FF0000]The following part doesn't work. PowerShell jumps straight  to SQLReader Close[/color]
  16.  
  17. While ($sqlReader.Read())
  18. {
  19.     [String]$strServer = $sqlReader.Item("PrintServer").ToString().Trim()
  20.     [String]$strShareName = $sqlReader.Item("ShareName").ToString().ToUpper()
  21.     [String]$strPrinterName = $sqlReader.Item("PrinterName").ToString().ToUpper()
  22.     [String]$strPath = "\\" + $strServer + "\" + $strShareName
  23.     [String]$strKey = "\\" + $strServer + "\" + $strPrinterName
  24.     $dicDBprinter.Add($strKey, $strPath)
  25.     [Int]$intLoopcount = $intLoopcount + 1
  26.     #F_WriteLogFile("`t" + $strPrinterName)
  27.     If ($intLoopcount -gt 30)
  28.     {
  29.         #F_WriteLogFile "Möglicher Fehler in DB, Druckeranbindung in Schleife hängengeblieben!!"
  30.         [Bool]$bolLoopdetect = $True
  31.         Exit
  32.     }
  33. }
  34. $sqlReader.Close()
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 9:46 am
Answers: 39
Been upvoted: 30 times

Re: MS SQL and Powershell Studio

Post by brittneyr »

What errors are you receiving?
Brittney
SAPIEN Technologies, Inc.
User avatar
swehner
Posts: 7
Last visit: Wed Jun 23, 2021 11:17 pm

Re: MS SQL and Powershell Studio

Post by swehner »

Hi. I Get no error.
$sqlReader say's that $sqlReader has rows, but the while loop jumps directly to $sqlReader.close()

If i use the PowershellIse it works fine.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: MS SQL and Powershell Studio

Post by jvierra »

SQlReader does not have rows in the normal sense. It has to be moved forward with a "read()" command then the data for a row is available. The object can only be read once then it must be closed and reopened to use it again. There is no need to use the reader. Just load the DataTable and you can then do anything you need easily.

The code you are using is badly converted VB.Net code that is floating around the Internet. No programmer would do this with PowerShell. I can tell this because all of the constructs are from the VB.Net examples of how to code with a reader. They are also old and may have come from a bad VBScript example converted to ADO.Net or PowerShell. We haven't used Hungarian notation for 20 years but old VB and VBScri0pt coders seem to continue using it.

Here is how to code a reader in PowerShell:

Code: Select all

$intLoopcount = 0
While ($sqlReader.Read()){
    
    $strServer = $sqlReader.Item('PrintServer')
    $strShareName = $sqlReader.Item('ShareName')
    $strPrinterName = $sqlReader.Item('PrinterName')
    $strPath = "\\$strServer\$strShareName"
    $strKey = "\\$strServer\$strPrinterName"
    Write-Host $strKey,$strPath 
    $dicDBprinter.Add($strKey, $strPath)
    
    If ($intLoopcount++ -ge 30){
        #F_WriteLogFile "Möglicher Fehler in DB, Druckeranbindung in Schleife hängengeblieben!!"
        Write-Host 'Exiting PowerShell'
        pause
        Exit
    }
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: MS SQL and Powershell Studio

Post by jvierra »

I just ran your code with my changes in PowerShell Studio and it ran just fine.
This topic is 4 years and 1 day old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked