Databases – Unit 3 & 4. SQL is not restricted to a specific database, however in class we will be...
-
Upload
anis-shepherd -
Category
Documents
-
view
213 -
download
1
description
Transcript of Databases – Unit 3 & 4. SQL is not restricted to a specific database, however in class we will be...
![Page 1: Databases – Unit 3 & 4. SQL is not restricted to a specific database, however in class we will be looking at the two major database programmes in use.](https://reader035.fdocuments.in/reader035/viewer/2022070605/5a4d1acb7f8b9ab05996f5ff/html5/thumbnails/1.jpg)
Databases – Unit 3 & 4Databases – Unit 3 & 4
![Page 2: Databases – Unit 3 & 4. SQL is not restricted to a specific database, however in class we will be looking at the two major database programmes in use.](https://reader035.fdocuments.in/reader035/viewer/2022070605/5a4d1acb7f8b9ab05996f5ff/html5/thumbnails/2.jpg)
SQL is not restricted to a specific database, however in class we will be looking at the two major database programmes in use
Access – basic database, usable for academic purposes and basic data purposes
SQL Server – industry recognised database, more adaptable, more ‘in depth’
![Page 3: Databases – Unit 3 & 4. SQL is not restricted to a specific database, however in class we will be looking at the two major database programmes in use.](https://reader035.fdocuments.in/reader035/viewer/2022070605/5a4d1acb7f8b9ab05996f5ff/html5/thumbnails/3.jpg)
Made up of US English based ‘key words’
US? US spelling of words is used throughout programming languages due to their origins:
Colour is coded as color (feel free to twitch)
![Page 4: Databases – Unit 3 & 4. SQL is not restricted to a specific database, however in class we will be looking at the two major database programmes in use.](https://reader035.fdocuments.in/reader035/viewer/2022070605/5a4d1acb7f8b9ab05996f5ff/html5/thumbnails/4.jpg)
ADO.Net provides a ‘namespace’ for all possible database connections
System.Data.OleDb System.Data.SqlClient
Each namespace contains all the functions you will need to manipulate the database and data inside it
Access databaseSQL Server database
![Page 5: Databases – Unit 3 & 4. SQL is not restricted to a specific database, however in class we will be looking at the two major database programmes in use.](https://reader035.fdocuments.in/reader035/viewer/2022070605/5a4d1acb7f8b9ab05996f5ff/html5/thumbnails/5.jpg)
ADO has different versions as it has developed
In version 2.1 more objects were released, ADOX is essentially: ADO eXtrensions
In ADOX all objects use Generalisation to define objects (remember the essays?)
![Page 6: Databases – Unit 3 & 4. SQL is not restricted to a specific database, however in class we will be looking at the two major database programmes in use.](https://reader035.fdocuments.in/reader035/viewer/2022070605/5a4d1acb7f8b9ab05996f5ff/html5/thumbnails/6.jpg)
DatasetDataset – stores all data held in ‘cache’
DataAdapterDataAdapter – represents a bridge between the Dataset and Database holding the:▪ Connection StringConnection String▪ SQL CommandsSQL Commands
ConnectionConnection – the address of your DatasourceDatasource (aka database)
![Page 7: Databases – Unit 3 & 4. SQL is not restricted to a specific database, however in class we will be looking at the two major database programmes in use.](https://reader035.fdocuments.in/reader035/viewer/2022070605/5a4d1acb7f8b9ab05996f5ff/html5/thumbnails/7.jpg)
Before you can create any functional SQL your application must be able to connect to your database.
All database connections are called a ‘connection string’
These can be set as global variables or ‘per page’ connections may also be set
![Page 8: Databases – Unit 3 & 4. SQL is not restricted to a specific database, however in class we will be looking at the two major database programmes in use.](https://reader035.fdocuments.in/reader035/viewer/2022070605/5a4d1acb7f8b9ab05996f5ff/html5/thumbnails/8.jpg)
public void ConnectToAccess() { System.Data.OleDb.OleDbConnection conn = new
System.Data.OleDb.OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data source= C:\Documents and Settings\username\" + @"My Documents\AccessFile.mdb";
try { conn.Open(); } catch (Exception ex) { MessageBox.Show("Failed to connect to data
source"); }
finally { conn.Close(); } }
Call an Call an instance of instance of ADO.Net ADO.Net
namespacenamespace
Create the Create the ‘string’ ‘string’ address for
address for your databaseyour database
Try Catch Try Catch Statement Statement
![Page 9: Databases – Unit 3 & 4. SQL is not restricted to a specific database, however in class we will be looking at the two major database programmes in use.](https://reader035.fdocuments.in/reader035/viewer/2022070605/5a4d1acb7f8b9ab05996f5ff/html5/thumbnails/9.jpg)
Why create a table?
Temporary tables may be created, filled and deleted
Why? Memory Management – A large update is quicker than many small updates
![Page 10: Databases – Unit 3 & 4. SQL is not restricted to a specific database, however in class we will be looking at the two major database programmes in use.](https://reader035.fdocuments.in/reader035/viewer/2022070605/5a4d1acb7f8b9ab05996f5ff/html5/thumbnails/10.jpg)
try { conn.Open();
string strTemp = " [FirstName] Text, [LastName] Text ";
OleDbCommand myCommand = new OleDbCommand();
myCommand.Connection = conn;
myCommand.CommandText = "CREATE TABLE tbl_People(" + strTemp + ")";
myCommand.ExecuteNonQuery();}
![Page 11: Databases – Unit 3 & 4. SQL is not restricted to a specific database, however in class we will be looking at the two major database programmes in use.](https://reader035.fdocuments.in/reader035/viewer/2022070605/5a4d1acb7f8b9ab05996f5ff/html5/thumbnails/11.jpg)
try { conn.Open();
string strTemp = " [FirstName] = " + txtFname.text + ", [LastName] = " + txtLname.Text;
OleDbCommand myCommand = new OleDbCommand();
myCommand.Connection = conn;
myCommand.CommandText = “INSERT INTO tbl_People(" + strTemp + ")";
myCommand.ExecuteNonQuery();}
![Page 12: Databases – Unit 3 & 4. SQL is not restricted to a specific database, however in class we will be looking at the two major database programmes in use.](https://reader035.fdocuments.in/reader035/viewer/2022070605/5a4d1acb7f8b9ab05996f5ff/html5/thumbnails/12.jpg)
The Datagrid is a massively useful tool for showing multiple records of data
Using a simple statement records can be shown as a read only table or an editable form
![Page 13: Databases – Unit 3 & 4. SQL is not restricted to a specific database, however in class we will be looking at the two major database programmes in use.](https://reader035.fdocuments.in/reader035/viewer/2022070605/5a4d1acb7f8b9ab05996f5ff/html5/thumbnails/13.jpg)
private void Form1_Load(object sender, System.EventArgs e){Try{OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM Student","Provider=Microsoft.JET.OLEDB.4.0;data source=C:\\mcb.mdb" );
DataSet ds = new DataSet();
da.Fill(ds, “Fname");
dataGrid1.DataSource = ds.Tables[“FName"].DefaultView ;} catch (Exception ex) { MessageBox.Show("Failed to connect to data source"); }
finally { conn.Close(); } }
![Page 14: Databases – Unit 3 & 4. SQL is not restricted to a specific database, however in class we will be looking at the two major database programmes in use.](https://reader035.fdocuments.in/reader035/viewer/2022070605/5a4d1acb7f8b9ab05996f5ff/html5/thumbnails/14.jpg)
Create yourself a set of revision notes for the SQL code used here. This should include:
A Table of Keywords and their uses An explanation of ADO An explanation of connection strings An explanation of a dataset