Database
Download
1 / 13

Database - PowerPoint PPT Presentation


  • 75 Views
  • Uploaded on

Database. Reading from an SQL Database. Server Explorer. Visual Studio includes Server Explorer. This allows you to attach a database and see the tables and the data You can use it to create tables. You can use it to change data. You can use it to create a connection in C#. mdf files.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Database' - ivo


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Database

Database

Reading from an SQL Database


Server explorer

Lecture 6: Database

Server Explorer

  • Visual Studio includes Server Explorer.

    • This allows you to attach a database and see the tables and the data

    • You can use it to create tables.

    • You can use it to change data.

    • You can use it to create a connection in C#


Mdf files

Lecture 6: Database

mdf files

  • The file World.mdf contains an SQL database.

  • You can save the mdf on your computer and open it as a database.

  • You can use New Query to write SQL commands.

  • You will be able to connect to the database from your C# program.



The world table

Lecture 6: Database

The World Table


Sql queries

Lecture 6: Database

SQL Queries

  • A simple SQL Query:

    SELECT name, region, area, population, gdp

    FROM World

  • This will give all the rows and all the columns.


Filtering data

Lecture 6: Database

Filtering Data

  • Get only the countries where the region is South America:

    SELECT name, region, area, population, gdp

    FROM world

    WHERE (region = 'South America')


Ordering data

Lecture 6: Database

Ordering Data

  • You can get the data in order of the size

    SELECT name, area, population, gdp

    FROM world

    WHERE (region = 'South America')

    ORDER BY area


Limiting rows

Lecture 6: Database

Limiting Rows

  • You can say TOP(3) to get the top three rows:

    SELECT TOP (3) name, area, population, gdp

    FROM world

    ORDER BY population DESC


Connection string

Lecture 6: Database

Connection String

  • To connect to the database from C# you need:

    String ConnectionString = @"Data Source=..";

    SqlConnection con = new SqlConnection(cs);

    con.Open();

    String sql = @"SELECT name,region,area,population,gdp FROM world";

    SqlCommand cmd = new SqlCommand(sql, con);

    SqlDataReader sdr = cmd.ExecuteReader();

    con.Close();


Reading values

Lecture 6: Database

Reading Values

SqlCommand cmd = new SqlCommand(sql, con);

SqlDataReader sdr = cmd.ExecuteReader();

Table t = new Table();

while (sdr.Read())

{

TableRow tr = new TableRow();

TableCell td = new TableCell();

td.Text = sdr["name"].ToString();

tr.Cells.Add(td);

t.Rows.Add(tr);

}

Controls.Add(t);

con.Close();


Sqldatasource

Lecture 6: Database

SqlDataSource


Summary

Lecture 6: Database

Summary

  • An mdf database is a single file.

  • You see the database using Server Explorer

  • You write SQL to query the database

  • You can filter and sort and limit

  • There are two good ways to view the data:

    • SqldataSource

    • SqlDataReader