FreeKB - Visual Studio Getting Started with Microsoft SQL Server in Visual Studio
Visual Studio - Getting Started with Microsoft SQL Server in Visual Studio

This assumes that you already have Microsoft SQL Server installed. If you are using the Community Edition of Visual Studio, only the Express version of SQL Server will work.

  1. In Visual Studio, in the right panel (solution explorer), right-click the name of our site and select Add > Add ASP.NET Folder > App_Data or select Add > New Folder, and name the new folder App_Data.
  2. With the App_Data folder highlighted, press Ctrl + Shift + A (or right-click and select Add > New Item).
  3. In the Add New Item dialog box, select C# or Visual Basic > Data >SQL Server Database. Select Add.

Note: If you get error Connections to SQL Server database files (.mdf) require LocalDB or SQL Server Express, go here.

  1. In Solution Explorer should now be a file named database1.mdf. Right-click database1.mdf and select Open. A new tab appears in the left panel titled Server Explorer.
  2. In Server Explorer, database1.mdf should have a red X. This means the database has not yet been configured. Right-click database1.mdf and select Modify Connection.
  3. In the Modify Connection dialog box, with Use Windows Authentication selected, select Test Connection. If the connection is successful, the database1.mdf file should no longer have the red X and should instead have the plug icon. Expanding Database1.mdf will show the available SQL Server folders and files.

Follow these steps to use SQL Server Authentication:

  1. In Service Explorer, right-click database1.mdf and select Modify Connection. Do the following:
    1. Select Change and select Microsoft SQL Server (SqlClient)
    2. Enter the name of your SQL Server (example: server1\\database1)
    3. Select SQL Server Authentication and enter your SQL Server username and password
    4. Select Test Connection. The connection should be successful.  In Server Explorer, the file should no longer be named Database1.mdf. Instead, in this example, the file would be named server1\\database1.master.dbo. In the App_Data folder, the file is still named Database1.mdf.

Follow these steps to add additional SQL Server database connections to Visual Studio:

  1. In the list of small icon at the top of Server Explorer, select the Connect to Database icon.
  2. In the Choose Data Source dialog box, select Microsoft SQL Server and select Continue
  3. In the Add Connection dialog box do the following:
    1. The server name in Visual Studio needs to be exact the same as the server name being uses when connecting to SQL Server
    2. Select SQL Server Authentication. Enter your SQL Server Authentication username and password.
    3. Select a Database
    4. Click the Test Connection button to ensure the connection is successful
    5. OK

Follow these directions to show the data in SQL Server on a Web page.

  1. On one of our pages, go to Design View.  In Server Explorer, expand Tables and drag and drop one of your tables into Design View.
  2. In the Grid View tasks Window, check-mark Enable Paging, Enable Sorting and Enable Selection.

If we view our page in a Web browser, our SQL data will be displayed.

  1. In the top menu of Visual Studio, select Tools > Options
  2. In the Options dialog box, select Database Tools > Data Connections
  3. ??? (LocalDB)\\MSSQLLocalDB

Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter b58d3 in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |