<span style="font-size:18px;">string conn = "Trusted_Connection = SSPI; Database = Application_site; Server = DELL-PC;";
SqlConnection mycon = new SqlConnection(conn);
mycon.Open();
SqlCommand read_Applicant_Class = new SqlCommand("select * from Applicant_Class", mycon);
SqlDataReader dr_Applicant_Class = read_Applicant_Class.ExecuteReader();</span>
</pre><pre name="code" class="csharp">此处略去一部分代码
<span style="font-size:18px;">
string Class_Date = (string)dr_Applicant_Class["Class_Date"];
string start = (string)dr_Applicant_Class["Start_Time"]; //报越界异常
string end = (string)dr_Applicant_Class["End_Time"];</span>
注:请务必确定不是访问地址越界,并且数据库表的列名没有拼写错误!!!
引起问题的原因初步判断是由于使用
<span style="font-size:18px;">string Class_Date = (string)dr_Applicant_Class["Class_Date"];</span>
语句之前,使用
<span style="font-size:18px;">dr_Applicant_Class.Read();</span>
读取过一次数据
解决方法如下:
在语句
<span style="font-size:18px;">string Class_Date = (string)dr_Applicant_Class["Class_Date"];</span>
之前加上语句
<span style="font-size:18px;">dr_Applicant_Class.Close();
dr_Applicant_Class = read_Applicant_Class.ExecuteReader();
dr_Applicant_Class.Read();
</span>
以关闭dr_Applicant_Class后重新执行读取语句
问题解决
具体原因暂时未知。如有大神路过,还望能够告知一下个中原理,万分感激!