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

时间:2013-03-10 12:39:44

标签: c# sql sql-server

查询两个表时出错了。 “'tbl_programme'附近的语法不正确。”当我运行以下代码时。我想从两个表中检索数据。

// 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);


        }

1 个答案:

答案 0 :(得分:6)

您的FROM声明

中错过了SELECT个关键字
cmd.CommandText = "SELECT * FROM tbl_programme";
                         // ^ HERE