C # SqlException was unhandled error: 26 - Error Locating Server / Instance Specified

1

I have been developing since the beginning of December 2016 a project to register tool loan vouchers for my school and I have used an sql database to keep the record. I added a local sql database following the following youtube tutorial:

"Insert Queries Modify Delete and Search in VB 2010 and 2008" (the video lasts 17 minutes with 45 seconds I can not add the link because I have low level in the forum, besides that in the video the author programs in C ++ and I program in C #)

And after having handled the queries (the Queries) of the database (which work correctly) I wanted to add a form to support the database following the following tutorial:

link

When I tried to do a backup, I did not do anything, and then I put the try and catch statements as a comment to see the error when trying again, the following error appeared:

  

SqlException was unhandled

     

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)

and I leave the screenshot of the error: then I put the complete code of the form:

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;

namespace Base_de_datos_ITQ
{
    public partial class Respaldo_duv : Form
    {
        SqlConnection con = new SqlConnection(Base_de_datos_ITQ.Properties.Settings.Default.Base_de_datosConnectionString);
        public Respaldo_duv()
        {
            InitializeComponent();
        }

        private void button_duv_examinar_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialogo = new FolderBrowserDialog();
            if (dialogo.ShowDialog() == DialogResult.OK)
            {
                textBox_duv_dirección_respaldo.Text = dialogo.SelectedPath;
            }
        }

        private void button_duv_respaldar_Click(object sender, EventArgs e)
        {
            string database = con.Database.ToString();
            //try
            {
                if (textBox_duv_dirección_respaldo.Text == string.Empty)
                {
                    MessageBox.Show("please enter backup file location");
                }
                else
                {
                    string cmd = "BACKUP DATABASE [" + database + "] TO DISK='" + textBox_duv_dirección_respaldo.Text + "\" + "database" + "-" + DateTime.Now.ToString("yyyy-MM-dd--HH-mm-ss") + ".bak'";

                    using (SqlCommand command = new SqlCommand(cmd, con))
                    {
                        if (con.State != ConnectionState.Open)
                        {
                            con.Open();//aquí es donde se marca el error
                        }
                        command.ExecuteNonQuery();
                        con.Close();
                        MessageBox.Show("database backup done successefully");
                    }
                }

            }
            //catch
            {

            }
        }
    }
}

Note that it is written in C #.

I must add that I am studying electronic engineering and not systems or computing, therefore I am not in my field (normally what I program are microcontrollers with C ++, that is, low level languages) and it is the first time I use databases sql. I give thanks in advance to all who respond.

    
asked by Manuel duv 15.03.2017 в 19:58
source

2 answers

1

According to the comment where you indicate the route of the database to which you are connecting, I can deduce that you are working with SQL Compact Edition , this due to the file extension .sdf . This means that to create the connection you can not use SqlConnection it would be correct if you would use SqlCeConnection which is the appropriate driver for this type of database. The same goes for class SqlCommand you should use SqlCeCommand .

EXAMPLE:

SqlCeConnection conn = null;

try
{
    conn = new SqlCeConnection("Data Source = MyDatabase.sdf; Password ='<pwd>'");
    conn.Open();

    SqlCeCommand cmd = conn.CreateCommand();
    cmd.CommandText = "INSERT INTO Customers ([Customer ID], [Company Name]) Values('NWIND', 'Northwind Traders')";

    cmd.ExecuteNonQuery();
}
finally
{
    conn.Close();
}

Now, I think that the BACKUP DATABASE.... instruction you're trying to use to perform Backup is not going to work either, because this is an instruction for SQL Server or SQL LocalDB.

To Backup to an .sdf file, just copy it, just like any other file on the disk, all you have to ensure before copying it is that no user is connected to the database at that time.

At the end of this reference link you can find what I'm telling you.

    
answered by 15.03.2017 / 20:48
source
0

The error that shows you basically means that it does not find the database, so we can assume in principle that there is something wrong in the connection string. On the other hand I see that you use a database .sdf that corresponds to SQL Server Compact Edition so it is not correct to use

  

System.Data.SqlCommand

This is used when you connect to a database that is running on the MSSQL engine installed on the server.

To use SQL Server Compact Edition you should use

  

System.Data.SqlServerCe

I do not understand how other queries have worked on a .sdf basis. You used System.Data.SqlCommand .

Anyway, since it is a file of type .sdf , .bak is not generated for the backups as you would in MS SQL Server traditional is to say with an instance of MSSQL running on the server.

These simpler databases as they simply correspond to a file, just make a copy of it and save it wherever you want.

    
answered by 15.03.2017 в 20:50