代码如下,查询不到数据库内的内容,总显示“没有成绩”,报错
调试中显示了System.Data.SqlClient.SqlException”类型的第一次机会异常在 System.Data.dll 中发生
的错误
sql语句是对的,在sql server里面试过可以查询
代码如下:using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;public partial class _Default : System.Web.UI.Page
{
          protected void Page_Load(object sender, EventArgs e)
    {    }
    protected void BTQuery_Click(object sender, EventArgs e)
    {
        string connString;
        string Sname, Cname;
        //获取姓名和课程名
        Sname = TBSname.Text;
        Cname = TBCname.Text;
        //设置数据库连接串,使用系统认证
        connString = "Initial Catalog=jiaoxuedb;Data Source=KWONG;Integrated Security=SSPI;";
        SqlConnection Conn = new SqlConnection(connString);
        SqlCommand QueryCommand = new SqlCommand("SELECT Score FROM Student,Course,SC" +
            "WHERE Student.Sno=SC.Sno and Course.Cno=SC.Cno and" +
            "Sname=@Sname and Cname=@Cname",Conn);
        //Add the parameters for the SelectCommand
        QueryCommand.Parameters.Add("@SName", SqlDbType.Char, 8);
        QueryCommand.Parameters.Add("@Cname", SqlDbType.Char, 20);
        QueryCommand.Parameters["@SName"].Value = Sname;
        QueryCommand.Parameters["@CName"].Value = Cname;
        Conn.Open();
        //执行 QueryCommand.ExecuteScalar方法查询成绩
        try
        {
            Int32 ScoreValue = (Int32)QueryCommand.ExecuteScalar();
            LaScore.Text = Convert.ToString(ScoreValue);
        }
        catch//错误处理
        {
            LaScore.Text = "没有成绩";
        }
        finally
        {
            Conn.Close();
        }
    }
}

解决方案 »

  1.   

    你可以换一个执行查询的方法。ExecuteScalar(),我不知道这个对不对,我查询从来没有用过这个方法,你试试另外的方法
      

  2.   

    ExecuteReader(),用法自己查吧。我也好久没有用了
      

  3.   

    SqlDataReader myreader=mycomm.ExecuteReader();//将从数据源检索到的数据赋给myreader
    for(int i=0;i<myreader.FieldCount;i++)

     s+=myreader.GetValue(i)
    }//把字段的值赋给s;
    Label1.Text=s;//最后让label显示检索到的字段的值.
    百度的
      

  4.   

        connString = "Initial Catalog=jiaoxuedb;Data Source=KWONG;Integrated Security=SSPI;"; 总觉的这个有问题