Home > DeveloperSection > Forums > How to close the datareader
Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    October-04-2014 1:15 AM

 MSSQL Server ASP.Net  C#  MSSQL Server 
Ratings:


 1 Reply(s)
 605  View(s)
Rate this:
this is a simple function that takes care of all the queries:

DataTable ReadIt(string query, params MySqlParameter[] sqlParams)

{

    DataTable dt = new DataTable();

 

    using(MySqlConnection myConnection = new MySqlConnection(sConnection))

    {

        myConnection.Open();

        MySqlCommand myCommand = myConnection.CreateCommand();

        myCommand.CommandText = query;

        foreach (var p in sqlParams)

        {

            myCommand.Parameters.Add(p);

        }

        MySqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

        dt.Load(myReader);

        myReader.Close();

    }

 

    return dt;

}

i want to myReader.Close(); afeter loading data to datatable



Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Posted on    October-04-2014 1:16 AM

try this code:

using (MySqlConnection myConnection = new MySqlConnection(sConnection))

        using (MySqlCommand myCommand = myConnection.CreateCommand())

        {

            myConnection.Open();

            myCommand.CommandText = query;

            foreach (var p in sqlParams)

                myCommand.Parameters.Add(p);

            using (MySqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection))

            {

                dt.Load(myReader);

            }

        }


Don't want to miss updates? Please click the below button!

Follow MindStick