| |

VerySource

 Forgot password?
 Register
Search
View: 645|Reply: 3

A strange question about SqlDataReader? ? ? ? ?

[Copy link]

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-18 11:30:01
| Show all posts |Read mode
I use SqlDataReader to return a string in a page, and I use using to automatically close the SqlDataReader, but when there are multiple such SqlDataReaders in a page (such as 10), more than 10 will be generated in the database Threads, stuffed my database thread pool, can't figure it out, please help! Thank you
public string BindAdWords (int count, string location)
{
string sql = "SELECT Top" + count + "* FROM AdWords WHERE Location = '" + location + "' AND IsCancel = 0";
string path = System.Configuration.ConfigurationSettings.AppSettings ["ReadAdWordsImage"];
string link = "";
string imagedefault = "";
Admin.DB db = new Admin.DB ();
using (SqlDataReader dr = db.ExecuteReadSQL (sql))
{
if (dr.HasRows)
{
while (dr.Read ())
{
link + = "<tr>";
link + = "<td align = 'center'>";
if (Convert.ToString (dr ["AdImage"]) == "" || dr ["AdImage"] == null)
{
link + = imagedefault;
}
else
{
link + = "<a href='" + dr["WebSite"].ToString() + "'target='_blank'> <IMG alt = '' src =" + path + dr ["AdImage"]. ToString () + "> </a>";
}
}
link + = "</ td>";
link + = "</ tr>";
}
else
{
link + = imagedefault;
}
return link;
}
DB:
public SqlDataReader ExecuteReadSQL (string sql)
{
sqlConn = new SqlConnection (connectionString);
cmd = new SqlCommand (sql, sqlConn);
SqlDataReader dr = null;
try
{
sqlConn.Open ();
dr = cmd.ExecuteReader ();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write ("<font color = 'Red'>" + ex.Message.ToString () + "</ font>");
}
return dr;


}
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-4-22 20:30:01
| Show all posts
Close the link?
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-4-23 12:15:01
| Show all posts
Is it not necessary to use explicit closing with using? It will automatically recycle the connection!
And after adding dr.close () manually in using, it is the same
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-4-23 13:30:01
| Show all posts
And if I refresh the page, there will be more threads in the database thread pool, but the previous one will not be recycled
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list