从内部连接脚本中搜索学生 "“关键字‘where’附近的语法不正确。”


private void button1_Click(object sender, EventArgs e)
        {
            con.Open();
            string str = "select * from (select tblNewAdmission.NAID as student_ID,tblNewAdmission.fname as full_name,tblNewAdmission.mname as Monther_Name,tblNewAdmission.gender as Gender,tblNewAdmission.dob as Date_of_Birth,tblNewAdmission.mobile as Mobile,tblNewAdmission.email as Email_ID,tblNewAdmission.semester,tblNewAdmission.prog as Programing_Language,tblNewAdmission.sname as school_name,tblNewAdmission.duration as Course_duration,tblNewAdmission.addres as Address,tblfee.fee as Fees from tblNewAdmission inner join tblfee on tblNewAdmission.NAID=tblfee.NAID) where NAID = '"+textBox1.Text+"'";
            SqlCommand cmd = new SqlCommand(str, con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
            //cmd.ExecuteNonQuery();
            con.Close();
        }
above code Iam writing in the button click event for searching students


Iam using the joins to get the result from the 2 tables but when iam searching specfic student from the above script inner join result, iam not geeting 
can you please help me with script.  

我尝试过的:

string str = "select * from (select tblNewAdmission.NAID as student_ID,tblNewAdmission.fname as full_name,tblNewAdmission.mname as Monther_Name,tblNewAdmission.gender as Gender,tblNewAdmission.dob as Date_of_Birth,tblNewAdmission.mobile as Mobile,tblNewAdmission.email as Email_ID,tblNewAdmission.semester,tblNewAdmission.prog as Programing_Language,tblNewAdmission.sname as school_name,tblNewAdmission.duration as Course_duration,tblNewAdmission.addres as Address,tblfee.fee as Fees from tblNewAdmission inner join tblfee on tblNewAdmission.NAID=tblfee.NAID) where NAID = '"+textBox1.Text+"'";

解决方案1

正如戴夫指出的,您可以简化查询。 另外,您不会传递这样的参数。

尝试这个:

C#
string str = "SELECT tblNewAdmission.NAID AS student_ID, tblNewAdmission.fname AS full_name, tblNewAdmission.mname AS Monther_Name, tblNewAdmission.gender AS Gender, tblNewAdmission.dob AS Date_of_Birth, tblNewAdmission.mobile AS Mobile, tblNewAdmission.email AS Email_ID, tblNewAdmission.semester, tblNewAdmission.prog AS Programing_Language, tblNewAdmission.sname AS school_name, tblNewAdmission.duration AS Course_duration, tblNewAdmission.addres AS Address, tblfee.fee AS Fees FROM tblNewAdmission INNER JOIN tblfee ON tblNewAdmission.NAID = tblfee.NAID WHERE tblNewAdmission.NAID = @NAID";

using (SqlCommand command = new SqlCommand(str, con))
{
    command.Parameters.AddWithValue("@NAID", textBox1.Text);

    // Execute your query and process the result
}

コメント

タイトルとURLをコピーしました