简体   繁体   中英

How do I access MySql database in C#.NET with WPF?

Please advise me. I am trying to develop Windows Application in WPF with Mysql server database.

Which approach I have to use to create connection establish on the Mysql database?

1) Entity Framework OR 2) ADO.NET OR 3) Other one

The Admin computer need to install mysql database on his computer and then access the mysql database to another (10 or more) client systems which are under.network connected.

Here, just one MySql database inside admin computer only. And all other client machines are accessing MySql database by admin's computer IP address. The IP connection string is like this:-

 @"Data Source=10.0.0.101

One of other matter is, the customer demanding that, when installing.exe file on client machine, then should have to automatically install mysql server database (ie, xampp or other ect..) also.

How can I do this?

I have tried, ADO.NET C# instead EF to connect mysql for wpf windows application. like that:-

public void AddUserList( UserSetUp user)  
    {  

        dbConnection.Open();  
        string query = "INSERT INTO Tble_UserSetUp (FirstName, SurName, Email, PhoneNumber, UserName, Password,Computer_Name,IP_Address,CreatedBy, CreatedDate,IsActive,Function,Department) VALUES (@FirstName, @SurName, @Email, @PhoneNumber, @UserName, @Password,@Computer_Name,@IP_Address,@CreatedBy, @CreatedDate,@IsActive,@Function,@Department)";  
        MySqlCommand insertSQL = new MySqlCommand(query, (MySqlConnection)dbConnection);  
        insertSQL.Parameters.AddWithValue("@FirstName", user.FirstName);  
        insertSQL.Parameters.AddWithValue("@SurName", user.SurName);  
        insertSQL.Parameters.AddWithValue("@Email", user.Email);  
        insertSQL.Parameters.AddWithValue("@PhoneNumber", user.PhoneNumber);  
        insertSQL.Parameters.AddWithValue("@UserName", Encrypt(user.UserName));  
        insertSQL.Parameters.AddWithValue("@Password", Encrypt(user.Password));  
        insertSQL.Parameters.AddWithValue("@Computer_Name", user.Computer_Name);  
        insertSQL.Parameters.AddWithValue("@IP_Address", user.IP_Address);  
        insertSQL.Parameters.AddWithValue("@CreatedBy", user.CreatedBy);  
        insertSQL.Parameters.AddWithValue("@CreatedDate", user.CreatedDate);  
        insertSQL.Parameters.AddWithValue("@IsActive", user.IsActive);  
        insertSQL.Parameters.AddWithValue("@Function", user.Function);  
        insertSQL.Parameters.AddWithValue("@Department", user.Department);  
        try  
        {  
            insertSQL.ExecuteNonQuery();  
            dbConnection.Close();  
        }  
        catch (Exception ex)  
        {  
            throw new Exception(ex.Message);  
        }  
    }  

Is it a bad idea instead of use Entity Framework or another one?

public partial class MainWindow : Window
{
    /*mysql
     * 
     * 1. add nuget mysql.data.dll(desktop application)
     * 2. using MySql.Data.MySqlClient;
     * 3. code like bellow , you can try 
     */
    private StringBuilder sb = new StringBuilder();
    public MainWindow()
    {
        InitializeComponent();
        mysql();
        tbx.Text = sb.ToString();
    }
    private void mysql()
    {
        try
        {
            var connstr = "Server=localhost;Uid=root;Pwd=123456;database=world";
            using (var conn = new MySqlConnection(connstr))
            {
                conn.Open();

                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from city where countryCode= @ID";
                    cmd.Parameters.AddWithValue("@ID", "100");
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var ii = reader.FieldCount;
                            for (int i = 0; i < ii; i++)
                            {
                                if (reader[i] is DBNull)
                                    sb.AppendLine("null");
                                else
                                    sb.AppendLine(reader[i].ToString());
                            }

                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
}

I found these tutorials within official MySQL documentation:)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM