Used to be C # connection to SQL Server, now MySQL is also relatively fire, and is open source cross-platform, where connected to use, mainly to understand the entire process, here is the use of WinForm access to MySQL, of course, using Winfrom, or WFP, and other technologies are not discussed here.
1, install the MySQL database, here skip, you can refer to this document for installation.
Http://jingyan.baidu.com/article/642c9d34aa809a644a46f717.html
This document has been more detailed, the installation is not complicated, mainly the next step, to return to the basic configuration, where my database user name or SA (recommended to build a user), root username and password We do not use here.
2, connect MySQL database, can use the own MySQL Workbench 6.2 ce tool
After logging in, you can create a table and other operations, here I set up a database named My_oa database, we directly use the SQL statement to execute and build the database and then refresh the list to see the database we have established
(1) (2) (3) (4) The corresponding steps, in addition to establish a database also need to set up a few tables to store data, these are basic and SQL Server is similar.
The script is as follows:
CREATE Database My_oa
CREATE TABLE My_oa.sys_user
(
ID INT not NULL auto_increment PRIMARY KEY,
UserName varchar (32),
PWD varchar (32)
);
Of course, you can use the graphical interface to configure, directly right mouse button to build the table, mainly to see the habits of the individual, familiar with the direct SQL sentence will be more convenient and faster,
Here we quickly insert three data, for later testing use, because the ID is self-increment, so you can not tube, after inserting you will find that the ID has its own, only MySQL auto_increment and sqlseve identity difference
INSERT into Sys_user (username,pwd) VALUES (' 51diysoft ', ' 123 ');
INSERT into Sys_user (username,pwd) VALUES (' xmd ', ' 123 ');
INSERT into Sys_user (username,pwd) VALUES (' HJK ', ' 123 ');
Of course, we can also use VS2013 to connect to MySQL database, because the back of the EF framework to access, you can definitely buy a connection,
We open the VS "view" Server resource, point to "database connection" right-click to add "Database", and then the same steps as normal connection to SQL Server
Choose MySQL here, the normal situation when installing MySQL will be connected to the connection driver and other related content directly to join, do not have to manually add, of course, see here is also can connect to the Oracel database, the Red line mark Place, the reason is similar.
Below needs the server's address, user name and password, the best test can connect through, I am here is sa user (note is not the default), is a user set up, the default is root, if you need root to log on to the server, you must check the installation of MySQL, Otherwise you will create a new user to operate.
The data here already, in addition we can also use sqldbx, navicat_premium_11.0.10 and other database client tools to connect, this tool smart hints are strong, and small size for portability and maintenance
Below we take sqldbx as an example, explain how to connect the database, in fact, are the same connection, as follows:
Sqldbx and this SQL script example can be downloaded to the Baidu Cloud:
The first step:
Need to choose the database type of connection (MYSQL), here can also connect other types of database, see for yourself, you can try
Step Two:
You can also enter a username and password to connect.
Database can be empty, and so on after logging in select Database can also.
In addition, we compare the database table replication, SQL Server we can directly copy (in the select * from table) in the middle of adding an into the new table:
SELECT * into Sys_user3 from Sys_user
And in MySQL we write this directly is an error: only the traditional table to re-guide the data, such as
CREATE TABLE Test
(
SELECT * from Sys_user
), this ID that is generated by default in phase two does not automatically grow, and it is important to note that we primarily want to back up the table's data.
There must be some small differences between the different databases, let's study slowly.
3, how to use VS2013 to build MySQL project (here WinForm Bar)
There's a lot of talk on the top, hurry up.
(1), VS "File" = "new" = "Project" Skip, note that the corresponding project type is default to WinForm and ASP. NET's own choice, in addition we need to choose the version of EF,
How to do not feel about the Entity Framework it doesn't matter, simply means that Microsoft helped you connect the database is an Entity Framework, so that you can operate more convenient, do not have to write sqlhelper alone and with the LINQ statement LAMDA expression, etc., to improve the development efficiency , this is more practical.
Reference Description:
Http://baike.baidu.com/link?url=jDHhtM4QGOpQJmgY9bCe24VJy4kgKAvsuRHJgGmuXlGilJ7lmVVzQkoVMX9KlYCmdagGACDm8042dQnR-E6_nq
Also note is the version of EF, I use EF6, can be managed by NuGet Toolkit to get the latest version
Here are the steps to build a VS project:
The first step is to create a new MySQL connection, and then select the corresponding table to generate the corresponding EF Entity Framework, so that an EF WinForm project is basically built.
4. View WINFROM Project Structure
Open the corresponding *.tt file, these are the EF automatically generated connected database after the related entity class, you do not have to set up the class (need to build their own vew_sys_user)
Namespace Mysql_windows_forms_project2
{
Using System;
Using System.Collections.Generic;
public partial class Sys_user
{
public int ID {get; set;}
public string UserName {get; set;}
public string pwd {get; set;}
}
}
C # access MySQL database (WINFORM+EF)