C # access MySQL database (WINFORM+EF)

Source: Internet
Author: User

Source: C # access to MySQL database (WINFORM+EF)

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 techniques are not discussed here, personally feel that this comparison is quick to get started.

Http://jingyan.baidu.com/article/642c9d34aa809a644a46f717.html

1, install the MySQL database, here skip, you can refer to this document for installation.

This document has been more detailed, the installation is not complicated, mainly the next step is to go, 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:

1 CREATE DatabaseMy_oa2 3 CREATE TABLEMy_oa.sys_user4 (5IdINT  not NULLAuto_incrementPRIMARY KEY,6UserNamevarchar( +),7Pwdvarchar( +)8 9);

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

1 INSERT  intoSys_user (USERNAME,PWD)VALUES('51diysoft','123');2 3 INSERT  intoSys_user (USERNAME,PWD)VALUES('XMD','123');4 5 INSERT  intoSys_user (USERNAME,PWD)VALUES('HJK','123');

Of course, we can also use VS2013 to connect to the MySQL database, because behind the EF framework to access, must also be able to connect it,

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 on the line, normal installation of 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 Oracel database, Red Mark Place, the truth should be 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.

The database can be empty, and then select the database after logging on.

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 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 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, not too much to write sqlhelper and the LINQ statement LAMDA expression, and so on together, To improve the efficiency of development, 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)

namespacemysql_windows_forms_project2{usingSystem;usingSystem.Collections.Generic; Public Partial classsys_user{ Public intID {Get;Set; } Public stringUserName {Get;Set; } Public stringPWD {Get;Set; }}}

Then let's call the EF-generated entity and make a simple login page and System homepage.

5, the implementation of the login page

WinForm How to make a login page, the basic process is to drag a few lable and TextBox, and set the basic properties, such as the password box is an asterisk, the starting position of the window is Centerscreen, is minimized and so on

Let's take a look at the code, very simple, using the partial LINQ query, combined with the EF framework to operate, more convenient

(Mainly experience the process, without any landscaping)

1      //get the user name and password entered2             using(Myoamodelentities db =Newmyoamodelentities ())3             {4                 stringstrUserName =TxtUserName.Text.Trim ();5                 stringStrpwd =TxtPwd.Text.Trim ();6                 //querying records in a database7                 varUserInfo = ( fromMinchDb.sys_user8                                 whereM.username = = strUserName && M.pwd = =strpwd9                                 Selectm). FirstOrDefault ();  A                 if(UserInfo! =NULL) -                 { -Currentuserinfo = UserInfo;//tagging global users for easy use elsewhere the                     //MessageBox.Show ("Login Successful! "); -                      This. Hide (); -MainWindow m =NewMainWindow (); - M.showdialog (); +                 } -                 Else +                 { AMessageBox.Show ("user name or password is wrong! "); at}

Code:

Http://pan.baidu.com/s/18n54i

Later will continue to learn MySQL database, another individual in the study C # Mono for Android, interested friends can discuss together, one another!

C # access MySQL database (WINFORM+EF)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.