using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.OleDb;using System.Windows.Forms;namespace 系统登录界面
{
    public partial class 学生信息 : Form
    {
        public 学生信息()
        {
            InitializeComponent();
        }        public string xsID;
        
        string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=I:\\C#\\作业\\keshe\\系统登录界面\\课程.mdb";
        //显示学生已选择课程信息并统计学分
        public void show_selected()
        {
            OleDbConnection conn = new OleDbConnection(strcon);
            conn.Open();
            //显示学生已选择课程信息
            string str_stuSel = "select 选课.kcID,课程.kename,课程.keXF from 课程,选课,学生表 where 选课.kcID=课程.kecID and 选课.xsID=学生表.xsID  and 学生表.xsID='" + xsID + "'";            OleDbDataAdapter da = new OleDbDataAdapter(str_stuSel, strcon);
            DataSet ds = new DataSet();
            da.Fill(ds, "selected");
            dataGridView2.DataSource = ds.Tables["selected"];
        }   
        //显示可选课程信息
        public void show_courseSel()
        {
            OleDbConnection conn = new OleDbConnection(strcon);
            conn.Open();
            //当某门课程选课人数超过30人,不允许学生选课,不在“可选课程”表格中显示
            string str_courseSel = string.Format("select kecID,keName,keXF from 课程 where Count<10");
            OleDbDataAdapter da = new OleDbDataAdapter(str_courseSel, conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "选课");
            dataGridView1.DataSource = ds.Tables["选课"];
            conn.Close();
        }        private void button2_Click(object sender, EventArgs e)
        {
            show_selected();
            //统计学生已选课程学分
            OleDbConnection conn = new OleDbConnection(strcon);
            conn.Open();
            string str_credit = "SELECT Sum(课程.keXF) as test FROM 课程, 学生表,选课 where 选课.kcID=课程.kecID and 选课.xsID=学生表.xsID  and 学生表.xsID='" + xsID + "'";            OleDbDataAdapter da_credit = new OleDbDataAdapter(str_credit, strcon);
            DataSet ds_credit = new DataSet();
            da_credit.Fill(ds_credit);
            label3.Text = ds_credit.Tables[0].Rows[0]["test"].ToString();
            conn.Close();
        }           
       
      
         private void button3_Click(object sender, EventArgs e)
        {
           
            show_courseSel();
        }
        private void button1_Click_1(object sender, EventArgs e)
        {
            登陆界面 aa = new 登陆界面();
            aa.Show();
            this.Hide();        }        //退选
        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           
        }        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int aa = int.Parse(label3.Text);
            {
                if (dataGridView1.CurrentCell.Value.ToString() == "选课")
                {
                    if (aa > 10)
                        MessageBox.Show("学分已满3分,不能再选课!");
                    else
                    {
                        OleDbConnection conn = new OleDbConnection(strcon);  //建立连接              
                        conn.Open();  //打开连接
                        string course_Id = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString().Trim();                        //该学生是否已经选过这门课程
                        string sql_query = string.Format("select * from 选课 where kcID='{0}' and xsID='{1}'", course_Id, xsID);
                        OleDbCommand cmd_query = new OleDbCommand(sql_query, conn);
                        OleDbDataReader dr = cmd_query.ExecuteReader();
                        if (dr.HasRows)
                        { MessageBox.Show("该课程已选,请选择其他课程!");
                        }
                        else
                        {  
                           
                            //向courseSel表添加选课信息
                            string sql_insertSel = string.Format("insert into 选课(kcID,xsID,xktime) values('{0}','{1}','{2}')",course_Id, xsID,DateTime.Now.ToShortDateString());
                            OleDbCommand cmd_insertSel = new OleDbCommand(sql_insertSel, conn);
                            if (cmd_insertSel.ExecuteNonQuery() > 0)
                            {
                                MessageBox.Show("添加成功!");
                                //更新courseMsg中选中课程的选课总人数selCount
                                string sql_updateSelCount = string.Format("update [课程] set count=count+1 where kecID='{0}'", course_Id);
                                OleDbCommand cmd_updateSelCount = new OleDbCommand(sql_updateSelCount, conn);
                                cmd_updateSelCount.ExecuteNonQuery();
                                conn.Close();
                                //刷新学生选课信息和学分
                                show_selected();
                                //刷新可选课程信息
                                show_courseSel();
                            }
                            else
                            {
                                MessageBox.Show("添加失败!");
                            }
                        }
                    }
                }            }
        }        private void 学生信息_Load(object sender, EventArgs e)
        {
                    }        private void dataGridView2_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {            OleDbConnection conn = new OleDbConnection(strcon);  //建立连接              
            conn.Open();  //打开连接
            string sel = dataGridView2[1, e.RowIndex].Value.ToString();
            if (dataGridView2.CurrentCell.Value.ToString() == "退选")
            {
                string str_del = string.Format("delete * from 选课 where kcID='{0}' and xsID='{1}'", sel, xsID);
                OleDbCommand cmd = new OleDbCommand(str_del, conn);
                if (cmd.ExecuteNonQuery() > 0)
                {
                    MessageBox.Show("退选成功!");
                    //更新courseMsg中选中课程的选课总人数selCount
                    string sql_updateSelCount = string.Format("update [课程] set count=count-1 where kecID='{0}'", sel);
                    OleDbCommand cmd_updateSelCount = new OleDbCommand(sql_updateSelCount, conn);
                    cmd_updateSelCount.ExecuteNonQuery();
                    conn.Close();
                    //刷新学生选课信息和学分
                    show_selected();
                    //刷新可选课程信息
                    show_courseSel();
                }
                else
                    MessageBox.Show("退选失败!");
            }        }      
    }
}

解决方案 »

  1.   

    你先吧sql语句的最终内容显示出来,就清楚了
      

  2.   

    可能是关键字,试试这样
     string sql_updateSelCount = string.Format("update [课程] set [count]=[count]-1 where kecID='{0}'", sel);
      

  3.   

    SELECT *  into [数据库].[dbo].[新表名]
      FROM [数据库].[dbo].[原表]
      

  4.   

    你的学生表.xsID 是什么类型的 如果是int型的 条件后面就不需要加单引号
      

  5.   

    你的学生表.xsID 是什么类型的 如果是int型的 条件后面就不需要加单引号赞同
      

  6.   

     就是这样,,count是关键字,要加括号
      

  7.   

    建议将数据库访问封装成一个DLL,然后去调用,否则这程序看起来真是累死,楼主是做毕设吧
      

  8.   

    把sql考出来单独运行调试。。