잊지 않겠습니다.

MS SQL Query Script

.NET Framework 2009. 7. 13. 15:37

[System.Reflection.Assembly]::LoadWithPartialName("System")
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Reflection.Assembly]::LoadWithPartialName("System.Data")

$fileContexts = [System.IO.File]::ReadAllLines("c:\mailStore.txt")
$connection = New-Object System.Data.SqlClient.SqlConnection("==THIS IS CONNECTION STRING==");

foreach($fileContext in $fileContexts) {
    $samAccount = $fileContext.Replace("name: ", "").Replace("@mailpush.co.kr", "")
       
    $connection.Open()
    $command = New-Object System.Data.SqlClient.SqlCommand;
    $command.Connection = $connection
    $command.CommandText = "SELECT PhoneNumber, Email From Product WHERE LogonName = '" + $samAccount + "'" + " and ProductStateId = 1"
    $reader = $command.ExecuteReader()

    while($reader.Read())
    {
        $reader.GetSqlString(0).ToString() + "`t" + $reader.GetSqlString(1).ToString() >> c:\output1.txt
    }
    $reader.Close()
    $connection.Close()   
}
[System.Windows.Forms.MessageBox]::Show("Complete!")
notepad.exe "c:\output1.txt"


재미있는 것은 .NET쪽 코드랑 별 다를 것이 없는 코드를 내가 작성하고 있다는 것이다. -_-


Posted by Y2K
,