Как подключить sql к windows forms
Перейти к содержимому

Как подключить sql к windows forms

  • автор:

How can I connect to MySQL from windows forms?

How can I connect to a MySQL database from Windows Forms?

4 Answers 4

Numerous sample of connection strings here : http://www.connectionstrings.com/

Here is a full article on connecting to mysql using Connector/Net 6.0.

Alternatively you can also use OleDb to connect to MySql.

i think this gonna be the simple one right. but thanx all of u guys for responding and providing me the documentation!

    The Overflow Blog
Linked
Related
Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.5.23.43453

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Required Programs

A fter you had install Xampp and Visual Studio, the firste thig you gotta need to install the MySQL extension for Visual Studio, don’t worry, is not that difficult, just follow the next few steps and you will done it:

Installing MySQL extension for Visual Studio

F irst of all you’re going to download the extension, you can find the link by click the “MySQL extension” ubicate it in the Required Programs section.

N ext thing you gonna do is open/create your Windows Forms on Visual Studio, in the Solution explores section you’re gonna right-click in the name of your project, right-click on “reference” and again right-click on “add reference”

T hen just click on “assemblies»extensions” and for last but not least, select “MySQL.Data”, and click “OK”.

Y ou did it, you’ve installed the MySQL extension for Visual Studio, now is time to conncet to Xampp, even if we use terms that sounds so complicated, this step is the esaiest one, or at least one of the most. The only thing you gotta do is open Xampp Control Panel and activate the Apache and MySQL services by click eachone “start”:

Now you’re ready to connect your database with Windows Forms, if you don’t have a database already and you don’t know how to create one, don’t worry, just click here and it’d sent you to a YouTube tutorial to create a database using HeidiSQL, also I leave you the link to download HeidiSQL in the Programs Required section 😉

A bout C# you have to add the library “MySql.Data.MySqlClient” on the top of your program, yes exactly, just place it under the other libraries.

T hen you can base your program in this generic program:

And that’s all, now you had connect to your database using Windows Forms and C#.

B efore you leave check out this basic concepts when talking about create and more specifically modify a database: “INSERT INTO”, “UPDATE”, “DELETE” and “SELECT FROM”.

INSERT TO

With this statment you can add new data in your database, just by following the next syntaxis:

INSERT INTO table_name (column1, column2, column3, …) VALUES (value1, value2, value3, …);

UPDATE

With this statment you can modify the already-exist-data in your database, just by following the next syntaxis:

UPDATE table_name SET column1 = value1, column2 = value2, … WHERE condition;

DELETE

With this statment you can delete data of your database, just by following the next syntaxis:

DELETE FROM table_name WHERE condition;

SELECT FROM

With this statment you can consult data of your database, with only intriducing the primary key of the data you wanna consult, just by following the next syntaxis:

SELECT column1, column2, … FROM table_name;

You made it, you’ve reached the end of the tutorial, hope you like it and found it helpfull. If you do please comment and share this quick tutorial, so other people can know of it existence

How to connect to MySQL with C# Winforms and XAMPP

Carlos Delgado

Learn how to set up a local connection between winforms and mySQL.

XAMPP is a free and open source cross-platform web server solution stack package developed by Apache Friends, consisting mainly of the Apache HTTP Server, MariaDB database, and interpreters for scripts written in the PHP and Perl programming languages.

If you use xampp, you probably know how easy is to create and maintain databases with the integrated module of phpmyadmin. You may find easily to work with PHP, but, if you have .NET knowledge you can start working with it too.

MySQL offers a connector a easy connector that will allow you to execute queries to phpmyadmin from your winform.

In this article we’ll learn how to access a database created in phpmyadmin using winforms in C# easily.

Requirements

  • Visual Studio (any version). .
  • XAMPP Distribution (we’ll assume that you know how to use mysql and xampp).

Implementation

This task is more easy than you think, to achieve a succesfully implementation follow these steps :

  • Add the reference to the MySQL connector to your winform project.
  • Create your database (ignore if you already have any) in MySQL with PHPMyAdmin.
  • Start to execute queries.

Add the reference to the MySQL connector to the project

To get started, you need obligatory the .NET MySQL extension installed in your system as we need to add the reference in our project later. You can choose one of the latest version in the official website of MySQL.

Install on your system mysql connector extension

You can choose wether a complete installation or typical.

Installation

After the installation, we are going to proceed to create an empty Winforms project in Visual Studio as you usually do.

Now we need to add the reference to the mysql connector in our project. Locate the solution explorer in the right top corner of Visual Studio when your project is open, use right click on References and then select Add Reference from the context menu.

References .net C#

In the shown menu, navigate to Extensions and select the checkbox from the list the MySql.Data ( MySql.Data.dll ) and then click on OK.

MySQL data .net C# reference

Now we’ll be able to connect to execute queries to MySQL with C#.

Creating a test database in phpmyadmin (localhost)

As mentioned before, we assume that you already have Xampp installed on your system and you know how to use it.

First, do not forget to enable the apache and mysql services in the xampp panel (which is recommended in Administrator mode).

Xampp menu

Now navigate in your browser to http://localhost/phpmyadmin and go to the databases area.

Create a database (in this example our database will be test) and create a table named user .

PHPMyAdmin mySql table

Remember to enable the autoincrementable option to the id field, otherwise you’ll need to add an id everytime you insert a row.

Now that our database «test» contains at least one table «user», we can start executing queries.

Using C# to connect and execute queries

Now comes the fun part ! we’ll write some code to interact with the MySQL database. Primary, don’t forget to add the using statement of the reference in your class :

You can understand how works the connection and how to execute a query with the following snippet :

And that’s it ! Basically, you just need to change the query and start testing. You can read more about the connection string and all the available properties here.

Basic examples of queries

In these examples we are going to execute the most basic tasks to execute (CRUD):

Form insertion

Note that we’ll use a simple listView component (with 4 columns : id,first name, last name and address), 3 textBox and 2 Buttons.

Create

In the following snippet, we’ll create a register in the test database :

In the following snippet we’ll list all the users in the test database in a listview (if available or show in the console) :

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *