public Attractions GetAttractions(string ID) { //Step 1 - Define a connection to the database by getting // the connection string from App.config string DBConnect = ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString; SqlConnection myConn = new SqlConnection(DBConnect); //Step 2 - Create a DataAdapter to retrieve data from the database table string sqlStmt = "Select * from Attractions where Id = @para_ID"; SqlDataAdapter da = new SqlDataAdapter(sqlStmt, myConn); da.SelectCommand.Parameters.AddWithValue("@para_ID", ID); //Step 3 - Create a DataSet to store the data to be retrieved DataSet ds = new DataSet(); //Step 4 - Use the DataAdapter to fill the DataSet with data retrieved da.Fill(ds); //Step 5 - Read data from DataSet. Attractions emp = null; int rec_cnt = ds.Tables[0].Rows.Count; if (rec_cnt == 1) { DataRow row = ds.Tables[0].Rows[0]; // Sql command returns only one record string name = row["name"].ToString(); string desc = row["desc"].ToString(); string price = row["price"].ToString(); decimal pay = Convert.ToDecimal(price); string image = row["image"].ToString(); emp = new Attractions(ID, name, desc, pay, image); } return(emp); }
public List <Attractions> GetAttractionsAll() { //Step 1 - Define a connection to the database by getting // the connection string from web.config string DBConnect = ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString; SqlConnection myConn = new SqlConnection(DBConnect); //Step 2 - Create a DataAdapter to retrieve data from the database table string sqlstmt = "Select * from Attractions"; SqlDataAdapter da = new SqlDataAdapter(sqlstmt, myConn); //Step 3 - Create a DataSet to store the data to be retrieved DataSet ds = new DataSet(); //Step 4 - Use the DataAdapter to fill the DataSet with data retrieved da.Fill(ds); //Step 5 - Read data from DataSet. List <Attractions> empList = new List <Attractions>(); int rec_cnt = ds.Tables[0].Rows.Count; for (int i = 0; i < rec_cnt; i++) { DataRow row = ds.Tables[0].Rows[i]; // Sql command returns only one record string ID = row["Id"].ToString(); string name = row["name"].ToString(); string desc = row["description"].ToString(); string price = row["price"].ToString(); decimal pay = Convert.ToDecimal(price); string image = row["image"].ToString(); Attractions obj = new Attractions(ID, name, desc, pay, image); empList.Add(obj); } return(empList); }