Ado Presentation


Published on

screen shots and instructions for reading data and rendering to a .net web page

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Ado Presentation

  1. 1. Create Database <ul><li>Open a new web site in Visual Studio
  2. 2. Right click on add new item
  3. 3. Choose Database
  4. 4. Make a SQL database named “MyDatabase.mdf” </li></ul>
  5. 5. Database Explorer <ul><li>Creating a database takes you from Solution Explorer to Database Explorer
  6. 6. You should see your database “MyDatabase.mdf” </li></ul>
  7. 7. Create a New Table <ul><li>Drill down under MyDatabase
  8. 8. Right click on Tables and Add new table </li></ul>
  9. 9. Enter fields into table <ul><li>Enter a field and name it ”my_varchar” and make it type varchar(50)
  10. 10. Enter a field name “my_int” and make it type int – make the int an identity field so that it can auto increment and server as a key </li></ul>
  11. 11. Save Table <ul><li>Save table and name it “my_tableve table and name it “my_table ” </li></ul>
  12. 12. Open Table to add data to it <ul><li>Right click on table and select 'Show table data' </li></ul>
  13. 13. Enter data into table <ul><li>Enter at least 2 rows of data
  14. 14. Enter a string in the 'my_varchar' field
  15. 15. You won't need to, nor be allowed to enter data into the my_int field if you set it up as identity because it will auto fill </li></ul>
  16. 16. Get Connect String <ul><li>Right click on Database name
  17. 17. Click on properties
  18. 18. See connection string in lower left properties </li></ul>
  19. 19. ConnectionString extract from Properties <ul><li>You should be able to select the connect string and copy it to the buffer
  20. 20. See next slide for what it should look like when you put it in the code </li></ul>
  21. 21. Connect String You need to modify the connect string that you extract from the properties of the Database so that the compiler will accept it. Here is a sample connect string: Data Source=.QLEXPRESS; AttachDbFilename=|DataDirectory| MyDatabase.mdf;Integrated Security=True;User Instance=True;
  22. 22. Create Web Form <ul><li>Go back to Solution Explorer
  23. 23. Right click on the website name and add new item
  24. 24. Add a web form names AdoTest.aspx
  25. 25. Indicate that it is using code behind </li></ul>
  26. 26. AdoTest.aspx.cs <ul><li>Code behind helps to separate the code from the front end (html and controls)
  27. 27. We'll code the database read and data rendering in the code behind
  28. 28. We'll put cod in Page_Load method </li></ul>
  29. 29. Code SqlConnection <ul><li>Create a string containing the data connect string that we discussed above
  30. 30. Create a Sql Connection using the string
  31. 31. Note the red squigly line under SqlConnection </li></ul>
  32. 32. Add the “using ...” to Reference .net library <ul><li>If you right click on the red squiggly you can choose to resolve the problem by insering a “using” clause to the top of the code page
  33. 33. Correct this error and remove the red line </li></ul>
  34. 34. Add the SqlCommand <ul><li>Code a SqlCommand by combining a sql string with a connection
  35. 35. To select the data we created in the database use this sql:
  36. 36. Select my_varchar,my_int from my_table </li></ul>
  37. 37. Open the connection <ul><li>Before you can execute a command or work you must open a connection
  38. 38. Open connections are like open pipes to the database – you will need to close it when you are done </li></ul>
  39. 39. Execute Command and Get a Reader back <ul><li>Enter the code to execute the command a return an instance of a SqlDataReader </li></ul>
  40. 40. Render the data to the web form with HTML <ul><li>You can use the Reponse.Write method to write data directly to the browser
  41. 41. See comments in code on the next slide as to how data is extracted form the reader </li></ul>
  42. 42. Extracting Data from a SqlDataReader When you extract data from a reader, you should be aware of the data type you are extracting and how it maps to the language (.net) data types
  43. 43. Code to iterate through a reader while (reader.Read()) { //my_varchar is varchar which maps to .net string so use GetString string name = reader.GetString(0); //my_int is int which maps to Int32 Int32 id = reader.GetInt32(1); //now that I have loaded the data into c# string I can output //here I'm using response.write to output directly to the //browser. //I concatenate the data with html to produce the 'layout' //I want Response.Write(name + &quot; &quot; + id + &quot;<br />&quot;); }
  44. 44. Close connection <ul><li>After you have finished with the reader and the database connection you need to close it
  45. 45. If you don't close the connection you will run into problems with the database – it will think you are still connected </li></ul>
  46. 46. View Web page in browser <ul><li>You can view you web page in the browser
  47. 47. This will force your code to compile and bring up a browser window with the response
  48. 48. To do this right click on the .aspx file in the solution explorer and choose to 'View in Broswer' </li></ul>
  49. 49. Simple Output <ul><li>In my first bit of code I just output the data with an HTML break to see that my code is compiling and reading from the database </li></ul>
  50. 50. Write a Table <ul><li>In the next bit of code I render the data in an html table
  51. 51. This table can be styled if needed </li></ul>
  52. 52. Tabular Output <ul><li>View the data rendered as a table </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.