Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Working with MS SQL Server Beginning ASP.NET 4.5.1 in C# and VB Chapter 12.

Similar presentations


Presentation on theme: "1 Working with MS SQL Server Beginning ASP.NET 4.5.1 in C# and VB Chapter 12."— Presentation transcript:

1 1 Working with MS SQL Server Beginning ASP.NET 4.5.1 in C# and VB Chapter 12

2 2 Objectives You will be able to Use Visual Studio Server Explorer for GUI based interactive access to a Microsoft SQL Server database. Populate a table with data from an Excel worksheet. Use Visual Studio Server Explorer to: Create database tables. Insert new rows into tables. Retrieve and display table data. Modify existing data.

3 3 The allman server has problems in Visual Studio Sever Explorer. Let's use the local DB server that is installed along with Visual Studio. Create a folder at the root of your C drive to hold files that we will use in the Server Explorer. C:\db USF Resources

4 4 Using Visual Studio for Databases Visual Studio includes database access functions. Start Visual Studio View "Server Explorer" Set up a Data Connection to the local DB server as shown on the following slides.

5 5 Adding a Data Connection in Visual Studio Right click on Data Connections and select Add Connection.

6 6 Adding a Data Connection in Visual Studio You will see this the first time you add a data connection. Select Microsoft SQL Server and click Continue.

7 7 Adding a Data Connection Click here (LocalDB)\v11.0

8 Testing the Connection 8

9 The Data Connection 9

10 You might need to set an option 10

11 Select Table and Diagram Options 11 Uncheck this Under Database Tools

12 12 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 data as a CSV file. Insert the file data into the database table

13 13 Data Types As we define the new table we will have to specify the type of each column. Common SQL Data Types int nvarchar(n)n = max number chars nchar(n)n = number chars decimal (p,s)p = total number of digits s = number decimal places datetime Many more! nvarchar and nchar are Unicode characters

14 Download Addresses.csv 14 Download file Addresses.csv from the class web site to the directory that you created on your C:/ drive. http://www.csee.usf.edu/~turnerr/Web_Application_Design/ Downloads/122_MS_SQL_Server/ http://www.csee.usf.edu/~turnerr/Web_Application_Design/ Downloads/122_MS_SQL_Server/ Double click on the file to open in Excel.

15 15 The Addresses Table We will create a new database table to hold the addresses in file Addresses.csv.

16 Adding a Table Right click on Tables and select Add New Table. 16

17 Set Table Name in T-SQL Tab 17

18 Define the Table 18 Click Update to add the table to the database.

19 Update the Database 19

20 Table "Addresses" is Now Present 20 Right click on Tables and select Refresh.

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

22 22 Viewing Table Data Table is currently empty.

23 Import Data Let's import the data from Addresses.csv Right click on the connection and select New Query. Query: BULK INSERT Addresses FROM 'C:\db\Addresses.csv' WITH ( FIELDTERMINATOR = ',', ROWTERMINATOR = '\n' ) 23

24 Execute the Query 24 Click here to execute

25 Command Successful 25

26 Check the Data in the Table 26 It may take some time for the data to appear.

27 Table Addresses 27

28 28 The SQL Language The following slides demonstrate SQL commands using Visual Studio Server Explorer. Remember SQL is used everywhere we interact with a database server: Database management programs Command line Our own programs

29 SQL Commands in Visual Studio We can use Visual Studio Server Explorer to issue arbitrary SQL commands to the server. Right click on Addresses and select New Query 29

30 New Query 30

31 SELECT * FROM Addresses 31 Click here to execute the command.

32 Query Results 32 Results appear below the Query window.

33 Subset of Columns 33

34 Retrieve Specific Entries 34 Find all addresses with zip code 33707

35 Wildcard Selection 35 Find all addresses with last name beginning with S

36 Wildcard Selection All addresses with last name containing an S 36

37 37 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.” Changing is complicated. http://social.msdn.microsoft.com/forums/sqlserver/en-US/3e564d44-8fb1- 4d2d-82ee-588f4f055d30/change-sql-configuration-to-case-sensitive http://social.msdn.microsoft.com/forums/sqlserver/en-US/3e564d44-8fb1- 4d2d-82ee-588f4f055d30/change-sql-configuration-to-case-sensitive Search comparisons same as sorting order. Default is “not case sensitive”

38 Search for ‘Adams’ 38

39 Search for ‘ADAMS’ 39

40 Search for ‘adams’ 40

41 Sorting the Output 41 Zip_Code is a string. So sort is lexicographic, not numeric. Scroll down to bottom.

42 Sorting by Zip Code 42 Zip codes are sorted legicographically, not numerically.

43 Counting Rows How many addresses are there with zip code 33707? 43

44 Updating Records 44 Suppose Ronald Araque’s zip code should be 33707.

45 Updating Records 45 Note importance of having a unique ID field.

46 Updated Record 46

47 Updating Multiple Fields 47 Suppose the Alfonsos have moved.

48 Updating Multiple Fields 48

49 Updated Record 49

50 Deleting a Record 50 Suppose the Andersons have left the club.

51 Deleting a Record 51

52 After the Delete 52

53 Adding a Record 53

54 Record Added 54

55 55 Learning SQL Textbook, Chapter 12 Many books available One that I recommend: Teach Yourself SQL in 24 Hours (Fifth Edition) Ryan Stephens, Ron Plew, and Arie Jones, SAMS, 2012 Lots of information on the web A sampling: http://en.wikipedia.org/wiki/SQL http://www.asp.net/web-forms/videos/sql-2005 http://www.w3schools.com/sql/default.asp http://db.grussell.org/ch3.html


Download ppt "1 Working with MS SQL Server Beginning ASP.NET 4.5.1 in C# and VB Chapter 12."

Similar presentations


Ads by Google