1

I'm trying to create a SQL Server database, and I get this error

Exception calling ".ctor" with "2" arguments error.

I'm also supposed to create a table from a csv, but I'm executing the code in segments first to make sure I'm doing it right.

Import-Module -Name sqlps -DisableNameChecking
$srv = New-Object Microsoft.SqlServer.Management.Smo.Server("(localhost)")
$db = New-Object Microsoft.SqlServer.Management.Smo.Database($srv, "Test_Database")
$db.Create()
Write-Host $db.Name "created" $db.CreateDate
2
  • The "local" machine in SQL Server can be specified as . (just a dot), (local) (with the parenthesis), or localhost (without any parens) - your spelling of (localhost) is not valid Commented Jun 22, 2019 at 18:38
  • this did it! Thank you Commented Jun 22, 2019 at 23:24

2 Answers 2

2

My guess is that there is a issue with server instance name:

$srv = New-Object Microsoft.SqlServer.Management.Smo.Server("(localhost)")
=>
$srv = New-Object Microsoft.SqlServer.Management.Smo.Server(".")
Sign up to request clarification or add additional context in comments.

1 Comment

So I believe the instance name is UCERTIFY3 and I've replaced localhost with that. I'm pretty much learning SQL for school and I'm definitely not experienced in it.
0

If the instance is the default instance, then you can use the following:

$srv = New-Object Microsoft.SqlServer.Management.Smo.Server
$db = New-Object Microsoft.SqlServer.Management.Smo.Database($srv, "Test_Database")
$db.Create()

For the named instance, UCERTIFY3, you can use the following syntax:

$srv = New-Object Microsoft.SqlServer.Management.Smo.Server("SERVERNAME\UCERTIFY3")
$db = New-Object Microsoft.SqlServer.Management.Smo.Database($srv, "Test_Database")
$db.Create()

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.