Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Structured Query Language, SQL. SQL Select Command SELECT * FROM tableName WHERE criteria;

Similar presentations


Presentation on theme: "Introduction to Structured Query Language, SQL. SQL Select Command SELECT * FROM tableName WHERE criteria;"— Presentation transcript:

1 Introduction to Structured Query Language, SQL

2 SQL Select Command SELECT * FROM tableName WHERE criteria;

3 Creating A String Containing SQL Select Command Assuming the CID is selected from a list box: string strSQL = "select * from customer where cid='" + listBox1.SelectedItem + "'";

4 Create SQL Statement from Radiobuttons

5 Code Example string rating; if (radioButton1.Checked) rating = "A"; else if (radioButton2.Checked) rating = "B"; else rating = "C"; string strSQL = "select * from customer where rating='" + rating + "'";

6 SQL Insert Command INSERT INTO tableName VALUES (field values separated by commas); Ex 1. Customer table: CID, CNAME, CITY, RATING. INSERT INTO CUSTOMER VALUES (‘C1’, ‘SMITH’, ‘SF’, ‘A’); Ex 2. Orders table: OID, OrderDate, CID, SalesPerson INSERT INTO ORDERS VALUES (‘O11’, ‘9/28/02’, ‘C1’, ‘Peter’); Ex 3. Employee table: EID, Ename, Sex, Salary, HireDate, DID INSERT INTO EMPLOYEE VALUES(‘e12’,’John Smith’,7500.00,’M’, ‘8/4/2014’,’D2’); Note: No quotation for numeric data.

7 Creating A String Containing SQL Insert Command Assuming the four fields of the new customer record are entered in textboxes: string strSQLInsert; strSQLInsert = "Insert into Customer values ('"; strSQLInsert += textBox1.Text + "','" + textBox2.Text + "','"; strSQLInsert += textBox3.Text + "','" + textBox4.Text + "')";

8 Employee Table Fields: –EID, Ename, Sex, Salary, HireDate, DID Employee Data Entry Form: –Radiobuttons for sex –dateTimePicker for HireDate: dateTimePicker properties: –Format: long,short –Text –Listbox for DID

9 string sex, HireDate, DID, strSQLInsert; if (radioButton1.Checked) sex = "F"; else sex = "M"; HireDate = dateTimePicker1.Text; DID = listBox1.SelectedItem.ToString(); strSQLInsert = "Insert into Customer values ('" + textBox1.Text + "','" + textBox2.Text; strSQLInsert += "'," + textBox3.Text + ",'" + sex + "','" + HireDate + "','" + DID + "')"; MessageBox.Show(strSQLInsert);

10 String.Format Method http://msdn.microsoft.com/en- us/library/system.string.format(v=vs.110).aspx String sqlInsert= String.Format(“insert into customer values (‘{0:s}’,’{1:s}’,’{2:s}’,’{3:s}’)”,textBox1.Text,textBox2.Text,t extBox3.Text,textBox4.Text);

11 SQL Delete Command DELETE FROM tableName WHERE criteria; Ex 1. Delete a record from the Customer table. DELETE FROM CUSTOMER WHERE CID = ‘C1’;

12 Creating A String Containing SQL Delete Command Assuming the deleted record’s ID is selected from a list box: string strSQL = "delete from customer where cid = '" + listBox1.SelectedItem + "'";

13 SQL Update Command UPDATE tableName SET field = new value WHERE criteria; Ex. UPDATE CUSTOMER SET RATING = ‘A’ WHERE CID=‘C1’;

14 Creating A String Containing SQL Update Command Assuming the CID is selected from a list box, and the new rating is entered in a text box: string strSQL = "Update customer set rating = '" + newRating + "' where cid='" + listBox1.SelectedItem + "'";


Download ppt "Introduction to Structured Query Language, SQL. SQL Select Command SELECT * FROM tableName WHERE criteria;"

Similar presentations


Ads by Google