C# Programming For The Microsoft SQL Server Database: Dave Henson
C# Programming For The Microsoft SQL Server Database: Dave Henson
• Recommended Software:
• Visual Studio 2003
• SQL Server Standard Version
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
Chapter 1 - Tools
• Visual Studio
• Command Line Tools
• Other Tools
Visual Studio
• Fully integrated IDE
• Intellisense
• Automatic Code Generation
• Drag/Drop Database and Control Interface
• Database and Other Design Wizards
Command Line Tools
• Subdirectory:
C:\WINDOWS\Microsoft.NET\Framework\V1.1.4322
class App{
public static void Main()
{
Console.WriteLine("hey");
Console.ReadLine();
}
}
Case Sensitivity
• Most .Net classes are Mixed Case
• Many C# keywords are lower case
• Valid Code:
…
String greeting = “hello”;
String Greeting = “goodbye”;
if Statement
if (loop != 20)
{
string errorMessage = “Oops”;
}
while statement
while(dr.Read())
{
Console.WriteLine(dr[“FirstName”]);
}
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
{
}
Creating Objects
• An object is an instance of a class
• Examples:
String stuff = “hello”;
MyClass class = new MyClass(1);
SqlDataSet ds = CreateDataSet(query);
Chapter 3 - Class Libraries
Definitions
• Class library – body of functions and
properties usable by other code
• Examples:
– ActiveX control/COM object
– .Net Assembly
– Web Service
– Win32 dll
• Implementation:
– Often in a .dll file
Purpose
• Supply re-usable code to developer
• Maintain consistency
• Ease of maintenance – Separation of front
end from back end
• Reduce development costs
Methods
Class biz{
//Static – member of the type
static public string SayHi(){
return “hi”;
}
//Non static – member of the instance
public string SayBye(){
string message = _message;
return message;
}
}
Properties
• Properties are methods!
• Get
• Set
• Options Available:
– Configuration Class
– Front End App (.aspx file)
– Web.Config
– UDL File (OleDB Only)
– Registry
– Custom File
– COM+ Catalog Using Connection Strings