csharpdatabase
TRANSCRIPT
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 1/37
C# Programming for the
Microsoft SQL Server Database
Dave Henson
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 2/37
Logistics
Class Hours
Notes/Handouts
Demos Class website
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 3/37
Recommended Reading
Beginning C# Databases, APress, ISBN 1-
59059-433-9
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 4/37
Course Setup
Software required to complete course labs: Windows xp, 2000 or 2003
.Net Framework 1.1
MSDE
Recommended Software: Visual Studio 2003
SQL Server Standard Version
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 5/37
Course Topics
Quick C# Primer
Tools
Creating/Using Class Libraries
ADO.Net
Exception handling
Stored Procedures
Avoiding SQL Injection Com Interop
SQL Server Best Practices and Techniques
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 6/37
Chapter 1 - Tools
Visual Studio
Command Line Tools
Other Tools
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 7/37
Visual Studio
Fully integrated IDE
Intellisense
Automatic Code Generation Drag/Drop Database and Control Interface
Database and Other Design Wizards
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 8/37
Command Line Tools
Subdirectory:C:\WINDOWS\Microsoft.NET\Framework\V1.1.4322
Csc.exe (vbc.exe, jsc.exe)
± Compiler GACUtil.exe
± Global Assembly Cache tool
Ngen.exe ± Native windows exe converter
RegAsm.exe ± Register a .Net assembly with COM
SN.exe ± Strong Name Generator
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 9/37
Csc.exe ± C# Compiler
Typical Syntax Examples:C:>Csc Simple.cs
± Produces Simple.exe in current directory
C:>Csc /target:library DBUtil.cs
± Produces DBUtil.dll in the current directory
C:>Csc /r:DBUtil.dll DBClient.cs
± Products DBClient.exe which uses the dll
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 10/37
Other Tools
IDE
± Codecharge studio
Automated Code Generation: ± SQL Queries that dynamically c#
± C# that dynamically builds c#
± C# that dynamically builds SQL
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 11/37
Chapter 2 ± C# Review
Program Structure
Case sensitivity
if/while/foreach try/catch
Creating objects
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 12/37
Program Structure
using System;
class App{
public static void Main()
{
Console.WriteLine("hey");
Console.ReadLine();
}
}
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 13/37
Case Sensitivity
Most .Net classes are Mixed Case
Many C# keywords are lower case
Valid Code:«
String greeting = ³hello´;
String Greeting = ³goodbye´;
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 14/37
if Statement
if (loop != 20)
{
string errorMessage = ³Oops´;}
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 15/37
while statement
while(dr.Read())
{
Console.WriteLine(dr[³FirstName´]);
}
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 16/37
foreach statement
DirectoryInfo dir = new DirectoryInfo(@³c:\\temp´);
try
{foreach(FileInfo f in dir.GetFiles())
{Console.WriteLine(f.Name);
}
}catch(Exception e)
{
Console.WriteLine(e.Message);
}Finally
{}
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 17/37
Creating Objects
An object is an instance of a class
Examples:
String stu
ff = ³hello´;MyClass class = new MyClass(1);
SqlDataSet ds = CreateDataSet(query);
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 18/37
Chapter 3 - Class Libraries
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 19/37
Definitions
Class library ± body of functions andproperties usable by other code
Examples:
± ActiveX control/COM object
± .Net Assembly
± Web Service
± Win32 dll Implementation:
± Often in a .dll file
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 20/37
Purpose
Supply re-usable code to developer
Maintain consistency
Ease of maintenance ± Separation of frontend from back end
Reduce development costs
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 21/37
Methods
Class biz{
//Static ± member of the type
static public string SayHi(){
retu
rn ³hi´;}
//Non static ± member of the instance
public string SayBye(){
string message = _message;
return message;
}
}
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 22/37
Properties
Properties are methods!
Get
Set
³value´ keyword has special meaning
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 23/37
Application Layers
Presentation
Business Logic
Data Access
Potential ease of use and potential
disaster
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 24/37
Chapter 4 ± ADO.Net
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 25/37
ADO.NET Components
SqlConnection
SqlDataAdapter
DataSet DataTable
DataRow, DataColumn collections
SqlDataReader SqlCommand
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 26/37
Required Namespaces
System.Data
System.Data.SqlClient
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 27/37
Providers
Providers Available:
± SQL Server .NET Provider
± OleDB .NET Provider Example-AllRecordsBasicOleDB.aspx
± ODBC .NET Provider
± SQL XML .NET Provider
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 28/37
Connections
Connection Defined
Where to Store the Connection String
Connection Syntax Examples Connection Pooling
Security
Close Your Connections! Monitoring Connections
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 29/37
Where to Store the Connection String
Options Available:
± Configuration Class ± Front End App (.aspx file)
± Web.Config ± UDL File (OleDB Only)
± Registry
± Custom File
± COM+ Catalog Using Connection Strings
Evaluation Terms: Security, Convenience,Performance
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 30/37
Two Connection String Syntax
Examples
In the .aspx file:ConnString = ³server=10.0.0.1;UID=sa;PWD=;´
Dim Conn As New SqlConnection(ConnString)
In Web.Config XML file:<configuration>
<appSettings>
<add key=³ConnString´
value=³server=10.0.0.1;UID=sa;PWD=;´/></appSettings>
</configuration>
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 31/37
Connection Pooling
Defined
Controlling Min/Max-Example6ShowConnectionStatePoolControl.aspx
Importance of ³Exact String Match´
Pooling for SqlClient vs. OleDBClient
Effects of pooling on SQL security
Close Your Connections!
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 32/37
Performance Issues
Choose Providers Wisely
DataReader vs. DataAdapter
Repeater Control vs. DataGrid Control
Connection Pooling
Embedding SQL vs. Stored Procedures
Controlling The HTML
Typed Accessor Methods-Example7AdapterVsReaderUsingTypedAccessorMethods.asp
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 33/37
DataReader Vs. DataAdapter
DataReader
± Forward Only
± Only One Record At A Time In Memory
± ³Firehose´ Functionality
± Typed Accessor Methods Avoid Conversions
± One datareader open per connection
DataAdapter ± More Overhead
± More Flexible
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 34/37
Repeater Control vs. DataGrid(or
DataList) Control
Repeat Control Simply Repeats
± Low overhead
± You Have To Do Everything
± You Can Do It Better Than Microsoft Did!
DataGrid
± Default HTML Behaviour
± Higher Overhead, Most Functionality
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 35/37
Embedding SQL vs. Stored
Procedures Stored Proc Advantages:
± Procedure Cache
± Separate Security Model
± Potentially Less Network Traffic
± Output Params, Error Code & Result Set
± Can Do Anything Server Side
± Abstracts the Front End from Changes ±Possible Disadvantage with Xcopy Deployment
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 36/37
Controlling the Presentation
HTML-Use Stylesheets if Possible!
Make Sure The Page Fails Gracefully If
Needed With DataGrids, Use TemplateColumns
8/8/2019 CSharpDatabase
http://slidepdf.com/reader/full/csharpdatabase 37/37
Final Recommendations
Use DataGrids Only When Updates Are
Needed
Embed Connection In Web.Config throughConfig class
Only ³Select´ What You Need
Call StoredProcs For UltimatePerformance When ³Paging´