C #: The name 'database' does not exist in the current context (database is the name of a local sql database type .sdf)

1

My project has a local sql database to register tool loan vouchers and has many tables. In the form presented in the following image, I intend to develop a routine to support the information in the database following this tutorial .

For some reason, the project does not recognize the name of the database and I get the following error (the error is also shown in the image above):

  

The name 'base_data' does not exist in the current context

On the next line in the section that says Base_de_datos :

SqlConnection conexion = new SqlConnection(/*Inicia error*/Base_de_datos/*termia error*/.Properties.Settings.Default.Base_de_datosConnectionString);

Code :

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 conexion = new SqlConnection(/*Inicia error*/Base_de_datos/*termia error*/.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 Conexion_Base_de_datos = conexion.Database.ToString();
            //try
            {
                if (textBox_duv_dirección_respaldo.Text != "")
                {
                    string comando_sql = "BACKUP DATABASE [" + Conexion_Base_de_datos + "] TO DISK '" + textBox_duv_dirección_respaldo.Text + "\Respaldo de base de datos del " + DateTime.Now.ToString() + ".bak'";

                    using (SqlCommand comando = new SqlCommand(comando_sql, conexion))
                    {
                        if (conexion.State != ConnectionState.Open)
                        {
                            conexion.Open();
                        }
                        comando.ExecuteNonQuery();
                        conexion.Close();
                        MessageBox.Show("Respaldo realizado correctamente");
                    }
                }
                else
                {
                    MessageBox.Show("Seleccione una dirección para el respaldo");
                }
            }
            //catch
            {
            //    MessageBox.Show("Error al respaldar, comprueve la dirección");
            }
        }
    }
}

In other sections of the project I have used database queries without problems, such as in the form code I use to register students:

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

namespace Base_de_datos_ITQ
{
    public partial class Base_de_datos_de_alumnos : Form
    {
        public Base_de_datos_de_alumnos()
        {
            InitializeComponent();
        }

        private void Base_de_datos_de_alumnos_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'base_de_datosDataSet.Base_de_datos_de_los_alumnos' table. You can move, or remove it, as needed.
            this.base_de_datos_de_los_alumnosTableAdapter.Fill(this.base_de_datosDataSet.Base_de_datos_de_los_alumnos);

        }

        private void Base_de_datos_de_alumnos_Load_1(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'base_de_datosDataSet.Base_de_datos_de_los_alumnos' table. You can move, or remove it, as needed.
            this.base_de_datos_de_los_alumnosTableAdapter.Fill(this.base_de_datosDataSet.Base_de_datos_de_los_alumnos);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.base_de_datos_de_los_alumnosTableAdapter.FillBy(this.base_de_datosDataSet.Base_de_datos_de_los_alumnos, toolStripTextBox_duv_texto_a_filtrar_alumnos.Text);
        }

        private void button_duv_guardar_cambios_alumnos_Click(object sender, EventArgs e)
        {

            this.button_duv_alterar_valor_alumnos.Enabled = true;           // Se habilita el boton "Alterar valor seleccionado"
            this.groupBox_duv_datos_nuevos_alumnos.Enabled = false;         // Se deshabilita el grupbox "Datos nuevos"
        }

        private void button_duv_cancelar_cambios_alumnos_Click(object sender, EventArgs e)
        {
            this.button_duv_alterar_valor_alumnos.Enabled = true;           // Se habilita el boton "Alterar valor seleccionado"
            this.groupBox_duv_datos_nuevos_alumnos.Enabled = false;         // Se deshabilita el grupbox "Datos nuevos"
        }

