How to use MySql With C# Ross Sherman 1. Using MySql MySql is free. You could download it from:

Slides:



Advertisements
Similar presentations
Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control.
Advertisements

How to use a scanner Throughout this slide show there will be hyperlinks (highlighted in blue) follow the hyperlinks to navigate to the specified Topic.
DNR-322L & DNR-326.
Chapter 14.3 LINQ to SQL Programming in Visual Basic 2010: The Very Beginner’s Guide by Jim McKeown Databases – Part 3.
Chapter 18 - Data sources and datasets 1 Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
TUTORIAL (1) Software installation Written by: Eng. Ahmed Mohamed Abdel Ghafar, Edited by Eng. Muhammed Hammad, Eng. Hamdy Soltan & Eng. Osama Talaat.
Mozilla Thunderbird Roderick Bautista Cheryl Knott Gyeong Lee Lee Raynes Kelly Santos.
CONFIGURING WINDOWS SERVER MIS 424 Professor Sandvig.
1 Working with MS SQL Server. 2 Objectives You will be able to Use Visual Studio for GUI based interactive access to a Microsoft SQL Server database.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
INTERNET APPLICATION DEVELOPMENT For More visit:
Microsoft Azure Introduction ISYS 512. Microsoft Azure Microsoft Azure is a cloud.
Chapter 7 PHP Interacts with Ms. Access (Open DataBase Connectivity (ODBC))
Database Applications – Microsoft Access Lesson 9 Designing Special Queries Updated 4/11.
Databases in Visual Studio. Database in VisualStudio An MS SQL database are built in Visual studio The Name can be something like ”(localdb)\Projects”
11 Updating a Database Table Textbook Chapter 14.
IT 211 Project Integration and Deployment Lab #11.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Once you have contacted me to set up a new account, your computer needs to be configured to dial into.DevlinEx and use our servers for things like .
Form and Graphical User Interfaces. Lesson plan More about queries More about entering data into a table Form.
11 Web Services. 22 Objectives You will be able to Say what a web service is. Write and deploy a simple web service. Test a simple web service. Write.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
Office of Education Technology (OET) Active Directory Bulk Import Saturday March 11, 2006 /8:30 – 10:30 AM.
1 Working with MS SQL Server Textbook Chapter 14.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Working with MSSQL Server Code:G0-C# Version: 1.0 Author: Pham Trung Hai CTD.
C# Tutorial -1 ASP.NET Web Application with Visual Studio 2005.
MySQL Connection using ADO.Net Connecting to MySQL from.NET Languages.
Unit 8.3 Learning Objectives Insert users into the ASP.NET Membership system from code Capture data being sent to the database Capture Exceptions that.
CIS 338: Using Queries in Access as a RecordSource Dr. Ralph D. Westfall May, 2011.
How to install Office 2007  Step 1: Insert the Microsoft Office 2007 Enterprise CD. If the Setup Wizard does not automatically begin, then click Start.
Present :Arezoo Mollahasani. Step 1  Define your server connection Open MySQL WorkBench and click New Server Instance on the right of the window.
Sample Application Multi Layered Architecture (n-tier): –Graphical User Interface (GUI): Forms, components, controls The Visual Designer in Visual Studio.
CSC 240 (Blum)1 Forms and Importing Data in Access.
ADO.NET Objects – Data Providers Dr. Ron Eaglin. Requirements Visual Studio 2005 Microsoft SQL Server 2000 or 2005 –Adventure Works Database Installed.
Command Object’s ExecuteNonQuery Method ISYS 512.
1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
Introduction to ADO.Net and VS Database Tools and Data Binding ISYS 350.
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.
Installing SAS 1. Requirements If you do not have an old copy of SAS installed on your computer, go directly to Slide 6. Make sure you have uninstalled.
All information's of PLINQO in this Document, I got it from: So, you could visit the link above to research.
Task #1 Create a relational database on computers in computer classroom 308, using MySQL server and any client. Create the same database, using MS Access.
Managing Database With Oracle Replacement for Ch10 COP 4708.
Architecture Multi Layered Architecture (n-tier): Application: Model Controllers Database Access Graphical User Interface (GUI): Forms, components, controls.
Using ADO.Net to Build a Login System Dr. Ron Eaglin.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
VB.NET and Databases. ADO.NET VB.Net allows you many ways to connect to a database. The technology used to interact with a database or data source is.
Access Queries and Forms. Adding a New Field  To insert a field after you have saved your table, open Access, and open the table  It is easier to add.
Customize Your View of Data Training Presentation for Supply Chain Platform: BAE Systems May 2015.
Software-Projekt 2008 Seminarvortrag“Short tutorial of MySql“ Wei Chen Verena Honsel.
Copyright © 2014 Pearson Canada Inc. Ext. 5b-1 Copyright © 2014 Pearson Canada Inc. Application Extension 5b Using Microsoft Access Part 2: Using Information.
COM621: Advanced Interactive Web Development Lecture 10 PHP and MySQL.
C# MySQL onnect-C-to-MySQL 1.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 11 Creating Web Applications and Writing Data to a Database.
3 A Guide to MySQL.
Common SQL keywords. Building and using CASE Tools Data Base with Microsoft SQL-Server and C#
Creating Data Base & Sql Data Source
, MS-Access, QBE, Access/Oracle
An authorized user can make payments on your account by logging on with their own username and password. Click on the Authorized Users tab to add an authorized.
Introduction to Web programming
Unit 9.2 Database access from code Database Cycle Review
Aqua Data Studio.
Creating and Modifying Queries
© NTT Communications 2017 All Rights Reserved
Lecturer: Yong Liu Contact me at:
MySQL Connection using ADO.Net
Creating Data Base & Sql Data Source
Install MySQL Community Server and MySQL Workbench
Presentation transcript:

