I have the following code:
public static int Guardar(string codigo, string nombre, string apellido)
{
Clase_Conexion conexion = new Clase_Conexion();
int retorno = 0;//existe codigo. Producto repetido
String sql="INSERT into Table (codigo, nombre, apellido) VALUES (@codigo, @nombre, @apellido)";
//String cstring = ConfigurationManager.ConnectionStrings["conecta"].ConnectionString;
//using (SqlConnection connection = new SqlConnection(cstring))
//{
using (SqlCommand command = new SqlCommand())
{
command.Connection = conexion.con; // <== lacking
command.CommandType = CommandType.Text;
command.CommandText = "INSERT into Table (codigo, nombre, apellido) VALUES (@codigo, @nombre, @apellido)";
command.Parameters.AddWithValue("@codigo", codigo);
command.Parameters.AddWithValue("@nombre", nombre);
command.Parameters.AddWithValue("@apellido", apellido);
try
{
conexion.conectar();
int recordsAffected = command.ExecuteNonQuery();
retorno = 1;
}
catch (SqlException)
{
// error here
}
finally
{
conexion.cerrar();
}
}
//}
return retorno;
}
And I have the following connection class:
public class Clase_Conexion
{
public SqlConnection con;
public void conectar()
{
con = new SqlConnection("data source = localhost; initial catalog = BD_ejemplo"); // LOCALHOST
con.Open();
}
//metodo para cerrar
public void cerrar()
{
con.Close();
}
}
I have a database called BD_ejemplo.mdf
in App_Data
And in the web.config I have the following conectionString
of the following form:
<add name="conecta" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-ejemplo_ajax_webform-20170317135041;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-ejemplo_ajax_webform-20170317135041.mdf" />
Firstly I was using the connection through the web.config, but it did not work out because it said it could not find the database path. Later I decided to do it through a connection class, but I can not either, since I get the following error:
Error related to the network or specific to the instance while establishing a connection to the SQL Server. The server was not found or it was not accessible. Verify that the name of the instance is correct and that SQL Server is configured to support remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection with SQL Server)
I would like to know how I make the connection both through the web.config and especially with the connection file (class). I know that something is badly written, or some parameter I need to enter in:
con = new SqlConnection("data source = localhost; initial catalog = BD_ejemplo") //(para el caso de la clase de conexion)
I hope I can find the solution.
Observation: For now I am without a password to the database. Since you add from the same Visual Studio.
I have checked the connection and it is correct. Here I show the following:
The name of the database file is visual studio 2013 \ Projects \ example_ajax_webform \ example_ajax_webform \ App_Data \ BD_example.dmdf
And in Advance, there is a part that says:
Data Source = (LocalDB) \ v11.0; AttachDbFilename="C: \ Users \ X_user \ Documents \ Visual Studio 2013 \ Projects \ WebAxExample \ WebAxExample \ App_Data \ BD_Example.dmdf.mdf"; Integrated Security = True