Introduction to Database C# MySQL onnect-C-to-MySQL 1.

Slides:



Advertisements
Similar presentations
PHP II Interacting with Database Data. The whole idea of a database-driven website is to enable the content of the site to reside in a database, and to.
Advertisements

PHP SQL. Connection code:- mysql_connect("server", "username", "password"); Connect to the Database Server with the authorised user and password. Eg $connect.
Chapter 10 ADO. What is ADO? ADO is a Microsoft technology ADO stands for ActiveX Data Objects ADO is a programming interface to access data in a database.
Manipulating MySQL Databases with PHP. PHP and mySQL2 Objectives Connect to MySQL from PHP Learn how to handle MySQL errors Execute SQL statements with.
Objectives Connect to MySQL from PHP
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Concepts of Database Management Sixth Edition
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions.
1 CS428 Web Engineering Lecture 23 MySQL Basics (PHP - VI)
PHP Programming. Topics Background and History of PHP Installation Comments in PHP Variables Conditions Loops Functions File Handling Database Handling.
SJSU CS157B Dr. Lee1  2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section PHP and MySQL.
Lecture 3 – Data Storage with XML+AJAX and MySQL+socket.io
PHP1-1 PHP & SQL Xingquan (Hill) Zhu
CSCI 6962: Server-side Design and Programming
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
INTERNET APPLICATION DEVELOPMENT For More visit:
ASP.NET Programming with C# and SQL Server First Edition
Chapter 7 PHP Interacts with Ms. Access (Open DataBase Connectivity (ODBC))
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
Concepts of Database Management Seventh Edition
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
ADO.NET A2 Teacher Up skilling LECTURE 3. What’s to come today? ADO.NET What is ADO.NET? ADO.NET Objects SqlConnection SqlCommand SqlDataReader DataSet.
FUNCTIONS AND STORED PROCEDURES & FUNCTIONS AND PROTECTING A DB AND PHP (Chapters 9, 15, 18)
Introduction to MySQL Lab no. 10 Advance Database Management System.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
MySQL Connection using ADO.Net Connecting to MySQL from.NET Languages.
(Chapter 10 continued) Our examples feature MySQL as the database engine. It's open source and free. It's fully featured. And it's platform independent.
Concepts of Database Management Seventh Edition
Introduction to CS520/CS596_026 Lecture Two Gordon Tian Fall 2015.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
Summary 1 Gordon Tian CS520/CS596_026. HTTP Browser – Chrome | FireFox XML/HTML/CSS3/JavaScript/REST Web Server – Nginx PHP/Python/Perl/Java/Ruby/Node.js.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
Distributed Database Systems INF413. ADO.NET is a set of classes that comes with the Microsoft.NET framework to facilitate data access from managed languages.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
CHAPTER 10 PHP MySQL Database
>> PHP: MySQL & CRUD. R ecall Database Tables Records is composed of Operations (CRUD) Create Retrieve Update Delete DBMS Access Control MySQL phpMyAdmin.
Architecture Multi Layered Architecture (n-tier): Application: Model Controllers Database Access Graphical User Interface (GUI): Forms, components, controls.
CP476 Internet Computing Perl CGI and MySql 1 Relational Databases –A database is a collection of data organized to allow relatively easy access for retrievals,
MySQL MySQL and PHP – interacting with a database.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
Introduction to MySQL  Working with MySQL and MySQL Workbench.
Software-Projekt 2008 Seminarvortrag“Short tutorial of MySql“ Wei Chen Verena Honsel.
.NET Data Access and Manipulation
Outline  XAMPP  XAMPP Install  Put php and HTML documents  Windows and Mac Version  Security.
COM621: Advanced Interactive Web Development Lecture 10 PHP and MySQL.
C# MySQL onnect-C-to-MySQL 1.
ASP.NET Programming with C# and SQL Server First Edition
Introduction to Dynamic Web Programming
CS320 Web and Internet Programming SQL and MySQL
How to Create Login Form using vb.net and SqlServer Database
Lecture 6 VB.Net SQL Server.
Introduction to Web programming
Session 4 PHP & MySQL.
Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
SQL commands from C# and ASP.net
ISC440: Web Programming 2 Server-side Scripting PHP 3
Chapter 8 Working with Databases and MySQL
MySQL Connection using ADO.Net
Chapter 10 ADO.
PHP: Database connection
Tutorial 6 PHP & MySQL Li Xu
CS3220 Web and Internet Programming SQL and MySQL
CS3220 Web and Internet Programming SQL and MySQL
Presentation transcript:

