0

I'm trying to connect to a sql server, I have no idea why but I get a stack overflow on the Constructor method of the connection class enter image description here

This was provided by my teacher and I have used it on another projects but It doesn't seem to work anymore, I am at a loss

You can check my whole connection class here:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace Integrador_Araujo_Octubre.Persistencia
{
    public class Conexion
    {
    public static string cadenaConexion = @"Password=123;Persist Security Info=True;User ID=sa;Initial 
    Catalog=Integrador_Araujo_Octubre;Data Source=NoteBook-Lucas\SQLEXPRESS";
    public static SqlConnection conexion;
    public Conexion() { }
    public static bool Conectar()
    {
        bool conectado = false;
        conexion = new SqlConnection(cadenaConexion);
        try
        {
            conexion.Open();
            conectado = true;
        }
        catch (Exception error)
        {
            Console.WriteLine(error.Message);
            conectado = false;
        }
        return conectado;
    }
    public DataSet EjecutarSeleccionSQL(string sqlQuery)
    {

        DataSet lResult = null;
        SqlCommand lSqlCommand = null;
        try
        {
            lSqlCommand = new SqlCommand();
            lSqlCommand.CommandText = sqlQuery;
            lSqlCommand.CommandType = CommandType.Text;

            SqlDataAdapter lAdapter = new SqlDataAdapter(lSqlCommand);
            lResult = new DataSet();

            Conectar();
            lSqlCommand.Connection = conexion;

            lAdapter.Fill(lResult);
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            if ((lSqlCommand != null))
            {
                lSqlCommand.Dispose();
                lSqlCommand = null;
            }
            if ((conexion != null))
            {
                conexion.Close();
                conexion = null;
            }
        }
        return lResult;
    }
    public void EjecutarConsultaSQL(string sqlQuery)
    {
        SqlCommand lSqlCommand = null;
        try
        {
            lSqlCommand = new SqlCommand();
            lSqlCommand.CommandText = sqlQuery;
            lSqlCommand.CommandType = CommandType.Text;

            Conectar();
            lSqlCommand.Connection = conexion;
            lSqlCommand.ExecuteNonQuery();
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            if ((lSqlCommand != null))
            {
                lSqlCommand.Dispose();
                lSqlCommand = null;
            }
            if ((conexion != null))
            {
                conexion.Close();
                conexion = null;
            }
        }
    }/*
    public void EjecutarTransaction(string sqlQuery)
    {
        Conectar();
        SqlTransaction ObjTran = conexion.BeginTransaction();
        try
        {
            ObjTran.Commit();
        }
        catch (Exception)
        {
            ObjTran.Rollback();
        }
        conexion.Close();
    }*/
    public void EjecutarTransaction(List<string> sqlQuerys)
    {
        Conectar();
        SqlCommand lSqlCommand = null;
        SqlTransaction ObjTran = conexion.BeginTransaction();
        try
        {
            foreach (String sqlQuery in sqlQuerys)
            {
                lSqlCommand = new SqlCommand();
                lSqlCommand.CommandText = sqlQuery;
                lSqlCommand.CommandType = CommandType.Text;
                lSqlCommand.Transaction = ObjTran;

                lSqlCommand.Connection = conexion;
                lSqlCommand.ExecuteNonQuery();
            }
            ObjTran.Commit();
        }
        catch (Exception)
        {
            ObjTran.Rollback();
        }
        conexion.Close();
    }
}

}

Lucas Araujo
  • 133
  • 1
  • 2
  • 11
  • Can you show the calling part? – Rand Random Oct 10 '17 at 01:04
  • 1
    Look at callstack and you'll find problem – Backs Oct 10 '17 at 01:33
  • You really shouldn't have a static SqlConnection to begin with. That class implements [IDisposable](https://msdn.microsoft.com/en-us/library/system.idisposable(v=vs.110).aspx) and should only be declared in a `using` block unless you have a very good reason to do otherwise, which you don't. – mason Oct 10 '17 at 01:56
  • Follow answer on this question, you can learn about how this exception happens... then you can resolve it easily https://stackoverflow.com/questions/206820/how-do-i-prevent-and-or-handle-a-stackoverflowexception – Shiwanka Chathuranga Oct 10 '17 at 02:40
  • "This was provided by my teacher". your teacher provided you with faulty code. You should never use a static or class level SqlConnection. In fact, you should avoid as much as possible to use any IDisposable as a static or class level member. – Zohar Peled Oct 15 '17 at 14:10

0 Answers0