working with ms sql server n.
Skip this Video
Loading SlideShow in 5 Seconds..
Working with MS SQL Server PowerPoint Presentation
Download Presentation
Working with MS SQL Server

Loading in 2 Seconds...

play fullscreen
1 / 54

Working with MS SQL Server - PowerPoint PPT Presentation

  • Uploaded on

Working with MS SQL Server. Textbook Chapter 14. Objectives. You will be able to Use Visual Studio for GUI based interactive access to a Microsoft SQL Server database. Populate a table with data from an Excel worksheet.

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

PowerPoint Slideshow about 'Working with MS SQL Server' - jeb

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
working with ms sql server

Working with MS SQL Server

Textbook Chapter 14


You will be able to

  • Use Visual Studio for GUI based interactive access to a Microsoft SQL Server database.
    • Populate a table with data from an Excel worksheet.
  • Use sqlcmd for command line interactive access to a Microsoft SQL Server database.
  • Using either Visual Studio or sqlcmd to:
    • Create and delete tables.
    • Insert new rows into tables.
    • Modify table definitions.
    • Modify existing rows.
    • Retrieve and display table data.
coe resources
CoE Resources
  • We all have accounts, with a single database each, on a Microsoft SQL Server on the CoE network:
  • Classroom and lab computers can access scorpius directly.
  • A VPN connection is required for your home computer or wireless laptop to access scorpius.
    • or maybe not for wireless access on campus
the database server
The Database Server
  • This is the same machine that we use as a web server.
  • Different server software.
  • Different user accounts.
    • Be sure to use the username and password that you have been given for the SQL Server
      • Same username as for the web server (wpusrxx).
      • Different password.
using visual studio
Using Visual Studio
  • Visual Studio includes database access functions.
    • View "Server Explorer"
    • Set up a Data Connection
  • Visual Studio 2010 will not work with scorpius.
    • The database server version on scorpius is too old!
    • We have to use Visual Studio 2008.
download visual studio 2008
Download Visual Studio 2008
  • If you don't have Visual Studio 2008 on your computer, you can get a free 90 day trial from Microsoft.
    • Very large download!
    • .iso file
      • Can't be installed directly.
      • Must use Virtual CD program or burn a real CD
      • See
  • OR
adding a data connection in visual studio 2008
Adding a Data Connection in Visual Studio 2008

Right click on Data Connections and select Add Connection.

adding a data connection in visual studio 20081
Adding a Data Connection in Visual Studio 2008

You will see this the first time you add a data connection.

adding a data connection
Adding a Data Connection

Your SQL Server Username

Your SQL Server Password

adding a table
Adding a Table
  • We will add a table to the database.
    • Populate the table with data from an Excel spreadsheet.
  • Define the table in Visual Studio.
  • Download the table as a CSV file.
  • Open the spreadsheet in Excel.
  • Copy the spreadsheet data in Excel.
  • Paste the data into the database table in Visual Studio.
  • Modify the table definition to add a unique ID column.
the addresses table
The Addresses Table
  • We will create a new table to hold the addresses in file Addresses.csv.

File Addresses.csv

data types
Data Types
  • Common SQL Data Types
    • int
    • nvarchar(n) n = max number chars
    • char(n) n = number chars
    • decimal (p,s) p = total number of digits

s = number decimal places

    • datetime
  • Many more!
viewing table data
Viewing Table Data

Right click on Addresses and select “Show Table Data”.

viewing table data1
Viewing Table Data

Table is currently empty.

populating a table
Populating a Table
  • Download file Addresses.csv from the Downloads area of the class web site:
  • Double click to open in Excel
populating the database table
Populating the Database Table
  • We will use the contents of this Excel worksheet to populate the Address table in the database.
  • Click inside the worksheet, then press Ctrl-A to select all.
  • Press Ctrl-C to copy the entire worksheet to the clipboard.
populating the database table1
Populating the Database Table

Click here to select all of the table.

Press Ctrl-V to paste the clipboard into the table.

(This may take a while to complete.)

setting an id field
Setting an ID Field
  • It is good practice to include an ID field for every row of a database table.
    • Unique identifier
    • Normally an Identity column.
    • Value set by server as each row is added
  • Not present in the Excel worksheet.
open table definition
Open Table Definition

Right click on Addresses and select Open Table Definition.

Add column ID as int.

add column id
Add Column ID

Select row and drag to the top.

set column properties
Set Column Properties

Make ID an identity column.

set primary key
Set Primary Key

Right click on ID and select Set Primary Key.

show table data
Show Table Data

End of Section

the sqlcmd utility
The sqlcmd Utility
  • Command line utility for MS SQL Server databases.
    • Previous version called osql
  • Available on classroom and lab PCs.
  • In Windows command window
    • Connect to a Database Server
    • Enter SQL commands on the command line.
    • Results output to the console.
getting and installing sqlcmd
Getting and Installing sqlcmd
  • Included with Management Studio Express.
  • Free download from Microsoft
  • In Visual Studio Help, search for sqlcmd.
    • Or just google sqlcmd
the sql language
The SQL Language
  • The following slides demonstrate SQL commands using the sqlcmd program.
  • Remember SQL is used everywhere we interact with a database server:
    • Command line
    • Database management programs
    • Our own programs
using sqlcmd
Using sqlcmd


The Server

Password entered here

Execute commands in buffer

sql command line example
SQL Command Line Example
  • View a subset of the columns.
retrieve specific entries
Retrieve Specific Entries

Find all addresses with zip code 33707

wildcard selection
Wildcard Selection

Find all addresses with last name beginning with S

wildcard selection1
Wildcard Selection

All addresses with last name containing an s

case sensitivity
Case Sensitivity
  • SQL commands and keywords are not case sensitive.
  • Data is case sensitive
    • Characters are stored as upper case or lower case.
  • Sorting order is a mode.
    • Default is not case sensitive
    • Search comparisons same as sorting order.
sorting the output
Sorting the Output

Zip_Code is a string. So sort is lexicographic, not numeric.

zip codes
Zip Codes

Zip codes are sorted legicographically, not numerically.

counting rows
Counting Rows

How many addresses are there with zip code 33707?

sql commands in visual studio
SQL Commands in Visual Studio
  • We can also use Visual Studio to issue arbitrary SQL commands to the server.
  • On the Data menu select New Query
sql commands in visual studio3
SQL Commands in Visual Studio

Enter SQL command here.

sql commands in visual studio4
SQL Commands in Visual Studio

Click here to execute the command.

sql commands in visual studio5
SQL Commands in Visual Studio

Results appear below the Query window.

learning sql
Learning SQL
  • Many books available
  • One that I recommend:
    • Teach Yourself SQL in 24 Hours (Third Edition)
    • Ryan Stephens and Ron Plew, SAMS, 2003
  • Lots of information on the web
  • A sampling: