Professional Documents
Culture Documents
Outline
SQL in C#
Read Chapter 8
But note that it does not talk about C#
Project Phase 1
Meet our TA: Chi-Wai Lau
C# - Crash Course
Hello World
Properties (getters/setters)
Enums
Partial classes
Dataset: DataTable, DataRow
Connecting to a database
http://www.ecma-international.org/activities/Languages/Introduction%20to%20Csharp.pdf
3
C# - Highlights
C# = C++.Sytnax + Java.Semantics
It is a safe language (like Java)
Can be embedded in Webpages
Can access a database
Complex, but you should see the predecessors !
Hello World
using
using System;
System;
class
class Hello
Hello {{
static
static void
void Main()
Main() {{
Console.WriteLine("Hello
Console.WriteLine("Hello world");
world");
}}
}}
5
Point
Point uvw
uvw == new
new Point();
Point();
uvw.position
uvw.position == 55;
55;
uvw.color
uvw.color == green;
green;
uvw.position
uvw.position ==
uvw.position
uvw.position ** 2;
2;
ifif (uvw.color
(uvw.color ==
== green)
green)
Indexers
public
class
{{ with []
publicGetters
class Stuff
Stuff
and setters
private
private int
int x[];
x[];
public
public int
int this[int
this[int i]i] {{
get
get {x[2*i+1]=0;
{x[2*i+1]=0; return
return x[2*i];
x[2*i]; }}
set
set {{ x[2*i]
x[2*i]== value;
value; x[2*i+1]=1;
x[2*i+1]=1; }}
}}
Stuff
Stuff uvw
uvw == new
new Stuff();
Stuff();
uvw[12]
uvw[12] == 55;
55;
uvw[99]
7
uvw[99] == uvw[12]*7
uvw[12]*7 ++ 2;
2;
Enum
enum
enum Color:
Color: byte
byte {{
Red
Red == 1,
1,
Green
Green == 2,
2,
Blue
Blue == 4,
4,
Black
Black == 0,
0,
White
White == Red
Red || Green
Green || Blue,
Blue,
}}
8
Partial Classes
Some fields defined in file 1
Other fields defined in file 2
Why ?
Chi-Wai creates file 1, you create file 2
DataSet
DataSet
DataSet myLocalDB
myLocalDB == new
new DataSet();
DataSet();
.. .. .. .. ..
.. .. .. .. .. /*
/* create
create inside
inside aa table
table called
called books
books */
*/
.. .. .. .. .. /*
/* (this
(this isis shown
shown on
on aa following
following slide)
slide) */
*/
/*
/* now
now use
usebooks
books */
*/
DataTable
DataTable xx == myLocalDB.Tables[books]
myLocalDB.Tables[books]
foreach
foreach (DataRow
(DataRow yy in
in x.Rows)
x.Rows) {{
ifif (y[title]
(y[title]==
== Harry
Harry Potter)
Potter) y[price]++;;
y[price]++;;
}}
11
Connecting to a Database
Create or edit web.config file
Specify iprojsrv, user, password
Give a name
Create a SqlConnection
refer to name
Create a SqlDataAdaptor
embed SQL query string
Connecting to a Database
/*
/* create
create inside
inside aa table
table called
called books
books */
*/
SqlConnection
SqlConnection cc == new
new SqlConnection(
SqlConnection( .. .. .. name
name .. .. .);
.);
string
string qq == select
select title,
title, price
price year
year
from
from products
products
where
where price
price << 100;
100;
SqlDataAdapter
SqlDataAdapter aa == new
new SqlDataAdapter(q,
SqlDataAdapter(q, c);
c);
DataSet
DataSet myLocalDB
myLocalDB == new
new DataSet();
DataSet();
a.Fill(myLocalDB,
a.Fill(myLocalDB, books);
books);
13
Phase 1
Task 1: Schema design
Task 2: Import sample data
Task 3: Modify starter code
14
15
CREATE
CREATETABLE
TABLECustomer
Customer(( ......))
CREATE
CREATETABLE
TABLEInvoice
Invoice(( ......))
......
DROP
DROPTABLE
TABLECustomer
Customer
DROP
DROPTABLE
TABLEInvoice
Invoice
......
16
17
INSERT
INSERTINTO
INTOCustomer
Customer((
VALUES
VALUES(John,
(John,.)
.)
INSERT
INSERTINTO
INTOCustomer
Customer((
VALUES
VALUES(Sue,
(Sue,.)
.)
......
......))
......))
(why ?)
DROP
DROPTABLE
TABLECustomer
Customer
DROP
DROPTABLE
TABLEInvoice
Invoice
......
19
http://iprojsrv.cs.washington.edu/444/Phase
1_Example/
20
21
Substitutes
id for {0}
string
string ss == String.Format(
String.Format(
@SELECT
@SELECT
FROM
FROM .
.
WHERE
WHERE x.customerId
x.customerId == {0}
{0} ,
, id);
id);
Defined in
Provided
StoreContext
StoreContext store
store ==StoreContext.current;
StoreContext.current;
DataSet
DataSet ds
ds == new
new DataSet(
DataSet( ););
DataTable
DataTable invoices
invoices == store.GetDataTable(s,
store.GetDataTable(s, ds,
ds, invoices);
invoices);
/*
/* continued
continued on
on next
next slide.
slide. */
*/
22
23
In you SQL
24
Time Estimate
Task 1: about 9 tables or so
2 hours or more
Task 3:
Need to find out WHAT to mofiy in starter code
May need to DROP TABLEs then go to Task 1
Total time here: anywhere from 2 to 14
25