How to use MySql With C# Ross Sherman 1

Using MySql MySql is free. You could download it from: When the setup wizard completes check Launch the MySQL Instance Configuration Wizard and click Finish Choose Development Machine and leave the defaults as they are Choose a password Add at least one user account Choose defaults for the rest of the screens Once installed, start the MySQL Workbench; click Open Connection to Start Querying

create database videostore; use videostore; create table moviesrented (personid int(4), movieid int(4) ); create table customer (id int(4), lastname varchar(12), firstname varchar(12), varchar(12), pass varchar(12) ); create table movies (id int(4), moviename varchar(20), numberavailable int (3)); Creating a new database in MySql; creating new tables in the database From Professor Gueorguieva’s database lecture: SQL Server Data Type Corresponding C# or.NET Framework Data Type bitbool datetimedateTime decimal floatdouble int moneydecimal ncharstring nvarcharstring

Inserting values into your database’s tables INSERT INTO customer VALUES INSERT INTO movies VALUES ('1333','Mission_Impossible','3'), ('1334','Jack_Reacher','4'), ('1335','Admission', '2'); INSERT INTO moviesrented VALUES ('1212','1334'), ('1212','1335'), ('1132','1334');

Querying your database Select * from customer; select max(id) from customer; select id from movies where id = 1212; select * from customer where firstname like ‘P%’; select customer.id as IDNameCustID, customer.firstname, moviesrented.persionid, movies.moviename from moviesrented, customer, movies where moviesrented.persionid = customer.id and moviesrented.movieid = movies.id;

To delete a row in a table: delete from moviesrented where movieid = 1212; To delete all rows from a table: delete from moviesrented; To drop a table from your database: drop table moviesrented; To add a new column to an existing table: alter table customer add birthdate date; Deleting rows, dropping tables, and adding columns

1) You might run into a problem when you try to delete rows. To fix this problem go to the Edit tab in the MySql Workbench and click on preferences. 2) Once you have clicked on preferences, click on SQL Queries and make sure that “Safe Updates” is unchecked. If it is checked, uncheck it, press ok, and restart MySql for the change to take affect. If you run into a problem deleting rows:

Connecting Visual Studio to MySql: First, Create a new Windows Forms Project.

9 Next, you will need to add MySql.data as a reference. Adding MySQL.data as a reference: In the Solution Explorer, click on the name of your presentation, right click on references, click on add reference.

After clicking on add reference you will be taken to the reference manager form. In Reference Manager form, select Extensions, find MySql.Data and press the check box next to MySq.Data, them click on ok.

11 At this point, you have created a database and you have connected that database to MySql. Now, you need to query and manipulate the data from your database in your C# code. Go to the design section of your newly created windows form and add a button to it. Double click the button to go to the code.

At the top, include the line: using MySql.Data.MySqlClient;

In the button’s code include the following lines: private void button1_Click(object sender, EventArgs e) { try { string connectionInfo = "datasource = localhost; port = 3306; username = root; password = csi"; MySqlConnection connect = new MySqlConnection(connectionInfo); connect.Open(); MessageBox.Show("Connected"); connect.Close(); } catch(Exception ex) { MessageBox.Show(ex.Message); }

To find out what to put into: string myConnection = "datasource = localhost; port = 3306; username = root; password = csi"; go to the MySql workbench and press ctrl+u. The following screen should appear: The password is the password you created. If you are using the school’s computer the password is csi.

How to use MySql to create a login. 1)Place two labels, two textboxes, and a button on your form. 2) Double click on the button to edit the code for the button. In my code on the next slide: FirstName’s textbox is textBox1 and Password’s textbox is textBox2 (I renamed the text but not the actual textboxes). The login button is button1.