Introduction to Database C# MySQL onnect-C-to-MySQL 1

โครงสร้างฐานข้อมูล เชิงสัมพันธ์ Relational Data Model ข้อมูลถูกจัดเก็บในฐานข้อมูลในรูปแบบ ตาราง (Table) ในแต่ละตารางประกอบ Field ( หรือ Attribute) ต่างๆ และแต่ละ Field อาจจะมีชนิดของ ข้อมูล (Data Type) ที่แตกต่างกันไป ข้อมูลที่จัดเก็บอยู่ในแต่ละตาราง ถูก จัดเก็บใน รูปแบบของ Record ( หรือ Row หรือ Tuple) 2

ชนิดของข้อมูล (Data Type) Data Type Numeric data types for integers and real numbers Characters Booleans Fixed-length strings Variable-length strings Date, time, timestamp Money Other special data types 3

ตัวอย่าง Table 4 หมายเหตุ : NULL หมายถึง ไม่มีข้อมูล (Empty) (Fields) (Records)

MySQL 5 MySQL, the most popular Open Source SQL database management system, is developed, distributed, and supported by Oracle Corporation. MySQL databases are relational. ฐานข้อมูลที่สร้างขึ้นโดยใช้ MySQL ถูกจัดเก็บอยู่ใน รูปแบบของ Relational Database

HeidiSQL 6 HeidiSQL is a useful and reliable tool designed for web developers using the popular MySQL server, and Microsoft SQL databases. It enables you to browse and edit data, create and edit tables, views, procedures, triggers and scheduled events. Also, you can export structure and data either to SQL file, clipboard or to other servers. Main Features  Connect to multiple servers in one window.  Connect to servers via command line.  Create and edit tables, views, stored routines, triggers, and scheduled events.  Export from one server/database directly to another server/database.  Manage user-privileges.  Import text-files.  Export table rows as CSV, HTML, XML, SQL, LaTeX, Wiki Markup, and PHP Array.  Browse and edit table-data using a comfortable grid.  Bulk edit tables (move to db, change engine, collation etc.)  And much more

Using HeidiSQL to Maintain MySQL Database Server 7 First, start XAMPP web server. XAMPP is a free and open source cross-platform web server solution stack package, consisting mainly of the Apache HTTP Server, MySQL database, and interpreters for scripts written in the PHP and Perl programming languages.

Create Session for connecting to MySQL Database Server using HeidiSQL 8

Create MySQL Database Server using HeidiSQL 9 เพื่อให้ Support ภาษาไทย

Create Table

Create Fields 11

Create Primary Key 12

Maintaining Data using HeidiSQL 13

Maintaining Data using SQL Command - Insert 14 Insert Command Approach 1 Insert into Table Name Values (data 1, data 2, …, data n ) Approach 2 Insert into Table Name (Field 1, Field 2, …, Field n ) Values (data 1, data 2, …, data n )

Maintaining Data using SQL Command - Insert 15 Approach 1 Approach 2

Maintaining Data using SQL Command - Update 16 Update Command Update Table Name Set Attribute Name = New Value Where Condition; // ถ้าไม่มีเงื่อนไข จะ update ทุก record ( ระวัง !!!)

Maintaining Data using SQL Command - Delete 17 Delete Command Delete From Table Name Where Condition; // ถ้าไม่มีเงื่อนไข จะ delete ทุก record ( ระวัง !!!) ก่อน ลบ หลัง ลบ

การสืบค้นข้อมูล (Query) - Select 18 Select Command Select * From Table Name; // มาทุก Record, ทุก Field Select Attribute List From Table Name; // มาทุก Record, เฉพาะ Field ที่ระบุหลัง Select Select * From Table Name; Where Condition; // มาทุก Field แต่มาเฉพาะ Record ที่ตรงตามเงื่อนไข Select Attribute List From Table Name; Where Condition; // มา เฉพาะ Field ที่ระบุหลัง Select และมาเฉพาะ Record ที่ตรงตามเงื่อนไข

การสืบค้นข้อมูล (Query) - Select 19

Downloading Connector/Net First make sure you have downloaded and installed the MySQL Connector/NET (Database Driver) from the MySQL /connector/net/6.1.html /connector/net/6.1.html ตอนนี้มีตัว update กว่านี้แล้ว 20

สร้าง Form 21

ตัวอย่าง Browse ไปที่ C:\Program Files\MySQL\MySQL Connector Net 6.8.3\Assemblies\v4.5 22

