我在使用SQL数据库的文本框中显示阿拉伯字符时出现问题。我在SQL中使用nvarchar
类型,当我在项目组合框中选择一个拉丁字符时,它的工作完美(img1),但是当我尝试在组合框中选择一个阿拉伯语项目时,在文本框1和2(img2)中没有任何事情发生。
代码:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
namespace testconnection
{
public partial class Form1 : Form
{
private SqlConnection con;
private SqlCommand cmd;
private SqlDataAdapter da;
private DataTable dt;
private SqlDataReader dr;
public Form1()
{
InitializeComponent();
combo();
}
void combo()
{
con = new SqlConnection(
@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\vemmi\Documents\user.mdf;Integrated Security=True");
con.Open();
cmd = new SqlCommand("SELECT usrs FROM usrtest", con);
try
{
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
comboBox1.Items.Add(dr["usrs"]);
}
dr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button2_Click(object sender, EventArgs e)
{
Form2 c = new Form2();
c.ShowDialog();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
con = new SqlConnection(
@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\vemmi\Documents\user.mdf;Integrated Security=True");
con.Open();
cmd = new SqlCommand("SELECT * FROM usrtest WHERE usrs like '" + comboBox1.Text + "' ");
cmd.Connection = con;
try
{
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string p = dr["pwd"].ToString();
string n = dr["nbr"].ToString();
textBox2.Text = p;
textBox3.Text = n;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
答案 0 :(得分:0)
尝试在查询中的阿拉伯字符串前添加“N”。
cmd = new SqlCommand("SELECT * FROM usrtest WHERE usrs like N'" + comboBox1.Text + "' ");