private void button1_Click(object sender, EventArgs e) { try { string connectionInfo = "datasource = localhost; port = 3306; username = root;” + “password = csi"; MySqlConnection connect = new MySqlConnection(connectionInfo); MySqlCommand SQLCommand1 = new MySqlCommand("select * from videostore.customer” + “where firstname = '"+ textBox1.Text +"‘” + “and pass = '"+ textBox2.Text +"';", connect); MySqlDataReader reader; connect.Open(); reader = SQLCommand1.ExecuteReader(); int count = 0; while (reader.Read()) { count = count + 1; } Check the entered firstname and password with the customer table of the videostore database.

if (count == 1) { MessageBox.Show("Firstname and password is correct"); } else if (count > 1) { MessageBox.Show("Duplicate Firstname and password. You are not allowed access"); } else MessageBox.Show("Firstname and password is not correct"); connect.Close(); } // end of try block catch(Exception ex) { MessageBox.Show(ex.Message); } } // end of login button code Login Button Code Continued

Adding additional forms to your Visual Studio project First, right click on presentation. Then, go down to add and click on new item. Select windows form and click add.

The code that makes clicking on the login button take you to the second form. Go back to the code for the login button in slide 17. Inside of if (count == 1) add the 3 lines of code that are in purple: if (count == 1) { MessageBox.Show("Firstname and password is correct"); this.Hide(); Form2 f2 = new Form2(); f2.ShowDialog(); }

Querying and inserting values into a database First, add 4 labels, 4 textboxes, and a button to your second form. Then double click on enter data and enter the following code to find the largest id that any customer has: private void button1_Click(object sender, EventArgs e) { string connectionInfo = "datasource = localhost; port = 3306;" + "username = root; password = pawsox11"; MySqlConnection connect = new MySqlConnection(connectionInfo); MySqlCommand SQLCommand1 = new MySqlCommand(" select max(id) as 'id‘” + ” from videostore.customer;", connect); MySqlDataReader reader; connect.Open(); reader = SQLCommand1.ExecuteReader();

int id = 1; while (reader.Read()) { id = reader.GetInt32("id") + 1; MessageBox.Show("This is" + id); } connect.Close(); string k = "stop"; MySqlCommand SQLCommand2 = new MySqlCommand("insert into” + ”videostore.customer(id,lastname,firstname, ,pass)" + "values('" + id + "','" + k + "','" + k + "', '" + k + "', '" + k + "');", connect); connect.Open(); reader = SQLCommand2.ExecuteReader(); connect.Close(); } // close the enter data button code Querying and inserting values into a database continued In the code below, you are adding one to the max(id) – the largest customer id – and you are inserting that value, along with four copies of string k, into the customer table of the videostore database:

Adding an exit button Add a new button to the second form. Double click on the button. Add the following code to the button: Application.Exit();

Adding things to a list box from a database Add a listbox and a new button to your second form. Double click on the new button (I named the text of the button “put into list box” but the button’s name is button2 – the exit button is button 3 in my code).

private void button2_Click(object sender, EventArgs e) { string connectionInfo = "datasource = localhost; port = 3306;" + "username = root; password = pawsox11"; MySqlConnection connect = new MySqlConnection(connectionInfo); MySqlCommand SQLCommand1 = new MySqlCommand("select * from ” + “videostore.customer as c," + "videostore.moviesrented as mr," + "videostore.movies as m where c.id =“ + “mr.personid and m.id" + "= mr.movieid and c.firstname = " + "'" + textBox1.Text + "';", connect); Because the scope of this button doesn’t include objects created in the other button in this form you will have to create a new object of class MySqlConnection. In the code below I gave the new object of class MySqlConnection the same name as it had in the other button: connect. The SQL query in this code is the same SQL query that I introduced in slide 5. Adding things to a list box from a database continued

MySqlDataReader reader; try { connect.Open(); reader = SQLCommand1.ExecuteReader(); while (reader.Read()) { string movieName, firstNameinC; int id = 1; movieName = reader.GetString("moviename"); firstNameinC = reader.GetString("firstname"); id = reader.GetInt32("id"); listBox1.Items.Add(firstNameinC + " " + movieName + " " + id); } catch (Exception ex) { MessageBox.Show(ex.Message); } } // end of button2’s code Previously, I have shown you how an object of the class MySqlDataReader (I named the object reader) reads one thing from the database. But, as you can see in this code, objects of the MySqlDataReader class could get many things from a database. Here, I got a moviename from the movie table, the firstname from the customer table, and the id from the customer table.

//include the using MySql line at the top using MySql.Data.MySqlClient; //create an object of MySqlConnection with the string containing datasource, etc as a parameter string connectionInfo = "datasource = localhost; port = 3306; username = root; password = pawsox11"; MySqlConnection connect = new MySqlConnection(connectionInfo); //create an object of MySqlCommand with the SQL Query as the first parameter and the object of MySqlConnection //as the second parameter MySqlCommand SQLCommand1 = new MySqlCommand("select * from videostore.customer;”,connect); try { //open the connection using the MySqlConnection object connect.Open(); //use a DataReader to process each record MySqlDataReader reader; reader = SQLCommand1.ExecuteReader(); while (reader.Read()) { //do something with each record } } catch (Exception er) {//do something with the exception} //always close the connection connect.Close(); Summary

References From the Stanford Online Database Course: Introduction to SQL: basic-select-statement: table-variables-set-operators: subqueries-in-where: subqueries-in-from-select: Professor Chi's database class website: Professor Chi's SQL part 1 tutorial: C# Visual Studio with MySql Tutorials: Professor Gueorguieva's Database with C# lecture: