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) { } }}
8条回答 默认 最新
- 心蓝168 2022-06-04 09:11关注
F5跟一下吧,应该DataSet中的Table[0]这种有问题吧,这个得看看你查询数据时返回的DataSet是否包含Table。
有[n]的地方注意一下,是否用错变量。一般情况下都是变量用错才会导致超范围的。本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 c#窗体设计器错误如何解决?
- ¥30 征集Python提取PDF文字属性的代码
- ¥15 如何联系真正的开发者而非公司
- ¥15 有偿求苍穹外卖环境配置
- ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
- ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
- ¥15 clousx6整点报时指令怎么写
- ¥30 远程帮我安装软件及库文件
- ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
- ¥20 (求远程解决)深信服vpn-2050这台设备如何配置才能成功联网?