using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Data.SqlClient;namespace 学生选课系统{ public partial class frmSC : Form { public frmSC() { InitializeComponent(); } string selectcondition; private void frmSC_Load(object sender, EventArgs e) { DataBase dbStudent = new DataBase(); DataSet dataSetStudent = new DataSet(); #region 提取学生信息 string sqlStr = "select Sno,Sname from tbl_Student"; dataSetStudent = dbStudent.GetDataFromDB(sqlStr); if (dataSetStudent.Tables[0].Rows.Count > 0) { cmbStudent.Items.Clear();//清空 for (int i = 0; i < dataSetStudent.Tables[0].Rows.Count; i++) { //学号(4位)--姓名 cmbStudent.Items.Add(dataSetStudent.Tables[0].Rows[i]["Sno"].ToString() + "--" + dataSetStudent.Tables[0].Rows[i]["Sname"].ToString()); } } #endregion DataBase dbCourse = new DataBase(); DataSet dataSetCourse = new DataSet(); #region 提取课程信息 sqlStr = "select Cno,Cname from tbl_Course"; dataSetCourse = dbCourse.GetDataFromDB(sqlStr); if (dataSetCourse.Tables[0].Rows.Count > 0) { cmbCourse.Items.Clear(); for (int i = 0; i < dataSetCourse.Tables[0].Rows.Count; i++) { //课程号(4位)--课程名 cmbCourse.Items.Add(dataSetCourse.Tables[0].Rows[i]["Cno"].ToString() + "--" + dataSetCourse.Tables[0].Rows[i]["Cname"].ToString()); } } #endregion } //成绩录入 private void btnGradeIn_Click(object sender, EventArgs e) { try { if (cmbSelcondition.SelectedIndex == -1 || txtSelText.Text == "") { MessageBox.Show("录入成功"); cmbCourse.SelectedIndex = -1; cmbStudent.SelectedIndex = -1; txtGrade.Text = ""; } else { string sqlStr = "select tbl_SC.Sno,tbl_Student.Sname,tbl_SC.Cno,tbl_Course.Cname,tbl_SC.grade from tbl_Student,tbl_Course,tbl_SC wheretbl_Course.Cno=tbl_SC.Cno and tbl_Student.Sno=tbl_SC.Sno and" + selectcondition + "='" + txtSelText.Text.Trim() + "'"; sqlStr = string.Format("select tbl_SC.Sno,tbl_Student.Sname,tbl_SC.Cno,tbl_Course.Cname,tbl_SC.grade from tbl_Student,tbl_Course,tbl_SC where tbl_Course.Cno=tbl_SC.Cno and tbl_Student.Sno=tbl_SC.Sno and {0}='{1}'", selectcondition, this.txtSelText.Text.Trim()); DataBase db = new DataBase(); DataSet ds = new DataSet(); ds = db.GetDataFromDB(sqlStr); if (ds.Tables[0].Rows.Count == 0) { MessageBox.Show("没有符合条件的成绩记录!"); } else { dgrdvResult.DataSource = ds.Tables[0]; SetHeaderText(); } } } catch(Exception ex) { MessageBox.Show(ex.Message); } } private void btnSelect_Click(object sender, EventArgs e) { try { if (cmbSelcondition.SelectedIndex==-1||txtSelText.Text=="") { MessageBox.Show("请选择查询条件并输入查询值!"); } else { string sqlStr =string.Format( "select tbl_SC.Sno,tbl_Student.Sname,tbl_SC.Cno,tbl_Course.Cname,tbl_SC.grade FROM tbl_Student,tbl_Course,tbl_SC where tbl_Course.Cno=tbl_SC.Cno and tbl_Student.Sno=tbl_SC.Sno and {0}='{1}'", selectcondition,this.txtSelText.Text.Trim()); DataBase db = new DataBase(); DataSet ds = new DataSet(); sqlStr = "select * from tbl_SC"; ds = db.GetDataFromDB(sqlStr); if (ds.Tables[0].Rows.Count==0) { MessageBox.Show("没有符合条件的成绩记录!"); } else { dgrdvResult.DataSource = ds.Tables[0]; SetHeaderText(); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } void SetHeaderText() { dgrdvResult.Columns[0].HeaderText = "学号"; dgrdvResult.Columns[1].HeaderText = "姓名"; dgrdvResult.Columns[2].HeaderText = "课程号"; dgrdvResult.Columns[3].HeaderText = "课程名"; dgrdvResult.Columns[4].HeaderText = "成绩"; } private void cmbSelcondition_SelectedIndexChanged(object sender, EventArgs e) { switch (cmbSelcondition.SelectedIndex) { case 0: selectcondition = "tbl_SC.Sno"; break; case 1: selectcondition = "tbl_SC.Cno"; break; } } private void btnSelAll_Click(object sender, EventArgs e) { try { string sqlStr = string.Format("select tbl_SC.Sno,tbl_Student.Sname,tbl_SC.Cno,tbl_Course.Cname,tbl_SC.grade from tbl_Student,tbl_Course,tbl_SC where tbl_Course.Cno=tbl_SC.Cno and tbl_Student.Sno=tbl_SC.Sno ", this.txtSelText.Text.Trim()); DataBase db = new DataBase(); DataSet ds = new DataSet(); sqlStr = "select * from tbl_SC"; ds = db.GetDataFromDB(sqlStr); if (ds.Tables[0].Rows.Count == 0) { MessageBox.Show("没有符合条件的成绩记录!"); } else { dgrdvResult.DataSource = ds.Tables[0]; SetHeaderText(); } } catch(Exception ex) { MessageBox.Show(ex.Message); } } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } private void dgrdvResult_CellContentClick(object sender, DataGridViewCellEventArgs e) { } }}
F5跟一下吧,应该DataSet中的Table[0]这种有问题吧,这个得看看你查询数据时返回的DataSet是否包含Table。
有[n]的地方注意一下,是否用错变量。一般情况下都是变量用错才会导致超范围的。
catch得到的信息是什么贴出来
这代码太乱了,不好看
一般是数组超出索引范围。你这代码也太不整洁了,看起来很费劲。
打断点调试一下。看一下哪一句代码报错,然后看看数组有值没有,然后看下索引值从0开始 是不是超过了。
涉及到dataSet 判断下不为空
数组或者集合越界了,操作的索引超过了集合最大长度。
找一下代码里面是否有获取列表数据的代码,看下是否是你取值写的索引超出了列表数据存在的数据的索引
看你标题错误信息就是有个数组在取值的时候数字长度是0 或者 索引值小于0或者 大于数组的长度导致报错,提供的代码太乱了没耐心看