7

Hi I'm trying to connect an SQL server compact database to my program and I want a button that deletes all entries from the database, when I Press said button the program throws an exception and gives 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)"

Help Please? =]

Sorry, Code is Below =]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.SqlServerCe;


namespace Booking_system_Final
{
    public partial class PendingJobs : Form
    {
        SqlConnection sc = new SqlConnection("Data Source=C:\\Users\\Administrator\\My Documents\\BMS_Data.sdf");
        public PendingJobs()
        {
            InitializeComponent();
        }

        private void PendingJobs_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'bMSDataSet.Bookings' table. You can move, or remove it, as needed.
            this.bookingsTableAdapter.Fill(this.bMSDataSet.Bookings);
            // TODO: This line of code loads data into the 'bMS_DataDataSet1.Bookings' table. You can move, or remove it, as needed.


        }

        private void button1_Click(object sender, EventArgs e)
        {
            sc.Open();
            SqlCommand cmd = new SqlCommand("DELETE FROM Bookings");
            cmd.Connection = sc;
            cmd.ExecuteNonQuery();
            sc.Close();
            MessageBox.Show("Database Cleared");


        }
    }
}
7
  • 3
    Well let's see some code then. Also, can you open it up in SQL Server Management Studio? Commented Mar 1, 2013 at 12:09
  • Are you passing correct credentials, pointing to correct SQL server instance and Database? Commented Mar 1, 2013 at 12:15
  • I'm pointing to a locally stored SQL server Compact database and the directory name is correct Commented Mar 1, 2013 at 12:17
  • This usually because the sdf file does not exist. Does it? Commented Mar 1, 2013 at 12:22
  • Yeah it does I checked the Directory in Windows Explorer and the SDF file is there and the Connection String Points to the file directly, I can't see where I've gone wrong here Commented Mar 1, 2013 at 12:27

4 Answers 4

10

Try use SqlCeConnection class rather than SqlConnection:

SqlCeConnection sqlConnection1 = new SqlCeConnection();
sqlConnection1.ConnectionString = "Data Source = C:\\Users\\Administrator\\My Documents\\BMS_Data.sdf;Persist Security Info=False";
Sign up to request clarification or add additional context in comments.

1 Comment

How to modify the data source in case of LAN? Let's say, the .sdf is located on shared network as \\xyz-PC\db\dbfile.sdf
6

If you want to connect to SQL Server Compact, use SqlCeConnection, SqlCeCommand etc. Add a reference to the SQL Server Compact ADO.NET provider, System.Data.SqlServerCe.dll

Comments

0

Have a look at this blog article: SQL SERVER – FIX : ERROR : (provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server)

This goes step-by-step through what you might need to do:

In short:

  1. SQL Server should be up and running.
  2. Enable TCP/IP in SQL Server Configuration
  3. Open Port in Windows Firewall
  4. Enable Remote Connection
  5. Enable SQL Server Browser Service
  6. Create exception of sqlbrowser.exe in Firewall
  7. Recreate Alias

About the where and what to do in each step, you will find more in-depth information in the article.

You may also want to have a look at the Connection strings for SQL Server Compact. There you can find other variations of the connection string you could try to play with.

2 Comments

thanks for the detailed answer, I have found the source of the problem which is that SQL server for some reason cannot connect and I am now getting an error saying "the remote procedure call failed"
@ReeceCottam: I am not completely familiar with this, is a remote procedure the same as a stored procedure? If so, this is a feature that might possibly NOT supported by the CE edition: Have a look at the bottom of this feature comparison table: (Comparison of SQL Server Compact, SQL Server Express 2012 and LocalDB )[erikej.blogspot.de/2011/01/… where it says "Stored procedures, views, triggers: No"
0

You seems to be using a bad connection string. (Or the file path is wrong). Check out http://www.connectionstrings.com/sql-server-ce for connection string options.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.