Wednesday, March 28, 2012

How to get data in DataSet or DataTable from DataReader using SQL Perameters

The C# Part

string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
 SqlConnection mySqlConnection = new SqlConnection(connStr);
        mySqlConnection.Open();
        SqlCommand mySqlCommand = new SqlCommand("GetPersonByID", mySqlConnection);
        //Use this if you want to use a store procedure
        mySqlCommand.CommandType = CommandType.StoredProcedure;
        mySqlCommand.Parameters.AddWithValue("@id", ID);
        SqlDataReader dr = mySqlCommand.ExecuteReader();
        dt.Load(dr);
        GridView1.DataSource = dt;
        GridView1.DataBind();

No comments:

Post a Comment