【问题标题】:I have a error in update query...Check please my code我在更新查询中有错误...请检查我的代码
【发布时间】:2012-08-11 00:26:06
【问题描述】:

我有四个班级的学生、课程、注册和更新分数

在学生类中:

        public class Student
{
    public string studID  { get; set; }
    public string name { get; set; }

    public Student() { }

    public Student(string StudID,string Name)
    {
        this.studID = StudID;
        this.name = Name;
    }
}

在课程中

    public class Course
{
    public string courseID { get; set; }
    public string title { get; set; }
    public int credits { get; set; }

    public Course()
    {

    }

    public Course(string CourseID, string Name, int Credits)
    {
        this.courseID = CourseID;
        this.title = Name;
        this.credits = Credits;
    }
}

在注册类中

     public class Registration 
{
    public Student studentData { get; set; }
    public Course courseData { get; set; } 
    public DateTime DOEnroll { get; set; } 

    public Registration ()
    {
    }

    public Registration (Student sData, Course cData, DateTime doe)
    {
        this.studentData = sData;
        this.courseData = cData;
        this.DOEnroll = doe;

    }

}

在 UpdateScore 类中

    public class UpdateScore 
{
    public Registration enrollData { get; set; }
    public int score { get; set; }

    public UpdateScore () { }

    public UpdateScore (Registration enrData, int sco)
    {
        this.enrollData = enrData;
        this.score = sco;
    }
}

现在我正在使用此查询进行更新,但在 studentID 和 CourseID 中显示空数据

代码是:

     public static void Update(UpdateScore score)
    {
        SqlConnection conn = DB.GetConnection();

        try
        {
            conn.Open();
            string selectStm = "UPDATE EnrollmentTable set Score = @Score where StudentID = @StudentID AND  CourseID = @CourseID";

            SqlCommand command = new SqlCommand(selectStm, conn);

            SqlParameter param1 = new SqlParameter();
            SqlParameter param2 = new SqlParameter();
            SqlParameter param3 = new SqlParameter();

            param1.ParameterName = "@StudentID";
            param2.ParameterName = "@CourseID";
            param3.ParameterName = "@Score";


            Student st = new Student();
            Course cr = new Course();

            Registration enr = new Registration ();

            enr.courseData = cr;
            enr.studentData = st;
            score.enrollData = enr;

            param1.Value = st.studID ;
            param2.Value = cr.courseID;
            param3.Value = score.score;


            command.Parameters.Add(param1);
            command.Parameters.Add(param2);
            command.Parameters.Add(param3);



            int i = command.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            throw ex;
        }
        finally
        {
            conn.Close();//Close Connection
        }

    }

它给了我这个例外

参数化查询“(@StudentID nvarchar(4000),@CourseID nvarchar(4000),@Score int)U”需要参数“@StudentID”,但未提供。

您能告诉我如何获取 StudentID 和 CourseID 中的值吗?

【问题讨论】:

    标签: c# c#-4.0


    【解决方案1】:

    您不应该在参数名称本身中包含@。改变

    param1.ParameterName = "@StudentID";
    param2.ParameterName = "@CourseID";
    param3.ParameterName = "@Score";
    

    param1.ParameterName = "StudentID";
    param2.ParameterName = "CourseID";
    param3.ParameterName = "Score";
    

    还有,

    • 您捕获异常只是为了再次抛出它。不要捕获一个你不会做有用的事情的异常。
    • 使用 using 关键字比在 finally 块中手动关闭连接更简洁。

    【讨论】:

    • 也不要使用throw ex;,使用throw;,因为它会保留堆栈跟踪。
    • 或者使用throw new SomeSpecificException("My Meaning text", ex),这样原来的异常就是新异常的InnerException。
    【解决方案2】:

    Eric 对您的问题给出了正确的解释。我想指出另一个问题。

    在您的学生班级中,您稍后分配给 param1.Value 的自动属性 ​​studID 是一个字符串。 C# 中的字符串是对象,因此它们的默认值为 null。您的更新代码没有为 studID 分配任何值,因此当您将其分配给 param1.Value 时,实际上是在将 null 分配给 param1.Value。这可能不是你想要的。

    与 CourseID 相同。

    假设您在 Update 方法中收到的 UpdateScore 已按照您的对象模型的建议正确构造了其中的所有数据,您不应创建新的 Student 或 Course 对象。相反,做类似的事情

    param1.Value = score.enrollData.studentData.studID;
    

    其他信息也类似。

    继续编码!

    注意:这看起来很像家庭作业,因此您还应该借此机会考虑一下代码在做什么,还可以使用调试器运行它以查看值是如何分配的,等等。我已经概述了一些帮助,但是我不会为你做你的全部工作:-)

    【讨论】:

    • 你能告诉我如何解决它吗?
    • 我这样做了,但它采用的是空值,这就是我创建对象的原因。:(我真的陷入了这个错误大约 3 小时。:(
    猜你喜欢
    • 1970-01-01
    • 2021-08-31
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    相关资源
    最近更新 更多