我试图连接到SQL服务器,我不知道为什么但是我在连接类的Constructor方法上得到了堆栈溢出
这是由我的老师提供的,我已经将它用于其他项目,但它似乎不再起作用,我不知所措
您可以在此处查看我的整个连接类:
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();
}
}
}