MySql数据库代码中的错误 “resultDs.Tables”

huangapple go评论64阅读模式
英文:

Error in MySql database code "resultDs.Tables"

问题

我使用Visual Studio和C#制作一个简单的课程管理系统,但在编写课程查询函数时遇到了一些问题。

错误:引发了“System.NullReferenceException”类型的异常,针对“resultDs.Tables”。

以下是我的代码:

private void SelectResultForm_Load(object sender, EventArgs e)
{
    string sql = "select ca.id,c.name as coures_name,t.real_name as teacher_name,ca.course_time,ca.course_place,ca.max_num";
    sql += " from course_arrange ca,course c, teacher t where ca.course_id = c.id and ca.teacher_id = t.id";
    DataSet ds = SQLHelper.GetData(sql);
    courseArrangeGridView.DataSource = ds.Tables[0];
    int firstArrangeId = (int)ds.Tables[0].Rows[0][0];

    sql = "select * from select_result where course_arrange_id" + firstArrangeId;
    DataSet resultDs = SQLHelper.GetData(sql);
    ResultGridView.DataSource = resultDs.Tables[0];//error
}

我在这个地方设置了断点,发现resultDs.Tables为空。但我已经检查了数据库连接没有问题。

我是一名新手,希望大家能给我一些建议。

英文:

I am using Visual Studio and C# to make a simple course management system, but I have some problems when writing the course query function.

Error An exception of type 'System.NullReferenceException' was thrown for 'resultDs.Tables'

Below is my code

    private void SelectResultForm_Load(object sender, EventArgs e)
    {
        string sql = "select ca.id,c.name as coures_name,t.real_name as teacher_name,ca.course_time,ca.course_place,ca.max_num";
        sql += " from course_arrange ca,course c, teacher t where ca.course_id = c.id and ca.teacher_id = t.id";
        DataSet ds = SQLHelper.GetData(sql);
        courseArrangeGridView.DataSource = ds.Tables[0];
        int firstArrangeId = (int)ds.Tables[0].Rows[0][0];


        sql = "select * from select_result where course_arrange_id" + firstArrangeId;
        DataSet resultDs = SQLHelper.GetData(sql);
        ResultGridView.DataSource = resultDs.Tables[0];//error
    }

I have hit a breakpoint at this place and resultDs.Tables is null. But I checked the database connection are no problem.

I'm a rookie, I hope everyone can give me some advice

答案1

得分: 1

如Charlieface所说,你的SQL语法有错误。你需要将 where course_arrange_id" + firstArrangeId 修改为 where course_arrange_id=" + firstArrangeId.

当然,这不一定是错误的根本原因。如果你确定数据库连接没有问题,可以在 ResultGridView.DataSource = resultDs.Tables[0]; 处设置断点,然后查看 resultDs 的值。如果 resultDs 的值都是 null,你需要检查一下你的SQL语句是否有问题。你可以将SQL语句放入数据库管理工具中查看是否可以成功运行。

如果以上都没有问题,你需要提供 SQLHelper.GetData 的代码,这将有助于分析问题。

同时,需要防止SQL注入。如果只是做简单的练习,可以使用参数过滤的方法。

英文:

As Charlieface said, you have an error in your sql syntax. You need to change where course_arrange_id" + firstArrangeId to where course_arrange_id=" + firstArrangeId.

Of course, this is not necessarily the cause of this error. If you are sure that your database connection is fine, you can run with a breakpoint at ResultGridView.DataSource = resultDs.Tables[0]; and then see the value of resultDs. If the values of resultDs are all null, you need to check whether there is a problem with your sql statement. You can put your sql into the database management tool to see if it can run successfully.

If all this is ok you need to give the code for SQLHelper.GetData. This makes it easier to analyze the problem.

At the same time, it is necessary to prevent SQL injection. If you are just doing simple exercises, you can use the method of parameter filtering.

huangapple
  • 本文由 发表于 2023年4月11日 08:31:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定