Tuesday, February 24, 2015

How do you create a database in SQL Server using Power Shell?

Review this previous post    http://hodentekmsss.blogspot.com/2014/11/accessing-sql-server-via-smo-using.html

In the above post you will get an introduction to Power Shell and SQL Server management Objects. You also learn how to access the SQL Server and carry out a few tasks like listing out all the databases.

In this post you will learn how to create a new database in SQL Server using SMO and Power Shell. Using Transact-SQL or the SSMS you can easily create a database. Using PowerShell is another option to create a database using a script.

Launch Power Shell by right clicking the instance node as shown.


PS_SMO_DB01

This ensures that you have access to the SQLServer's SMO library.

You will be launching the shell with the following line:
PS SQLSERVER:\SQL\HODENTEK8\REGENCYPARK>

Enter the following code one line at a time. After you enter the line and click enter you may not get any message. Continue with the other lines.

$server= new-object Microsoft.Sqlserver.management.smo.server 'Hodentek8\RegencyPark'
$dbname= "Feb6"
$db=new-object  Microsoft.Sqlserver.management.smo.Database ($server,$dbname)
$db.Create()


When the final command line is processed you will have created a new database Feb6 in your SQL Server Instance as shown. It will have default settings.


PS_SMO_DB02

How can I connect to an Microsoft EXCEL file on my Windows 8.1 laptop?

If you have Office running on your laptop it should be immediately possible and you would not be asking this question. Although you do not have Office sometimes you may want to connect to it.
For example you may use a Export/Import wizard to create a Excel file from the data on your SQL server. In such cases you come up with the following error:


Excel11

You will find that there are no ODBC (x64) drivers on your machine which will enable to correct this error and you need to download and register them on your computer. Open ODBC Manager on your computer and verify.
 

Internet search reveals that you can download the Access driver (Microsoft Access Database Engine 2010 Redistributable) from here:
http://www.microsoft.com/en-us/download/details.aspx?id=13255


AccessDriver

The details of this driver are shown here from the Microsoft site. This installation will provide drivers for office applicaitions including MS ACCESS and MS EXCEL.

Download and install the AccessDatabaseEngine_x64.exe file and double click the file to run the program.
Excel 22

This will install the Microsoft EXCEL and Microsoft ACCESS drivers needed for connectivity.