Download Building CASE Tools Data Base Using Microsoft SQL

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Open Database Connectivity wikipedia , lookup

SQL wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Database wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Functional Database Model wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Clusterpoint wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Transcript
Building and using CASE Tools Data
Base with Microsoft SQL-Server and
C#
common SQL keywords
Figure 20.10. Common SQL keywords.
SQL
keyword
Description
SELECT
Retrieves data from one or more tables.
FROM
Specifies the tables involved in a query. Required in every query.
WHERE
Specifies optional criteria for selection that determine the rows to be
retrieved, deleted or updated.
ORDER BY
Specifies optional criteria for ordering rows (e.g., ascending,
descending).
INNER JOIN
Specifies optional operator for merging rows from multiple tables.
INSERT
Inserts rows in a specified table.
UPDATE
Updates rows in a specified table.
DELETE
Deletes rows from a specified table.
Basic SELECT Query
SELECT * FROM tableName
in which the asterisk (*) indicates that all the columns from the tableName table
should be retrieved. For example, to retrieve all the data in the Authors table, use
SELECT * FROM Authors
SELECT AuthorID, LastName FROM Authors
. WHERE Clause
SELECT columnName1, columnName2, ... FROM tableName WHERE criteria
For example, to select the Title, EditionNumber and Copyright columns from table
Titles for which the Copyright date is more recent than 2004, use the query
SELECT Title, EditionNumber, Copyright
FROM Titles
WHERE Copyright > '2004'
A pattern that contains a percent character (%) searches for strings that have zero or
more characters at the percent character's position in the pattern. For example, the
following query locates the rows of all the authors whose last names start with the
letter D:
SELECT AuthorID, FirstName, LastName
FROM Authors
WHERE LastName LIKE 'D%'
. WHERE Clause
An underscore (_) in the pattern string indicates a single wildcard character at that
position in the pattern. For example, the following query locates the rows of all the
authors whose last names start with any character (specified by _), followed by the
letter h, followed by any number of additional characters (specified by %):
SELECT AuthorID, FirstName, LastName
FROM Authors
WHERE LastName LIKE '_h%'
INSERT Statement
The INSERT statement inserts a row into a table. The basic form of this statement is
INSERT INTO tableName ( columnName1, columnName2, ..., columnNameN )
VALUES ( value1, value2, ..., valueN )
INSERT INTO Authors ( FirstName, LastName )
VALUES ( 'Sue', 'Smith' )
inserts a row into the Authors table. The statement indicates that the values 'Sue'
and 'Smith' are provided for the FirstName and LastName columns, respectively.
UPDATE Statement
An UPDATE statement modifies data in a table. The basic form of the UPDATE statement
is
UPDATE tableName
SET columnName1 = value1, columnName2 = value2, ..., columnNameN =
valueN
WHERE criteria
UPDATE Authors
SET LastName = 'Jones'
WHERE LastName = 'Smith' AND FirstName = 'Sue'
DELETE Statement
A DELETE statement removes rows from a table. The basic form of a DELETE
statement is
DELETE FROM tableName WHERE criteria
DELETE FROM Authors
WHERE LastName = 'Jones' AND FirstName = 'Sue'
deletes the row for Sue Jones in the Authors table. DELETE statements can delete
multiple rows if the rows all meet the criteria in the WHERE clause
SQL-Server Database classes
• Namespace System.Data is the root namespace
• Namespace System.Data.SqlClient contains classes that are
optimized to work with Microsoft SQL Server databases.
• An object of class SqlConnection (namespace
System.Data.SqlClient) represents a connection to a data
source specifically a SQL Server database.
• A SqlConnection object keeps track of the location of the data
source and any settings that specify how the data source is to
be accessed.
• An object of class SqlCommand (namespace
System.Data.SqlClient) represents a SQL command that a
DBMS can execute on a database.
• A program can use SqlCommand objects to manipulate a
data source through a SqlConnection.
SQL-Server Database classes
• The program must open the connection to the data source before
executing one or more SqlCommands and close the connection
once no further access to the data source is required.
• Class DataTable (namespace System.Data) represents a table of
data.
• A DataTable contains a collection of DataRows that represent the
table's data.
• A DataTable also has a collection of DataColumns that describe the
columns in a table. DataRow and DataColumn are both located in
namespace System.Data.
• An object of class System.Data.DataSet, which consists of a set of
DataTables and the relationships among them, represents a cache
of data that a program stores temporarily in local memory.
• The structure of a DataSet mimics the structure of a relational
database.
SQL server Database
Creating SQL Database file And
Displaying a Database Table in a
DataGridView
Create new windows application project
Click on add new data source option in data source
window
In choose data source type dialog box select Dataset
then click next
In choose your data connection dialog box click on
new connection button
Select Microsoft SQL Server Database file option as a
data source then click on continue button
In Add connection dialog box enter a name for the
database file ex: database1 then click on ok button
A massage box will appear to tell you that this is a
new database would you like to create it? Click on yes
button
Now a new database is created with a specific
connection string that will be used to connect the
application to this database.
Click next
Click on NO button
Click on next
Click on previous button
Click on finish button
Open server explorer then click on database1.mdf
then right click on Tables and choose Add new Table
option
Add new filed "name" to the table as following
To save changes on the table click on update then
Click on update database button
To change table name to "student"
Update the database then close the table
On data source window right click on
database1Dataset then choose Configure data source
with wizard then choose tables
Then choose table click finish
Drag and drop student table from data source
window to the form
Run the program
Add new record then click save
Remove the record then click save
How Data Binding Works
• The technique through which GUI controls are
connected to data sources is known as data binding.
• The IDE allows controls, such as a DataGridView, to be
bound to a data source, such as a DataSet that
represents a table in a database.
• Any changes you make through the application to the
underlying data source will automatically be reflected
in the way the data is presented in the data-bound
control (e.g., the DataGridView).
• Likewise, modifying the data in the data-bound control
and saving the changes updates the underlying data
source.
Create new windows application then create SQLserver database with following tables
Course
Cno
int
Cmark
int
stId
int
Student
stId
int
stName
Nvarchar(50)
Drag and drop student table and Add a button “print
names” to your form to print all student names as
following
To print all student names in student table add the
following code to the button “print names” event
handler
Run the program and add the following students to
student table
Then click the button the following message box will
be shown with names of all students
Add three textboxes and three labels to the form as
following then add new button “Add course” to add
new course to table course in the database
In button “Add course” event handler add the
following code
Add new button “print AVG” to the form to find the
average for all marks in course table as following
Add the following code to button “print AVG” event
handler
Add the following courses to course table then find
the average of all marks
Click on print AVG button