I have some problems with System.Data.SqlClient.SqlCredential class. I try to create an object in powershell by using this
cls;
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Data");
$creds = New-Object System.Data.SqlClient.SqlCredential;
But I get this error message
New-Object : Конструктор не найден. Не удается найти соответствующий конструкто
р для типа System.Data.SqlClient.SqlCredential.
строка:3 знак:10
+ $creds = New-Object System.Data.SqlClient.SqlCredential;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [New-Object], PSArgumentExce
ption
+ FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.C
ommands.NewObjectCommand
The translation of this error message is as follows:
The constructor could not be found. Unable to find the appropriate constructor for type.
I read technet and found out, that System.Data.SqlClient.SqlCredential class is contained in System.Data (System.Data.dll).
But I tried to load this assembly as you see from previous example. I use Windows 7, PowerShell 4.0. I have .NET Frameworks 4.6.1 installed. I'll really appreciate any help.
I've tried to use the tip GodEater offered me, but still get an error message
cls;
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Data");
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection;
$SqlConnection.ConnectionString = "Server=192.168.1.220,1433;Database=Archimed;";
$creds = New-Object System.Data.SqlClient.SqlCredential`
((Read-Host -AsSecureString), (Read-Host -AsSecureString));
$SqlConnection.Credential = $creds;
$SqlConnection.Open();
$SqlConnection.Close();
Error message is like this
New-Object : Не удается найти тип [System.Data.SqlClient.SqlCredential]: убедит
есь в том, что сборка, содержащая этот тип, загружена.
строка:7 знак:10
+ $creds = New-Object System.Data.SqlClient.SqlCredential`
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentExcepti
on
+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewOb
jectCommand
System.Security.SecureString
System.Security.SecureString
Исключение при вызове "Open" с "0" аргументами: "Ошибка входа пользователя ""."
строка:11 знак:1
+ $SqlConnection.Open();
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SqlException
Translations is as follows:
Can not find the type of [System.Data.SqlClient.SqlCredential]: make sure that the assembly that contains this type is loaded
Exception when calling the "Open" from the "0" argument: "Login failed for user ''."
I used sa and 1 for the user and pass.