Home > Net >  The problem of c # SqlDataReader <urgent!>
The problem of c # SqlDataReader <urgent!>

Time:09-18

Is designing a test system, the need to connect to a database on a test interface starttest topic,
This test is another login interface student child form,

In execution to the while (SDR) Read ()) will jump to the student, the execution starttest. The show () statement, then the interface were performed,
Excuse me what reason be? SqlDataReader use wrong is me?

CodePudding user response:

The SDR. The Read () after the execution, not closed, to perform sdrxz1. Read () will certainly gets stuck, because before the connection has not been released,

CodePudding user response:

Private void starttest_Load (object sender, EventArgs e)
{
Name. The Text=the userinfo. Loginname;
Num. Text=the userinfo. Loginnum;
The conn. The Open ();
SqlCommand CMD=new SqlCommand (" select * from examset ", the conn);
SqlDataReader SDR=CMD. ExecuteReader ();
The SDR. The Read ();

XZZF=the Convert. ToInt32 (SDRS [1]. The ToString ());
XZDF=the Convert. ToInt32 (SDRS [2]. The ToString ());
PDZF=the Convert. ToInt32 (SDRS [3]. The ToString ());
PDDF=the Convert. ToInt32 (SDRS [4]. The ToString ());
TKZF=the Convert. ToInt32 (SDRS [5]. The ToString ());
TKDF=the Convert. ToInt32 (SDRS [6]. The ToString ());
Alltime.=the Convert ToInt32 (SDRS [7]. The ToString ());


The SDR. The Close ();
The conn. Close ();

Alltimeshow=alltime/60;
Examtime. Text=alltimeshow. ToString ();

Xznum=XZZF/XZDF;
Pdnum=PDZF/PDDF;
Tknum=TKZF/TKDF;
Allquestion1. Text=xznum. ToString ();
Allquestion2. Text=pdnum. ToString ();
Allquestion3. Text=tknum. ToString ();

Xzup. Enabled=false;
Tkup. Enabled=false;
Pdup. Enabled=false;



//random choice
Getxz ();

//random judgment topic
Getpd ();
//random fills up the topic
Gettk ();

}

Public void Getxz ()
{
The conn. The Open ();
String SQLXZSL="select count (*) from question where typeid=1";
SqlCommand CMDXZ=new SqlCommand (SQLXZSL, db. Conn);
Int I=the Convert. ToInt32 (CMDXZ ExecuteScalar ());
The conn. Close ();
If (xznum & gt; I)
{
MessageBox. Show (" choice shortage, please contact the teacher to add the title and then try to answer, ", "prompt", MessageBoxButtons. OK, MessageBoxIcon. Warning).
Enclosing the Close ();
}
The else
{
Studans1=new string [xznum];
Rightan1=new string [xznum];
For (int a=0; A. & lt; Studans1. Length; +)
{
Studans1 [a]="";
}
If (xznum==0)
{
MessageBox. Show (" did not take questions, please contact the teacher to adjust and then try to answer ", "prompt", MessageBoxButtons. OK, MessageBoxIcon. Warning).
Xzup. Enabled=false;
Xzdown. Enabled=false;
Xz. Enabled=false;
}
The else
{
Int u=0;
The string one="select * from question where typeid=1";

The conn. The Open ();
SqlCommand cmdxz1=new SqlCommand (one, the conn);
SqlDataReader sdrxz1=cmdxz1. ExecuteReader ();


While (sdrxz1. Read ())
{
A1. The Add (sdrxz1 [0]. ToString ());
Rightan1 [u]=sdrxz1 [3]. The ToString (). The Trim ();
U++;
}
Sdrxz1. Close ();
String xzdiyiti="select * from question where id='" + Convert. ToInt32 (a1 [0]) +"' ";
SqlCommand cmdxz2=new SqlCommand (xzdiyiti, db. Conn);
SqlDataReader sdrxz2=cmdxz2. ExecuteReader ();
Sdrxz2. Read ();
Xuanzetimu. Text=sdrxz2 [2]. The ToString ();
Xuanxiang1. Text=sdrxz2 [4]. The ToString ();
Xuanxiang2. Text=sdrxz2 [5]. The ToString ();
Xuanxiang3. Text=sdrxz2 [6]. The ToString ();
Xuanxiang4. Text=sdrxz2 [7]. The ToString ();
Sdrxz2. Close ();
The conn. Close ();

Nowquestion1. The Text=1. The ToString ();
}
}

}



I of the SDR. The Close () has written

CodePudding user response:

If there are any abnormal information you missed, you add the try catch processing, estimation is where the exception.
  •  Tags:  
  • C #
  • Related