brian3415
brian3415

Reputation: 65

Cant create service based database in Visual Studio 2010

I am trying to learn C# and then I hit this roadblock. My Visual Studio 2010 wont let me create a service based database (the .mdf file). (Add>New Item>Service-Based Database)

Everytime I try to add new .mdf it would bring up an error.

A network-related or instance-specific error occurred while establishing a connection to the SQL server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

I can create a Local Database (Add>New Item>Local Database) without any problem, but I cant create .mdf

I am running on Windows 7 64bit OS and Visual Studio 2010 (Ultimate).

Upvotes: 0

Views: 9459

Answers (4)

Rashmi Prabha
Rashmi Prabha

Reputation: 49

To temporarily attach a database file (.mdf) from the Data Connections node

  1. In Server Explorer, open the short cut menu for Data Connections and choose Add Connection. The Add Connection dialogue box appears.
  2. Choose the Change button. The Change Data Source dialogue box appears.
  3. Select Microsoft SQL Server Database File and choose the OK button. The Add Connection dialogue box reappears, with Microsoft SQL Server Database File (SqlClient) shown in the Data source text box.
  4. Choose the Browse button and browse to an existing .mdf file. If you type a file name that does not exist, a blank .mdf file will be created.
  5. Select either Use Windows Authentication or Use SQL Server Authentication. For more information on SQL Server database access authentication, see Create New SQL Server Database Dialogue Box.
  6. Choose the OK button. The database appears in Server Explorer.

Upvotes: 0

Razin Bashar
Razin Bashar

Reputation: 159

You can try

  1. Go to Command Prompt from Start->Run(type cmd -> press enter)
  2. Check SqlLocalDB by

    Sqllocaldb.exe i

    You will see version number like (v11.0) otherwise complete the installation.

  3. Create the sqldb by

    sqllocaldb c dbname

    You will see like "LocalDB instance "dbname" created with version 11.0."

  4. Then Start the Db

    sqllocaldb s dbname

    You will see like "LocalDB instance "dbname" started"

  5. See the created Db info

    sqllocaldb.exe i dbname

Now try to create service-based database in c#.

Upvotes: 0

Basic requirements for using these two kinds of databases:

  • Local Database (.sdf): requires SQL Server Compact. That is, you need only an additional DLL, but no special system service. (If you know SQLite: SQL Server Compact is conceptually the same thing, only more powerful and feature-complete.)

  • Service-Based Database (.mdf & .ldf log/journal file): requires a running instance of SQL Server or SQL Server Express, both of which are standalone products (as Tim Copenhaver points out in his answer). For development purposes, you'd typically install one of these products on your development machine.

Likely cause of your problem: Guessing from the error message that you posted, you do not have SQL Server, nor SQL Server Express, installed. However, an instance of one of these products is required so that you can "attach" your database to it. This is required in order before you can actually access the .mdf database.

To see whether you have SQL Server installed, you could e.g. open Control Panel → Administrative Tools → Services, and look out for a system service named "SQL Server (.\SQLEXPRESS)", or "SQL Server (YOURCOMPUTERNAME\MSSQLSERVER)", or similar. The name in the brackets is the name of the database instance running on your machine. SQLEXPRESS is the default instance name for SQL Server Express, while MSSQLSERVER is the default instance name for the full version of SQL Server.

If you do not have SQL Server installed, you can download e.g. SQL Server 2012 Express or SQL Server 2008 R2 Express with SP2 for free from Microsoft.

Upvotes: 1

Tim Copenhaver
Tim Copenhaver

Reputation: 3302

A service-based database needs to connect to a SQL Server instance. It's not built into Visual Studio, it's a separate application which needs to be running somewhere for you to connect to. That error message is basically telling you it can't find the SQL Server instance you're trying to connect to.

Local Database works correctly because it keeps everything local to your application and doesn't require an external database server to function.

Upvotes: 1

Related Questions