Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

Sqlconnection and multiple connections

$
0
0

Hi all,

I have a doubt: when I run a query to db and I do a loop based on the results, I create a new SqlConnection for every query as follows:

selectSQL = "SELECT * FROM languages";
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand(selectSQL, con);

            try
            {
                con.Open();
                adapter = new SqlDataAdapter(cmd);
                dt = new DataTable();
                adapter.Fill(dt);
                rows = dt.Rows.Count;

                for (int i = 0; i < rows; i++)
                {
                    string SQL="SELECT * FROM ..."
                    SqlConnection cn = new SqlConnection(connectionString);
                    SqlCommand cd = new SqlCommand(SQL, cn);
                    try
                    {
                        cn.Open();
                        .. do the stuff

and in the finally statement I close the connections.

In a website that I have online sometimes I get an error: "timeout expired! max connections reached from the pool" or something like that.

The question is: is this a right approach or it can cause the problem above?

Is it possible to use only one connection to the db and pass it to the SqlCommand?

Thank you


Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>