C#中的NVarChar for MySQL

时间:2015-02-20 05:41:56

标签: c# mysql

我使用此INSERT将新记录添加到数据库。但它不能以正确的形式显示越南人。 (例如:Kh?ch s?n而不是Kháchsạn)。

在这种情况下,如何在查询中添加“N”。

if (general.TestEmail.IsEmail(txt_Email.Text) == true)
            {
                //Insert dữ liệu vào database
                email = txt_Email.Text;
                MySqlCommand them_hotel = new MySqlCommand("INSERT INTO quan_li.hotel (name, star, address, province, phone, fax, email) VALUES (@name, @star, @address, @province, @phone, @fax, @email)");
                them_hotel.CommandType = CommandType.Text;
                them_hotel.Connection = conn_class.connection;
             //   them_hotel.Parameters.AddWithValue("@hid", txt_Hid.Text);
                them_hotel.Parameters.AddWithValue("@name", txt_Ten.Text);
                them_hotel.Parameters.AddWithValue("@star", txt_Sao.Text);
                them_hotel.Parameters.AddWithValue("@address", txt_DiaChi.Text);
                them_hotel.Parameters.AddWithValue("@province", txt_Tinh.Text);
                them_hotel.Parameters.AddWithValue("@phone", txt_DienThoai.Text);
                them_hotel.Parameters.AddWithValue("@fax", txt_Fax.Text);
                them_hotel.Parameters.AddWithValue("@email", txt_Email.Text);

                conn_class.OpenConnection();
                them_hotel.ExecuteNonQuery();
                conn_class.CloseConnection();
                thanh_cong = "Đã thêm khách sạn thành công";
            }
            else
            {
                email_err = "Email không đúng định dạng \n";
            }

非常感谢你的帮助!

解决:

我将Charset = utf8添加到我的连接字符串中。 这是我的

public void Initialize()
    {
        server = "localhost";
        database = "quan_li";
        uid = "root";
        password = "";
        string connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";" + "Charset=utf8";

        connection = new MySqlConnection(connectionString);
    }

0 个答案:

没有答案