编程知识 cdmana.com

How to connect to MySQL database in. Net

dbForge Studio for MySQL It's a Windows The platform is widely used MySQL client , It enables MySQL Developers and administrators work with others to create and execute queries in a convenient environment , Development and debugging MySQL Program , Automated management MySQL Database objects, etc .

Click to download dbForge Studio for MySQL The latest trial version

stay .NET Connect to MySQL database

.NET It's great , It provides a lot of tools for working with databases and data sources . But sometimes , Data sources may not be inherently supported . stay MySQL In this case , When you create a binding source or data source for a project , You may not see it MySQL Connection options .

So what do you do ?

ok , It's not over yet ! It's just a little extra work .

First , Download some tools , These include MySQL The connector :

  • https://dev.mysql.com/downloads/file/?id=492453
  • https://dev.mysql.com/downloads/file/?id=491638
  • https://dev.mysql.com/downloads/connector/net/#downloads

After downloading the installation file , Please install them one by one . If you are using Visual Studio, Please make sure to turn it off before installing .

The installation is suitable for MySQL After , open Visual Studio And create Windows Forms project .

Choose from the toolbox BindingSource Control , Then double click it . stay “ Binding source ” In the properties window of , choice “DataSource” attribute , And then choose “Add Project Data Source”. This will open “ Data source configuration wizard ”.

choice Database, Then choose the next step .

choice Dataset, Then choose the next step .

choice New Connection Button .

choice Change Button .

Be careful ,MySQL The database now appears in the list , Pictured 1 Shown .

.NET Medium MySQL

chart 1  Change data source

Choose... From the list MySQL Database, And then click OK,Add Connection The dialog box will be as shown in the figure 2 Shown .

.NET Medium MySQL

chart 2  Add connection

Input diagram 2 Required server name , User name and password , And then click “OK”.

Select the desired database object , Pictured 3 Shown .

.NET Medium MySQL

chart 3  Database objects

Click finish .

Now? , You can connect to MySQL Database and use it .

If I don't want to use Bindingsource Even what about design views ? What if I just want to use code ?

Let's take a look .

To import data functions , You need to import MySQL The name space and System.Data The name space , The method is as follows :

using MySql.Data.MySqlClient;  using System.Data;

All you need to do is C# Use in using Sentence can be used . Now? , You can visit MySQLClient All the functions of a namespace .

Use the following code to create the connection object :

MySqlConnectionParameters connectionParameters = 
   new MySqlConnectionParameters("Server", "Database", "User", "Password");  string conStr = 
 "server=ServerName;port=PortNumber;database=DatabaseName;uid=Username;password=Password;TreatTinyAsBoolean=false";

From you to MySQL Stored procedures get data , Please use the following code :

MySqlConnection con = new MySqlConnection(conStr);     
MySqlDataAdapter adapter = new MySqlDataAdapter();    
MySqlCommand cmd;        
cmd = new MySqlCommand("CALL StoredProcedureName(@Parameter)", con);        
cmd.Parameters.AddWithValue("@Parameter", Parameter);     
cmd.Parameters.AddWithValue("@ToDate", ToDate);        
adapter.SelectCommand = cmd;  
DataSet ds = new DataSet();    
adapter.Fill(ds);

Conclusion

As you can see , Use .NET Connect to MySQL The database is very easy . I wish you a happy coding !

dbForge Studio for MySQL It's feature rich IDE, So that you can easily put DevOps Method is extended to MySQL and MariaDB Database development and deployment . download dbForge Studio For MySQL, Let your application development out of the world .

版权声明
本文为[Lever essence]所创,转载请带上原文链接,感谢

Scroll to Top