在数据库中保存阿拉伯语字符串列表
Save Arabic list of string in database
我有文本文件。我正在阅读文本文档并将其拆分为一个数组。我尝试删除白色 space。所以我将数组的元素移动到字符串列表中。
这里是部分代码
List<string> words =new List<string>();
string allcomments = File.ReadAllText("D:\all comment.txt");//read text flile
string[] tc = allcomments.Split(' '); //split contains of text flie into tokens
foreach (string t in tc)
{
string token = t;
token = token.Trim();//to remove white spaces
words.Add(token);
}
//save list of words in the database
SqlConnection connection = new SqlConnection("Server=DESKTOP-JRS3DQ4; DataBase=My_Project; Integrated Security=true");
connection.Open();
SqlCommand command = new SqlCommand("INSERT INTO tokens_all_comments (tokens) VALUES (@tokens)", connection);
command.Parameters.Add("@tokens", SqlDbType.NVarChar, 250); //size and type must match your DB
foreach(var w in words)
{
command.Parameters["@tokens"].Value = _words[w];
command.ExecuteNonQuery();
}
connection.Close();
当我尝试 运行 代码时,出现以下错误
Error3 The name '_words' does not exist in the current context
如何修复代码?
在这里:
foreach(var w in words)
{
command.Parameters["@tokens"].Value = _words[w];
command.ExecuteNonQuery();
}
您正在尝试使用未在任何地方声明的 _words
。这是一个错字,我相信你想使用 words
(没有下划线)。
你已经在循环 foreach
中的单词,所以你不必将 words
作为数组访问,而是直接访问 foreach 中声明的变量 w
。此外,目前您只会将最后一个词保留为 sql 参数,因为您没有连接它们的值,它总是被分配。
您可以使用 String.Join()
:
连接所有没有 foreach
的单词
command.Parameters["@tokens"].Value = String.Join("", words.ToArray());
你也可以省略所有的 split 和 foreach,直接用简单的 replace()
:
删除所有的空格
command.Parameters["@tokens"].Value = allcomments.Replace(" ", "");
我有文本文件。我正在阅读文本文档并将其拆分为一个数组。我尝试删除白色 space。所以我将数组的元素移动到字符串列表中。
这里是部分代码
List<string> words =new List<string>();
string allcomments = File.ReadAllText("D:\all comment.txt");//read text flile
string[] tc = allcomments.Split(' '); //split contains of text flie into tokens
foreach (string t in tc)
{
string token = t;
token = token.Trim();//to remove white spaces
words.Add(token);
}
//save list of words in the database
SqlConnection connection = new SqlConnection("Server=DESKTOP-JRS3DQ4; DataBase=My_Project; Integrated Security=true");
connection.Open();
SqlCommand command = new SqlCommand("INSERT INTO tokens_all_comments (tokens) VALUES (@tokens)", connection);
command.Parameters.Add("@tokens", SqlDbType.NVarChar, 250); //size and type must match your DB
foreach(var w in words)
{
command.Parameters["@tokens"].Value = _words[w];
command.ExecuteNonQuery();
}
connection.Close();
当我尝试 运行 代码时,出现以下错误
Error3 The name '_words' does not exist in the current context
如何修复代码?
在这里:
foreach(var w in words)
{
command.Parameters["@tokens"].Value = _words[w];
command.ExecuteNonQuery();
}
您正在尝试使用未在任何地方声明的 _words
。这是一个错字,我相信你想使用 words
(没有下划线)。
你已经在循环 foreach
中的单词,所以你不必将 words
作为数组访问,而是直接访问 foreach 中声明的变量 w
。此外,目前您只会将最后一个词保留为 sql 参数,因为您没有连接它们的值,它总是被分配。
您可以使用 String.Join()
:
foreach
的单词
command.Parameters["@tokens"].Value = String.Join("", words.ToArray());
你也可以省略所有的 split 和 foreach,直接用简单的 replace()
:
command.Parameters["@tokens"].Value = allcomments.Replace(" ", "");