• 25-04-2023, 01:50:50
    #1
    sorunumu kendim uzun deneme yanılmalarla yaptım, halloldu konu kapatılabilir
  • 25-04-2023, 04:08:41
    #2
    using System;
    using System.Data.SqlClient;
    using System.Linq;
    public partial class Veritabani
    {
    public static string ConnectionString = string.Empty;
    public static object Kontrol()
    {
    using (var l_oConnection = new SqlConnection(ConnectionString))
    {
    try
    {
    l_oConnection.Open();
    return true;
    }
    catch (SqlException ex)
    {
    return ex.Message.ToString();
    }
    }
    }
    public static DataRow DataRowGetir(string SQL)
    {
    var Tablo = DataTableGetir(SQL);
    if (Tablo is not null && Tablo.Rows.Count > 0)
    {
    return Tablo.Rows(0);
    }
    else
    {
    return default;
    }
    }
    public static DataTable DataTableGetir(string SQL)
    {
    var DT = new DataTable();
    var Command = new SqlCommand();
    var Adaptor = new SqlDataAdapter();

    var Baglanti = VTBaglanti();
    if (Baglanti.State == ConnectionState.Open)
    {
    Command.CommandText = SQL.ToString();
    Command.Connection = Baglanti;
    Adaptor.SelectCommand = Command;
    Adaptor.Fill(DT);
    Command.Connection.Close();
    Command.Dispose();
    Adaptor.SelectCommand.Connection.Close();
    Adaptor.Dispose();
    Baglanti.Dispose();
    return DT;
    }
    else
    {
    return default;
    }

    }
    public static SqlConnection VTBaglanti()
    {
    string ConnectionSTr = ConnectionString;
    var ClassConnection = new SqlConnection();
    if (ClassConnection.State == ConnectionState.Open)
    {
    ClassConnection.Close();
    ClassConnection.Dispose();
    }
    ClassConnection.ConnectionString = ConnectionSTr;
    ClassConnection.Open();
    if (ClassConnection.State == ConnectionState.Open)
    {
    return ClassConnection;
    }
    else
    {
    return null;
    }
    }

    public static DataTable TestFunction()
    {
    string OrnekSql = "SELECT * FROM FALANCA_TABLO";
    return DataTableGetir(OrnekSql);
    }
    }


    En sondaki TestFunction ile veritabanına istediğin SQL sorgusunu gönderip verini çekebilirsin.
  • 26-04-2023, 08:08:09
    #3
    Bu kodu kendi classima ekleyemedim. Kod çalışmıyor demiyorum belki ben yanlış yada eksik yaptım. Sorunum hala devam ediyor.
  • 26-04-2023, 08:44:58
    #4
    Kodunu chat gpt ye verip hatayı da verip kodu düzenlemesini istedin mi?
  • 26-04-2023, 13:55:02
    #5
    Yaptım evet. Lakin verdigi kodların hic biri hatayi gidermedi.
  • 26-04-2023, 14:02:27
    #6
    deSefa adlı üyeden alıntı: mesajı görüntüle
    Bir kaç sayfa gezince birde aşağıdaki hata çıkıyor biraz bekleyip sayfayı yenileyince gidiyor, bir türlü çözemedim, bilen varmı?

    Server Error in '/' Application.
    ExecuteReader requires an open and available Connection. The connection's current state is open.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    
    Exception Details: System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is open.
    
    Source Error:
    
    
    Line 99:         {
    Line 100:            DataTable table = new DataTable();
    Line 101:            adapter.Fill(table);
    Line 102:            CloseConnection();
    Line 103:            adapter.Dispose();
    
    Source File: c:\Inetpub\vhosts\izmirtermal.com.tr\httpdocs\App_Code\SqlHelper.cs    Line: 101
    
    Stack Trace:
    
    
    [InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is open.]
       System.Data.SqlClient.SqlConnection.GetOpenConnection(String method) +3330884
       System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command) +100
       System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) +314
       System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) +156
       System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +84
       System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +207
       System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +214
       System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior) +465
       System.Data.Common.DbDataAdapter.Fill(DataTable dataTable) +147
       SqlHelper.ToDataTable() in c:\Inetpub\vhosts\izmirtermal.com.tr\httpdocs\App_Code\SqlHelper.cs:101
       SqlHelper.ToValue(Int32 rowIndex, Int32 cellIndex) in c:\Inetpub\vhosts\izmirtermal.com.tr\httpdocs\App_Code\SqlHelper.cs:116
       yenikategori.Page_Load(Object sender, EventArgs e) in c:\Inetpub\vhosts\izmirtermal.com.tr\httpdocs\yenikategori.aspx.cs:23
       System.Web.UI.Control.OnLoad(EventArgs e) +108
       System.Web.UI.Control.LoadRecursive() +90
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1533
    
    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.8.4494.0
    sqlHelper Class kodum
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    public interface ISqlCommand
    {
        ISqlCommand CommandText(string commandText);
        ISqlCommand Connection(SqlConnection connection);
        ISqlCommand Transaction(SqlTransaction transaction);
        ISqlCommand CommandTimeout(int timeout);
        ISqlCommand CommandType(CommandType commandType);
        ISqlCommand Parameters(Func<ISqlCommandParameter, object> parameter);
        DataSet ToDataSet();
        DataTable ToDataTable();
        DataRow ToDataRow(int rowIndex = 0);
        TResult ToValue<TResult>(int rowIndex = 0, int cellIndex = 0);
        SqlDataReader ExecuteReader(CommandBehavior commandBehavior = CommandBehavior.Default);
        int ExecuteNonQuery();
        object ExecuteScalar();
        TResult ExecuteScalar<TResult>();
    }
    public interface ISqlCommandParameter
    {
        ISqlCommandParameter Add(SqlParameter parameter);
        ISqlCommandParameter Add(string parameterName, object value);
        ISqlCommandParameter Add(string parameterName, SqlDbType sqlDbType);
        ISqlCommandParameter Add(string parameterName, SqlDbType sqlDbType, int size);
        ISqlCommandParameter Add(string parameterName, SqlDbType sqlDbType, int size, string sourceColumn);
        ISqlCommandParameter AddWithValue(string parameterName, object value);
    }
    public class SqlHelper : ISqlCommand, IDisposable
    {
        #region Filds
        public static readonly SqlConnection DefaultConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString);
        private readonly SqlCommand _command = null;
        private SqlConnection _connection = null;
        private SqlTransaction _transaction = null;
        #endregion
        static SqlHelper()
        {
        }
        public SqlHelper()
        {
            _connection = DefaultConnection;
            _command = new SqlCommand { Connection = _connection };
        }
        #region ISqlCommand
        public ISqlCommand CommandText(string commandText)
        {
            _command.CommandText = commandText;
            return this;
        }
        public ISqlCommand Connection(SqlConnection connection)
        {
            _connection = connection;
            return this;
        }
        public ISqlCommand Transaction(SqlTransaction transaction)
        {
            _transaction = transaction;
            _command.Transaction = _transaction;
            return this;
        }
        public ISqlCommand CommandTimeout(int timeout)
        {
            _command.CommandTimeout = timeout;
            return this;
        }
        public ISqlCommand CommandType(CommandType commandType)
        {
            _command.CommandType = commandType;
            return this;
        }
        public ISqlCommand Parameters(Func<ISqlCommandParameter, object> parameter)
        {
            SqlCommandParameter param = new SqlCommandParameter(_command);
            parameter.Invoke(param);
            return this;
        }
        public DataSet ToDataSet()
        {
            using (SqlDataAdapter adapter = new SqlDataAdapter(_command))
            {
                DataSet dataSet = new DataSet();
                adapter.Fill(dataSet);
                return dataSet;
            }
        }
        public DataTable ToDataTable()
        {
            OpenConnection();
            using (SqlDataAdapter adapter = new SqlDataAdapter(_command))
            {
                DataTable table = new DataTable();
                adapter.Fill(table);
                CloseConnection();
                adapter.Dispose();
                return table;
            }
        }
        public DataRow ToDataRow(int rowIndex = 0)
        {
            DataTable table = ToDataTable();
            if (table.Rows.Count == 0) return null;
            return table.Rows[rowIndex];
        }
        public TResult ToValue<TResult>(int rowIndex = 0, int cellIndex = 0)
        {
            DataTable table = ToDataTable();
            if (table.Rows.Count == 0) return default(TResult);
            object value = table.Rows[rowIndex][cellIndex];
            if (value is TResult)
            {
                return (TResult)value;
            }
            throw new InvalidCastException();
        }
        public SqlDataReader ExecuteReader(CommandBehavior commandBehavior = CommandBehavior.Default)
        {
            OpenConnection();
            return _command.ExecuteReader(commandBehavior);
        }
        public int ExecuteNonQuery()
        {
            OpenConnection();
            int result = _command.ExecuteNonQuery();
            CloseConnection();
            return result;
        }
        public object ExecuteScalar()
        {
            OpenConnection();
            object value = _command.ExecuteScalar();
            CloseConnection();
            return value;
        }
        public TResult ExecuteScalar<TResult>()
        {
            OpenConnection();
            object value = _command.ExecuteScalar();
            if (value is TResult)
            {
                CloseConnection();
                return (TResult)value;
            }
            throw new InvalidCastException();
        }
        private void OpenConnection()
        {
            if (_connection.State != ConnectionState.Open)
            {
                _connection.Open();
            }
        }
        private void CloseConnection()
        {
            if (_connection.State != ConnectionState.Closed)
            {
                _connection.Close();
            }
        }
        #endregion
        #region IDisposable
        public void Dispose()
        {
            if (_transaction != null)
            {
                _transaction.Dispose();
            }
            if (_connection.State != ConnectionState.Open)
            {
                _connection.Close();
            }
        }
        #endregion
    }
    public class SqlCommandParameter : ISqlCommandParameter
    {
        private readonly SqlCommand _command;
        public SqlCommandParameter(SqlCommand command)
        {
            _command = command;
        }
        public ISqlCommandParameter Add(SqlParameter parameter)
        {
            _command.Parameters.Add(parameter);
            return this;
        }
        public ISqlCommandParameter Add(string parameterName, object value)
        {
            _command.Parameters.Add(parameterName, value);
            return this;
        }
        public ISqlCommandParameter Add(string parameterName, SqlDbType sqlDbType)
        {
            _command.Parameters.Add(parameterName, sqlDbType);
            return this;
        }
        public ISqlCommandParameter Add(string parameterName, SqlDbType sqlDbType, int size)
        {
            _command.Parameters.Add(parameterName, sqlDbType, size);
            return this;
        }
        public ISqlCommandParameter Add(string parameterName, SqlDbType sqlDbType, int size, string sourceColumn)
        {
            _command.Parameters.Add(parameterName, sqlDbType, size, sourceColumn);
            return this;
        }
        public ISqlCommandParameter AddWithValue(string parameterName, object value)
        {
            _command.Parameters.AddWithValue(parameterName, value);
            return this;
        }
    }
    [B][COLOR=#E25041][FONT="Arial Black"][/FONT][/COLOR][/B]

    Merhabalar, dönüş yapılmıştır.
  • 26-04-2023, 20:00:44
    #7
    sorunumu kendim uzun deneme yanılmalarla yaptım, halloldu konu kapatılabilir