Using Microsoft SQL Server on Windows

Microsoft offers various editions of its SQL Server product. We will use a free version that can run stand-alone, known as LocalDb. The latest version of LocalDb is installed as part of Visual Studio 2017.

Note

Microsoft SQL Server used to be a Windows-only RDBMS. In 2017, Microsoft plans to release a version for Linux that will be great for cross-platform .NET Core developers. A preview is available, but we will not cover it in this book.

Connecting to Microsoft SQL Server LocalDb

When you write code to connect to a SQL Server database, you need to know its server name. The name depends on the version you choose to use. Here are some examples:

  • Visual Studio 2017 installs SQL Server 2016: (localdb)mssqllocaldb
  • Visual Studio 2015 installs SQL Server 2014: (localdb)mssqllocaldb
  • Visual Studio 2012/2013 installs SQL Server 2012: (localdb)v11.0
  • If you install SQL Server Express: .sqlexpress

Creating the Northwind sample database

In Visual Studio 2017, go to File | Open | File... or press Ctrl + O .

Browse to select the Northwind.sql file and choose Open.

In the editor window, right-click and choose Execute... or press Ctrl + Shift + E.

In the dialog box, enter the server name as (localdb)mssqllocaldb and click on Connect, as shown in the following screenshot:

Creating the Northwind sample database

When you see the Command(s) completed successfully message, then the Northwind database has been created, and we can connect to it.

Note

LocalDb, sometimes, takes too long to start the first time, and you might see a timeout error. Simply click on Connect again, and it should work.

Managing the Northwind sample database

In Visual Studio 2017, choose View | Server Explorer... or press Ctrl + W, L.

In the Server Explorer window, right-click on Data Connections and choose Add Connection...

If you see the Choose Data Source dialog, as shown in the following screenshot, then select Microsoft SQL Server and click on Continue:

Managing the Northwind sample database

In the Add Connection dialog box, enter the server name as (localdb)mssqllocaldb, enter the database name as Northwind, and click on OK, as shown in the following screenshot:

Managing the Northwind sample database

In the Server Explorer window, expand the data connection and its tables. You should see a dozen tables, including the Products table, as shown in the following screenshot:

Managing the Northwind sample database

Right-click on the Products table and choose Show Table Data, as shown in the following screenshot:

Managing the Northwind sample database

To see the details of the Products table columns and types, right-click on Products and choose Show Table Definition, as shown in the following screenshot:

Managing the Northwind sample database

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.38.121