我想将此DataSet代码更改为SqlDataReader代码

时间:2015-02-07 06:35:18

标签: c# sql sql-server

此代码实际上是用dataset方法编写的。 它工作得很好。但是我想把它改成datareader方法。

public DataSet SelectTopic(LessionPlanBEL bellp)
{
    DataSet ds = new DataSet();
    try
    {
        con.Open();
        string query = "Select * from lessiontopic where subject=@subject ";
        SqlDataAdapter da = new SqlDataAdapter(query, con);
        da.SelectCommand.Parameters.Add("@subject", SqlDbType.Int).Value = bellp.subject;


        da.Fill(ds);
    }
    catch (Exception ex)
    {

    }
    finally
    {
        con.Close();
    }
    return ds;
}

1 个答案:

答案 0 :(得分:1)

试试这个

 public DataSet SelectTopic(LessionPlanBEL bellp)
            {
                SqlDataReader dr = new SqlDataReader();
                SqlCommand cmd;
                try
                {
                    con.Open();
                    cmd =new SqlCommand( "Select * from lessiontopic where subject=@subject ",conn);

                    cmd.Parameters.AddWithValue("@subject", bellp.subject.ToString());
     dr=cmd.ExecuteReader();
                }
                catch (Exception ex)
                {

                }
                finally
                {
                    con.Close();
                }

            }