如何在Wpf中连接到Mysql数据库 [英] How Do I Connect To Mysql Database In Wpf

查看:961
本文介绍了如何在Wpf中连接到Mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在app.config文件中使用了以下连接字符串



i used the following connection string in the app.config file

<configuration>
  <connectionStrings>
    <add name="winningways" connectionString="datasource=localhost; port=3306; username=root; password=chilochp;" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
</configuration>





以及我的代码中的以下内容将一些信息插入数据库



and the following in my code to insert some information into the database

public partial class Admin : Window
{
    string connection, username, password;
    public Admin()
    {
        connection = ConfigurationManager.ConnectionStrings["winningways"].ConnectionString;
        InitializeComponent();
    }

    private void addemp_Click(object sender, RoutedEventArgs e)
    {
            try
            {
                MySqlConnection conn = new MySqlConnection(connection);
                MySqlCommand add = new MySqlCommand("insert into winningways.staff (name, address, mobileNumber, position, username, password) values ('" + empname.Text + "', '" + empadd.Text + "', '" + empnum.Text + "', '" + emppos.Text + "', '" + empname.Text + "', '" + password + "')", conn);
                MySqlDataReader read;
                conn.Open();
                read = add.ExecuteReader();
                read.Close();

                MessageBox.Show("Staff added successfully \n Username: '"+empname.Text+"' \n Password: '"+password+"'", "New Staff");
                conn.Close();
            }
            catch (MySqlException myex)
            {
                MessageBox.Show(myex.Message, "Admin Add New Staff Error");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Admin Add New Staff Error");
            }
        }
    }



但它不插入也不显示消息框。请协助


but it doesn't insert nor displays d message box. please assist

推荐答案

最好不要使用连接的SQL查询,因为它会打开以注入恶意命令,尤其是直接使用用户输入时。

您应该使用参数。请参阅下面的代码示例。



使用使用子句也是一种好习惯,因为它会关闭离开范围时自动连接。

使用的工作方式类似于 try-catch-finally ,在finally子句中关闭连接的位置。

(在原始代码中,如果有异常,则永远不会关闭连接)



在这种情况下,您应该在执行INSERT时使用 ExecuteNonQuery ,并且可能无法从数据库获得回复。

It is good practice not to use concatenated SQL queries as it opens up for injection of malicious commands, especially when using user input directly.
You should use parameters instead. See the code example below.

It is also good practice to use the using clause as it will close the connection automatically when leaving the scope.
using works like try-catch-finally, where you close the connection in the finally clause.
(In your original code, you never close the connection if you have an exception)

In this case you should use ExecuteNonQuery as you do an INSERT and might not get a reply from the DB.
string cs = ConfigurationManager.ConnectionStrings["winningways"].ConnectionString;
using (MySqlConnection connection = new MySqlConnection(cs))
{
    connection.Open();

    MySqlCommand command = new MySqlCommand();
    command.Connection = connection;

    command.CommandText = "insert into winningways.staff (name, address, mobileNumber, position, username, password) values (@name, @address, @mobileNumber, @position, @username, @password)";
    command.Parameters.Add("@name", empname.Text);
    command.Parameters.Add("@address", empadd.Text);
    command.Parameters.Add("@mobileNumber", empnum.Text);
    command.Parameters.Add("@position", emppos.Text);
    command.Parameters.Add("@username", empname.Text);
    command.Parameters.Add("@password", password);
    command.ExecuteNonQuery();
}


而不是这个
read = add.ExecuteReader();
                read.Close();





只需使用



just use

add.ExecuteNonQuery();





只是它会帮助你!!



simply it will help you!!


这篇关于如何在Wpf中连接到Mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