有两个表pubclas和student,pubclass用来存储课程信息的,public用来存储学生选课信息的
以下是代码:
show.Visible = false;
                string connstring = WebConfigurationManager.ConnectionStrings["connstring"].ToString();
                OleDbConnection mycon = new OleDbConnection(connstring);
                mycon.Open();
                string sql = "select * from pubclass,studentkc where studentkc.userID='" + Session["yhm"].ToString() + "'AND 'pubclass.Cno==studentkc.Cno' AND 'pubclass.Csequence==studentkc.Csequence'";
                OleDbCommand mycmd = new OleDbCommand(sql, mycon);
                OleDbDataReader dr = mycmd.ExecuteReader();
                if (dr.HasRows)
                {
                    show.Visible = true;
                    show.Text = "<table border=1>";
                    show.Text += "<tr><td> 课程号</td>";
                    show.Text += "<td>课程名</td>";
                    show.Text += "<td>序列号</td>";
                    show.Text += "<td>课程学分</td>";
                    show.Text += "<td>开课系</td>";
                    show.Text += "<td>任课教师</td>";
                    show.Text += "<td>上课时间</td>";
                    show.Text += "<td>上课地点</td>";
                    show.Text += "<td>考试类型</td></tr>";
                    //show.Text = "<table border=1>";
                    while (dr.Read())
                    {
                        show.Text += "<tr><td>" + dr[0].ToString() + "</td>";
                        show.Text += "<td>" + dr[1].ToString() + "</td>";
                        show.Text += "<td>" + dr[2].ToString() + "</td>";
                        show.Text += "<td>" + dr[3].ToString() + "</td>";
                        show.Text += "<td>" + dr[7].ToString() + "</td>";
                        show.Text += "<td>" + dr[4].ToString() + "</td>";
                        show.Text += "<td>" + dr[5].ToString() + "</td>";
                        show.Text += "<td>" + dr[6].ToString() + "</td>"; 
                        show.Text += "<td>" + dr[8].ToString() + "</td></tr>";
                    }
                    show.Text += "</table>";
                }
                else
                {
                    show.Visible = true;
                    show.Text = "<table border=1>";
                    show.Text += "<td>你还没有选择课程!</td>";
                    show.Text += "</table>";
                }
                dr.Close();
                mycon.Close();
结果他把pubclass里的记录全部显示出来了 ,请问select那里错了

解决方案 »

  1.   

    你调试 把sql 复制出来在数据库里面去运行不就一下就看出来了嘛
      

  2.   

    string sql = "select * from pubclass,studentkc where studentkc.userID='" + Session["yhm"].ToString() + "' AND pubclass.Cno=studentkc.Cno AND pubclass.Csequence=studentkc.Csequence";
      

  3.   


      string sql = "select * from pubclass,studentkc where studentkc.userID='" + Session["yhm"].ToString() + "'AND pubclass.Cno==studentkc.Cno AND pubclass.Csequence==studentkc.Csequence";
    把你的sql换成这个
      

  4.   

    把SQL显示出来,看是不是你想的要的吧 response.write(sql)
    感觉得这边有问题string sql = "select * from pubclass,studentkc where studentkc.userID='" + Session["yhm"].ToString() + "'AND 'pubclass.Cno==studentkc.Cno' AND 'pubclass.Csequence==studentkc.Csequence'";==??
      

  5.   

      string sql = "select * from pubclass,studentkc where studentkc.userID='" + Session["yhm"].ToString() + "'   AND 'pubclass.Cno==studentkc.Cno' AND 'pubclass.Csequence==studentkc.Csequence'";
    第一个And前面应该加一个空格吧。
      

  6.   

     string sql = "select * from pubclass,studentkc where studentkc.userID='" + Session["yhm"].ToString() + "'AND pubclass.Cno==studentkc.Cno AND pubclass.Csequence==studentkc.Csequence";这个试了不行
      

  7.   

    And前面应该加一个空格什么意思?
      

  8.   

    显示异常详细信息: System.Data.OleDb.OleDbException: 语法错误 (操作符丢失) 在查询表达式 'studentkc.userID='20096801' AND  pubclass.Cno==studentkc.Cno AND  pubclass.Csequence==studentkc.Csequence' 中。显示错误
      

  9.   

     string sql = "select * from pubclass left join studentkc on pubclass.Cno = studentkc.Cno where studentkc.userID = ' " + Session["yhm"].ToString(); +"'";
      

  10.   


     如果不加空格就会这样
    select * from pubclass,studentkc where studentkc.userID='11'AND 'pubclass.Cno==studentkc.Cno' 
    产生语法错误。
      

  11.   

     OleDbCommand mycmd = new OleDbCommand(sql, mycon);
    OleDbDataReader dr = mycmd.ExecuteReader();有没有错啊
      

  12.   

    谢谢大家,特别是一楼和二楼的兄弟,问题已经解决了!是select查询语句中string sql = "select studentkc.Cno,pubclass.Cname,studentkc.Csequence,pubclass.Credit,pubclass.Ccollege,pubclass.Cteacher,pubclass.Ctime,pubclass.Caddr,pubclass.Ctype from pubclass,studentkc where studentkc.userID='" + Session["yhm"].ToString() + "' AND  pubclass.Cno=studentkc.Cno  AND  pubclass.Csequence=studentkc.Csequence"
    后面的两个and的条件不用加单引号
      

  13.   

    服了你了   没见过谁写SQL语句条件都加单引号的