使用多个where子句选择一行

时间:2014-02-21 19:10:06

标签: c# asp.net

以下是我的代码,以下代码是用ASP.net的.cs页面编写的。

public DataTable SearchSubject(string Branch, string Course, string Year, string Semester)
{
    da.SelectCommand = new SqlCommand();
    da.SelectCommand.CommandText = "select * from Subjects where Course='" + Course + "' AND Branch='" + Branch + "' AND Year='" + Year + "' AND Semester='" + Semester + "'";
    da.SelectCommand.CommandText = "select * from Subjects where Course= @Course AND Branch= @Branch AND Year= @Year AND Semester= @Semester";
    da.SelectCommand.Parameters.Add("@Course", SqlDbType.VarChar).Value = Course;
    da.SelectCommand.Parameters.Add("@Branch", SqlDbType.VarChar).Value = Branch;
    da.SelectCommand.Parameters.Add("@Year", SqlDbType.VarChar).Value = Year;
    da.SelectCommand.Parameters.Add("@Semester", SqlDbType.VarChar).Value = Semester;
    da.SelectCommand.Connection = con;
    da.Fill(ds, "Subjects");
    return ds.Tables["Subjects"];
}

0 个答案:

没有答案