        private void button_duv_alterar_valor_alumnos_Click(object sender, EventArgs e)
        {
            if (textBox_duv_NC_alumnos.Text == "")
            {
                textBox_duv_NC_alumnos.Text = "Nulo";
            }
            if (textBox_duv_N_alumnos.Text == "")
            {
                textBox_duv_N_alumnos.Text = "Nulo";
            }
            if (textBox_duv_AP_alumnos.Text == "")
            {
                textBox_duv_AP_alumnos.Text = "Nulo";
            }
            if (textBox_duv_AM_alumnos.Text == "")
            {
                textBox_duv_AM_alumnos.Text = "Nulo";
            }
            if (textBox_duv_Tel_alumnos.Text == "")
            {
                textBox_duv_Tel_alumnos.Text = "Nulo";
            }
            if (textBox_duv_TelE_alumnos.Text == "")
            {
                textBox_duv_TelE_alumnos.Text = "Nulo";
            }
            if (textBox_duv_CE_alumnos.Text == "")
            {
                textBox_duv_CE_alumnos.Text = "Nulo";
            }
            if (comboBox_duv_C_alumnos.Text == "")
            {
                comboBox_duv_C_alumnos.Text = "Nulo";
            }
            this.base_de_datos_de_los_alumnosTableAdapter.Editar_duv_alumnos(
                textBox_duv_NC_alumnos.Text,
                textBox_duv_N_alumnos.Text,
                textBox_duv_AP_alumnos.Text,
                textBox_duv_AM_alumnos.Text,
                textBox_duv_Tel_alumnos.Text,
                textBox_duv_TelE_alumnos.Text,
                textBox_duv_CE_alumnos.Text,
                comboBox_duv_C_alumnos.Text,
                número_de_controlTextBox.Text,
                nombre_s_TextBox.Text,
                apellido_PaternoTextBox.Text,
                apellido_MaternoTextBox.Text,
                teléfonoTextBox.Text,
                teléfono_de_emergenciaTextBox.Text,
                correo_electrónicoTextBox.Text);
            this.base_de_datos_de_los_alumnosTableAdapter.Fill(this.base_de_datosDataSet.Base_de_datos_de_los_alumnos);
            this.radioButton_deshabilitar_cambios_alumnos.Checked = true;
        }

        private void radioButton_deshabilitar_cambios_alumnos_CheckedChanged(object sender, EventArgs e)
        {
            // Se borran los valores en el grupbox "Datos nuevos"
            this.textBox_duv_NC_alumnos.Text = "";
            this.textBox_duv_N_alumnos.Text = "";
            this.textBox_duv_AP_alumnos.Text = "";
            this.textBox_duv_AM_alumnos.Text = "";
            this.textBox_duv_Tel_alumnos.Text = "";
            this.textBox_duv_TelE_alumnos.Text = "";
            this.textBox_duv_CE_alumnos.Text = "";
            this.comboBox_duv_C_alumnos.Text = "";

            this.groupBox_duv_datos_nuevos_alumnos.Enabled = false;
            this.button_duv_alterar_valor_alumnos.Enabled = false;
            this.button_duv_borror_dato_alumnos.Enabled = false;
            this.button_duv_crear_valor_alumnos.Enabled = false;
            this.base_de_datos_de_los_alumnosDataGridView.Enabled = true;
        }

        private void radioButton_duv_habilitar_edición_CheckedChanged(object sender, EventArgs e)
        {
            // Se copia la información del grupbox "Datos anteriores" al grupbox "Datos nuevos"
            this.textBox_duv_NC_alumnos.Text = número_de_controlTextBox.Text;
            this.textBox_duv_N_alumnos.Text = nombre_s_TextBox.Text;
            this.textBox_duv_AP_alumnos.Text = apellido_PaternoTextBox.Text;
            this.textBox_duv_AM_alumnos.Text = apellido_MaternoTextBox.Text;
            this.textBox_duv_Tel_alumnos.Text = teléfonoTextBox.Text;
            this.textBox_duv_TelE_alumnos.Text = teléfono_de_emergenciaTextBox.Text;
            this.textBox_duv_CE_alumnos.Text = correo_electrónicoTextBox.Text;
            this.comboBox_duv_C_alumnos.Text = carreraComboBox.Text;

            this.groupBox_duv_datos_nuevos_alumnos.Enabled = true;
            this.button_duv_alterar_valor_alumnos.Enabled = true;
            this.button_duv_borror_dato_alumnos.Enabled = true;
            this.button_duv_crear_valor_alumnos.Enabled = false;
            this.base_de_datos_de_los_alumnosDataGridView.Enabled = false;
        }

        private void radioButton_duv_habilitar_dato_nuevo_CheckedChanged(object sender, EventArgs e)
        {
            // Se borran los valores en el grupbox "Datos nuevos"
            this.textBox_duv_NC_alumnos.Text = "";
            this.textBox_duv_N_alumnos.Text = "";
            this.textBox_duv_AP_alumnos.Text = "";
            this.textBox_duv_AM_alumnos.Text = "";
            this.textBox_duv_Tel_alumnos.Text = "";
            this.textBox_duv_TelE_alumnos.Text = "";
            this.textBox_duv_CE_alumnos.Text = "";
            this.comboBox_duv_C_alumnos.Text = "";

            this.groupBox_duv_datos_nuevos_alumnos.Enabled = true;
            this.button_duv_alterar_valor_alumnos.Enabled = false;
            this.button_duv_borror_dato_alumnos.Enabled = false;
            this.button_duv_crear_valor_alumnos.Enabled = true;
            this.base_de_datos_de_los_alumnosDataGridView.Enabled = false;
        }

