将阿拉伯字符串列表保存在数据库中 [英] Save list of Arabic strings in database
问题描述
我有一个c#程序.我有字符串列表.该列表用阿拉伯语列出的元素.当我尝试将列表的元素保存在数据库中时,我看到符号"??????" 这是我的代码
I have a c# program. I have list of string. The elements of that list in Arabic. When I try to save the elements of list in database I see symbols "??????" Here my code
List<string> _names = new List<string>()
{
"ذهب",
"قال",
"تعال",
"متى",
"البرمجة",
"احمد"
};
SqlConnection connection = new SqlConnection("Server=DESKTOP-JRS3DQ4; DataBase=Library_DB; Integrated Security=true");
connection.Open();
for (int index = 0; index < _names.Count; index++)
{
SqlCommand command = new SqlCommand("INSERT INTO tbl_names (id,name) VALUES ('" + index + "', '" + _names[index] + "')", connection);
command.ExecuteNonQuery();
}
connection.Close();
请问如何解决这个问题?
How I can solve this problem please?
推荐答案
您的问题很可能是由于插入字符串(如varchar)而不是NVarchar而引起的.
Most likely, your problem is coming from inserting strings (as varchar) instead of NVarchar.
您的代码将更可靠,更安全,更安全地工作.如果在运行循环之前定义了参数化查询和参数,则速度更快:
Your code will work more-reliably, safer & faster if you define a parameterized query and parameters before you run your loop:
List<string> _names = new List<string>()
{
"ذهب",
"قال",
"تعال",
"متى",
"البرمجة",
"احمد"
};
SqlConnection connection = new SqlConnection("Server=DESKTOP-JRS3DQ4; DataBase=Library_DB; Integrated Security=true");
connection.Open();
SqlCommand command = new SqlCommand("INSERT INTO tbl_names (id,name) VALUES (@Id, @Name)", connection);
command.Parameters.Add("@Id", SqlDbType.Int);
command.Parameters.Add("@Name", SqlDbType.NVarChar, 20); //size and type must match your DB
for (int index = 0; index < _names.Count; index++)
{
command.Parameters["@Id"].Value = index;
command.Parameters["@Name"].Value = _names[index];
command.ExecuteNonQuery();
}
connection.Close();
最后一点:除非您的数据库将Name
列定义为NVarChar,否则这将无济于事.
One last note: This will not help unless your DB has the Name
column defined as a NVarChar.
这篇关于将阿拉伯字符串列表保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!