DCSIMG
There is already an open DataReader associated with this Command which must be closed first (איך להפעיל SqlDataReader בתוך SqlDataReader) - שלמה גולדברג (הרב דוטנט)

שלמה גולדברג (הרב דוטנט)

מרצה בסלע ויועץ בעולם ה - net.

There is already an open DataReader associated with this Command which must be closed first (איך להפעיל SqlDataReader בתוך SqlDataReader)

 

הייתי היום בהרצאה ממש טובה על Ado.Net Entity Freamwork של עידו, היה ממש נחמד ולמדתי המון דברים חדשים.
 
אבל היה שווה לבוא להרצאה רק כדי לשמוע על הפיצ'ר הבא (שלמעשה קיים כבר מ - Ado 2.0)
 
 
בדרך כלל אנחנו לא יכולים להפעיל redaer כל עוד שה - reader הקודם לא נסגר (אלא אם כן נייצר connection חדש)
לדוגמא - הקוד הבא:
 

SqlConnection cnn = new SqlConnection(@"Data Source=sever;Initial Catalog=DB;Integrated Security=True;");

 

SqlCommand cmd1 = new SqlCommand("SELECT * FROM Table1", cnn);

SqlCommand cmd2 = new SqlCommand("SELECT * FROM Table2 WHERE Table2.Table1Id = @Id", cnn);

cmd1.Parameters.Add("Id", SqlDbType.Int);

 

cnn.Open();

 

SqlDataReader reader1 = cmd1.ExecuteReader();

 

while (reader1.Read())

{

    //.... read

 

    cmd2.Parameters[0].Value = reader1["Id"];

    SqlDataReader reader2 = cmd2.ExecuteReader();

    while (reader2.Read())

    {

 

    }

}

 

cnn.Close();

 
כשנגיע לשורה שבה אנחנו מנסים לייצר reader על ידי ה - cmd2 - אנחנו נעוף עם:
"There is already an open DataReader associated with this Command which must be closed first"
 
 
כדי לפתור את זה, אפשר פשוט להוסיף ל - ConnectionString את המחרוזת הבאה:
MultipleActiveResultSets=True
 
וזהו, מאותו רגע נוכל לייצר reader בתוך reader.
פורסם: Sep 06 2009, 08:35 PM by Shlomo | with 5 comment(s)

תוכן התגובה

Ido Flatow כתב/ה:

Just wanted to say I had a great time giving this lecture. I’m pleased to hear people learned more than

# September 7, 2009 12:29 AM

There is already an open DataReader associated with this Command which must be closed first (?????? ???????????? SqlDataReader ???????? SqlDataReader) « Jasper Blog כתב/ה:

Pingback from  There is already an open DataReader associated with this Command which must be closed first (?????? ???????????? SqlDataReader ???????? SqlDataReader) « Jasper Blog

# September 7, 2009 10:33 AM

עמירם כתב/ה:

נחמד לדעת !

בטח היו עוד מלא דברים מענינים בהרצאה .

רציתי רק להוסיף לגבי

cmd1.Parameters.Add("Id", SqlDbType.Int);

אם מבצעים פעולת SQL ולא מוסיפים את הפרמטרים עם SqlDbType , זה גורם לירידת ביצועים משמעותית.

לפי מה שאצלינו בדקנו עם SQL ANALAYZER, שאילתה ללא SqlDbType לוקחת בין 5 -6 שניות מול שאילתה שכתובה עם SqlDbType לוקחת פחות משניה.

# September 7, 2009 10:43 AM

Israel Shn כתב/ה:

Great but isn't it "undocumented" and dangerous?

Microsoft Visual Studio 2008 Documentation says about SqlDataReader.Read Method:

Only one SqlDataReader per associated SqlConnection may be open at a time, and any attempt to open another will fail until the first one is closed. Similarly, while the SqlDataReader is being used, the associated SqlConnection is busy serving it until you call Close.

isr.shn@gmail.com

# November 8, 2009 3:36 PM

שלמה גולדברג כתב/ה:

ניתן להוריד את דוגמת הקוד מכאן . בפוסט הזה אני רוצה לדבר על ADO, אנחנו נבין את המושגים הבאים: SqlConnection

# December 19, 2009 9:13 PM
שלח תגובה

(שדה חובה)  

(שדה חובה)  

(אופציונלי)

(שדה חובה) 

Please add 2 and 3 and type the answer here:


Enter the numbers above: