从两个表中获取数据(查询)

本文关键字:获取 查询 数据 两个 | 更新日期: 2023-09-27 18:03:07

查询两个表时出现错误。当我运行以下代码时,"tbl_program"附近的语法不正确。我想从两个表中检索数据。

// Define the ADO.NET objects.
        SqlConnection con = new SqlConnection(connectionString);
        string selectSQL = "SELECT * FROM tbl_team";
        SqlCommand cmd = new SqlCommand(selectSQL, con);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataSet dsPubs = new DataSet();
        // Try to open database and read information.
        try
        {
            con.Open();
            adapter.Fill(dsPubs, "tbl_team");
            // This command is still linked to the data adapter.
            cmd.CommandText = "SELECT * tbl_programme";
            adapter.Fill(dsPubs, "tbl_student_project_choice");
            DataRelation test = new DataRelation("test",
            dsPubs.Tables["tbl_team"].Columns["teamId"],
            dsPubs.Tables["tbl_student_project_choice"].Columns["choiceGroupId"]);
            dsPubs.Relations.Add(test);

        }

从两个表中获取数据(查询)

您的SELECT语句中缺少FROM关键字,

cmd.CommandText = "SELECT * FROM tbl_programme";
                         // ^ HERE