Ado Presentation

download Ado Presentation

If you can't read please download the document

Transcript of Ado Presentation

Create Database

Open a new web site in Visual Studio

Right click on add new item

Choose Database

Make a SQL database named MyDatabase.mdf

Database Explorer

Creating a database takes you from Solution Explorer to Database Explorer

You should see your database MyDatabase.mdf

Create a New Table

Drill down under MyDatabase

Right click on Tables and Add new table

Enter fields into table

Enter a field and name it my_varchar and make it type varchar(50)

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

Save Table

Save table and name it my_tableve table and name it my_table

Open Table to add data to it

Right click on table and select 'Show table data'

Enter data into table

Enter at least 2 rows of data

Enter a string in the 'my_varchar' field

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

Get Connect String

Right click on Database name

Click on properties

See connection string in lower left properties

ConnectionString extract from Properties

You should be able to select the connect string and copy it to the buffer

See next slide for what it should look like when you put it in the code

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:

DataSource=.\\SQLEXPRESS; AttachDbFilename=|DataDirectory| MyDatabase.mdf;IntegratedSecurity=True;User Instance=True;

Create Web Form

Go back to Solution Explorer

Right click on the website name and add new item

Add a web form names AdoTest.aspx

Indicate that it is using code behind

AdoTest.aspx.cs

Code behind helps to separate the code from the front end (html and controls)

We'll code the database read and data rendering in the code behind

We'll put cod in Page_Load method

Code SqlConnection

Create a string containing the data connect string that we discussed above

Create a Sql Connection using the string

Note the red squigly line under SqlConnection

Add the using ... to Reference .net library

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

Correct this error and remove the red line

Add the SqlCommand

Code a SqlCommand by combining a sql string with a connection

To select the data we created in the database use this sql:

Select my_varchar,my_int from my_table

Open the connection

Before you can execute a command or work you must open a connection

Open connections are like open pipes to the database you will need to close it when you are done

Execute Command and Get a Reader back

Enter the code to execute the command a return an instance of a SqlDataReader

Render the data to the web form with HTML

You can use the Reponse.Write method to write data directly to the browser

See comments in code on the next slide as to how data is extracted form the reader

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

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 + " " + id + "
"); }

Close connection

After you have finished with the reader and the database connection you need to close it

If you don't close the connection you will run into problems with the database it will think you are still connected

View Web page in browser

You can view you web page in the browser

This will force your code to compile and bring up a browser window with the response

To do this right click on the .aspx file in the solution explorer and choose to 'View in Broswer'

Simple Output

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

Write a Table

In the next bit of code I render the data in an html table

This table can be styled if needed

Tabular Output

View the data rendered as a table