0

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.

2
  • The constructor doesn't take secure strings, it takes plaintext. Commented Jan 22, 2016 at 11:08
  • Also - your second attempt has failed because you didn't load the assembly the second time around. Commented Jan 22, 2016 at 12:39

2 Answers 2

2

I know this is an old question, but just in case someone stumbles across it.

The previous answer was almost correct. You do need to pass the user id and and password to the constructor, but the password must be a secure string.

$SecurePassword = ConvertTo-SecureString -AsPlainText -Force "ThePassword"
$creds = New-Object System.Data.SqlClient.SqlCredential('UserName', $SecurePassword)
Sign up to request clarification or add additional context in comments.

Comments

0

From what I can tell, the constructor for that type requires a user id and password - you can't create it without specifying them - so you'd need to call it like this :

$creds = New-Object System.Data.SqlClient.SqlCredential('UserName','Password')

Obviously replacing those with your actual username and password.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.