        private void button_duv_crear_valor_alumnos_Click(object sender, EventArgs e)
        {
            if(textBox_duv_NC_alumnos.Text == "")
            {
                textBox_duv_NC_alumnos.Text = "Nulo";
            }
            if(textBox_duv_N_alumnos.Text == "")
            {
                textBox_duv_N_alumnos.Text = "Nulo";
            }
            if(textBox_duv_AP_alumnos.Text == "")
            {
                textBox_duv_AP_alumnos.Text = "Nulo";
            }
            if(textBox_duv_AM_alumnos.Text == "")
            {
                textBox_duv_AM_alumnos.Text = "Nulo";
            }
            if(textBox_duv_Tel_alumnos.Text == "")
            {
                textBox_duv_Tel_alumnos.Text = "Nulo";
            }
            if(textBox_duv_TelE_alumnos.Text == "")
            {
                textBox_duv_TelE_alumnos.Text = "Nulo";
            }
            if(textBox_duv_CE_alumnos.Text == "")
            {
                textBox_duv_CE_alumnos.Text = "Nulo";
            }
            if(comboBox_duv_C_alumnos.Text == "")
            {
                comboBox_duv_C_alumnos.Text = "Nulo";
            }
            this.base_de_datos_de_los_alumnosTableAdapter.Insert(textBox_duv_NC_alumnos.Text, textBox_duv_N_alumnos.Text, textBox_duv_AP_alumnos.Text, textBox_duv_AM_alumnos.Text, textBox_duv_Tel_alumnos.Text, textBox_duv_TelE_alumnos.Text, textBox_duv_CE_alumnos.Text, comboBox_duv_C_alumnos.Text,"No deudor");
            this.base_de_datos_de_los_alumnosTableAdapter.Fill(this.base_de_datosDataSet.Base_de_datos_de_los_alumnos);
            this.radioButton_deshabilitar_cambios_alumnos.Checked = true;
        }

        private void button_duv_borror_dato_alumnos_Click(object sender, EventArgs e)
        {
            this.base_de_datos_de_los_alumnosTableAdapter.Borrar_fila_duv_alumnos(
                número_de_controlTextBox.Text,
                nombre_s_TextBox.Text,
                apellido_PaternoTextBox.Text,
                apellido_MaternoTextBox.Text,
                teléfonoTextBox.Text,
                teléfono_de_emergenciaTextBox.Text,
                correo_electrónicoTextBox.Text);
            this.base_de_datos_de_los_alumnosTableAdapter.Fill(this.base_de_datosDataSet.Base_de_datos_de_los_alumnos);
            this.radioButton_deshabilitar_cambios_alumnos.Checked = true;
        }

        private void toolStripTextBox_duv_texto_a_filtrar_alumnos_TextChanged(object sender, EventArgs e)
        {
            if (checkBox_duv_filtro_continuo.Checked)
            {
                this.base_de_datos_de_los_alumnosTableAdapter.FillBy(this.base_de_datosDataSet.Base_de_datos_de_los_alumnos, toolStripTextBox_duv_texto_a_filtrar_alumnos.Text);
            }
        }
    }
}

Thanks in advance.

    
asked by Manuel duv 13.03.2017 в 23:44
source

3 answers

0

If I can see on your screenshot you are referencing Base_de_datos.Properties.Settings... etc. When your namespace is Base_de_datos_ITQ . That's what the error is telling you, that you do not find that namespace.

    
answered by 14.03.2017 / 13:03
source
1

I recommend that you manage it through a web.config:

<connectionStrings>
    <add name ="NorthWind"
    connectionString="data source=|DataDirectory|\Nw40.sdf" />
</connectionStrings>

In the class:

conn.ConnectionString = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
conn.Open();
    
answered by 14.03.2017 в 00:05
0

the connection path is in your app.config, search there ... or right click on your solution.

in the Name it would take the name of your string so I see it would be'Base_of_data 'in type: "string" and in Scope: Application, finally the value that is where the path should lead (connection string); what would be something like this:

  

"Persist Security Info = False; User ID = *****; Password = *****; Initial Catalog = tudb;"

    
answered by 14.03.2017 в 00:00