Reputation: 7004
I'm having problems setting up a super-simple SQL database project in Visual Studio 2010.
Upon opening the connection to a local SQL database I get the following error message:
A network-related or instance-specific error occurred while establishing a connection to 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)
All my google searching comes up with network related solutions, checking firewall settings or configuring remote connections. But this is just a local sdf file.
I've literally just created a new blank WPF project. Right clicked and "Add New Item" chosen "Local Database" clicked next to produce a Database1.sdf. I can access this in the Server View in VS2010, add tables etc, and crucially, get the connection string.
I then create the connection using said string:
using System.Data.SqlClient;
namespace SQLTest
{
<summary>
Interaction logic for MainWindow.xaml
</summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
try
{
SqlConnection conn = new SqlConnection(@"Data Source=D:\Projects\SQLTest\SQLTest\Database1.sdf");
conn.Open();
MessageBox.Show(conn.ServerVersion);
}
catch (SqlException e)
{
MessageBox.Show(e.Message);
}
}
}
}
But it fails with the above error on "conn.Open()". I'm sure I've created local databases like this before, and all my googling comes back with people with actual network problems. Nothing for local databases.
I'm at a loss, wondering if following these steps results in the same thing for other people? I've had a colleague do the same thing and the same error occurs.
Upvotes: 1
Views: 1785
Reputation: 1
All your SQL commands need to include Ce example: SqlDataReader = SqlCeDataReader SqlCommand = SqlCeCommand and you need to remove using System.Data.SqlClient; and add using System.Data.SqlServerCe;
Upvotes: 0
Reputation: 91
Select the server in the server explorer and press f4 which will give you the properties one of which is the connection string you can cut and paste into ur code.
Upvotes: 0
Reputation: 14389
As concluded from your connection string you are trying to connect to an SQL CE Database. However you use SqlConnection
for that.
Instead use SqlCeConnection
by adding a reference to System.Data.SqlServerCe.dll
Upvotes: 3