//Add MySql Library using MySql.Data.MySqlClient; Then declaring and initializing the variables that we will use: connection: will be used to open a connection to the database. server: indicates where our server is hosted, in our case, it's localhost. database: is the name of the database we will use, in our case it's the database we already created earlier which is connectcsharptomysql. uid: is our MySQL username. password: is our MySQL password. connectionString: contains the connection string to connect to the database, and will be assigned to the connection variable. 23

private MySqlConnection connection; private string server; private string database; private string uid; private string password; public Form1() { InitializeComponent(); server = "localhost"; database = “university"; uid = "root"; password = ""; string connectionString; connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" +password+ ";charset=tis620;"; connection = new MySqlConnection(connectionString); } 24

Working with Insert, Update, Select, Delete Usually, Insert, update and delete are used to write or change data in the database, while Select is used to read data. For this reason, we have different types of methods to execute those queries. The methods are the following: ExecuteNonQuery: Used to execute a command that will not return any data, for example Insert, update or delete. ExecuteReader: Used to execute a command that will return 0 or more records, for example Select. ExecuteScalar: Used to execute a command that will return only 1 value, for example Select Count(*). 25

private bool OpenConnection() { try { connection.Open(); return true; } catch (MySqlException ex) { //When handling errors, you can your application's response based on the //error number. //The two most common error numbers when connecting are as follows: //0: Cannot connect to server. //1045: Invalid user name and/or password. switch (ex.Number) { case 0: MessageBox.Show("Cannot connect to server. Contact administrator"); break; case 1045: MessageBox.Show("Invalid username/password, please try again"); break; } return false; } 26

private bool CloseConnection() { try { connection.Close(); return true; } catch (MySqlException ex) { MessageBox.Show(ex.Message); return false; } 27

C# Programming 28 Steps Start with Insert, update and delete, which are the easiest. The process to successfully execute a command is as follows: 1.Open connection to the database. 2.Create a MySQL command. 3.Assign a connection and a query to the command. This can be done using the constructor, or using theConnection and the CommandText methods in the MySqlCommand class. 4.Execute the command. 5.Close the connection.

private void insert_Click (object sender, EventArgs e) { //open connection if (this.OpenConnection() == true) { string query = "INSERT INTO Student VALUES("+studentID.Text+", '"+studentName.Text+"', "+gpa.Text+")"; //create command and assign the query and connection from the constructor MySqlCommand cmd = new MySqlCommand(query, connection); //Execute command cmd.ExecuteNonQuery(); //close connection this.CloseConnection(); } 29

Steps for select To execute a Select statement, we add a few more steps, and we use the ExecuteReader method that will return a dataReader object to read and store the data or records. 1.Open connection to the database. 2.Create a MySQL command. 3.Assign a connection and a query to the command. This can be done using the constructor, or using theConnection and the CommandText methods in the MySqlCommand class. 4.Create a MySqlDataReader object to read the selected records/data. 5.Execute the command. 6.Read the records and display them or store them in a list. 7.Close the data reader. 8.Close the connection. 30

private void search_Click(object sender, EventArgs e) { string query = "SELECT * FROM Student WHERE studentID = " + studentIDSearch.Text; //Open connection if (this.OpenConnection() == true) { //Create Command MySqlCommand cmd = new MySqlCommand(query, connection); //Create a data reader and Execute the command MySqlDataReader dataReader = cmd.ExecuteReader(); //Read the data and store them in the list while (dataReader.Read()) { studentID.Text = dataReader["StudentID"].ToString(); studentName.Text = dataReader["StudentName"].ToString(); gpa.Text = dataReader["GPA"].ToString(); } //close Data Reader dataReader.Close(); //close Connection this.CloseConnection(); } 31

private void update_Click(object sender, EventArgs e) { //Open connection if (this.OpenConnection() == true) { string query = "UPDATE Student SET studentName='"+studentName.Text+"', gpa="+gpa.Text+ " WHERE studentID="+studentID.Text; //create mysql command MySqlCommand cmd = new MySqlCommand(); //Assign the query using CommandText cmd.CommandText = query; //Assign the connection using Connection cmd.Connection = connection; //Execute query cmd.ExecuteNonQuery(); //close connection this.CloseConnection(); } } 32

private void delete_Click(object sender, EventArgs e) { string query = "DELETE FROM Student WHERE studentID = " + studentID.Text; if (this.OpenConnection() == true) { MySqlCommand cmd = new MySqlCommand(query, connection); cmd.ExecuteNonQuery(); this.CloseConnection(); } 33