[英]Importing text from file into a NVARCHAR(MAX) column; getting � for characters
We're moving are article content from files into a NVARCHAR(MAX) column on our content_articles table. 我们正在将文章内容从文件移到content_articles表的NVARCHAR(MAX)列中。 The content is inserted via a simple stored procedure: 通过简单的存储过程插入内容:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[UpdateArticleContent]
@ArticleID VARCHAR(50),
@ArticleText NVARCHAR(MAX)
AS
BEGIN
SET NOCOUNT ON;
UPDATE content_article
SET article_content = @ArticleText
WHERE article_id = @ArticleID
END
GO
The function that grabs the article file, text, and then calls the stored procedure can be found here: 可以在此处找到获取文章文件,文本然后调用存储过程的函数:
private void LoadButton_Click(object sender, EventArgs e)
{
var dbSelected = ServerDropDown.SelectedItem.ToString();
if (String.IsNullOrWhiteSpace(dbSelected))
{
OutputTextBox.Text = "Cannnot reach db.";
}
else
{
var sb = new StringBuilder();
var articlesConvertedPath = string.Format(@"{0}\ArticlesConverted", FolderPathTextBox.Text);
var articlesConvertedDir = new DirectoryInfo(articlesConvertedPath);
var articlesConvertedFiles = articlesConvertedDir.GetFiles();
var articlesConvertedNames = articlesConvertedFiles.Select(x => x.Name).ToList();
var dirInfo = new DirectoryInfo(FolderPathTextBox.Text);
var sqlConnectionString = ConfigurationManager.ConnectionStrings[dbSelected].ConnectionString;
var con = new SqlConnection(sqlConnectionString);
con.Open();
foreach (var currFile in dirInfo.GetFiles().Where(x => !articlesConvertedNames.Contains(x.Name)))
{
sb.AppendLine(currFile.Name.Replace(".asp", string.Empty));
using (StreamReader sr = new StreamReader(currFile.FullName))
{
var articleText = sr.ReadToEnd()
.Replace("<!--#include file=\"../include/engine_article_header.asp\"-->", string.Empty)
.Replace("<!--#include file=\"../include/engine_article_footer.asp\"-->", string.Empty)
.Replace("<img src=\"", "<img src=\"http://www.oandp.com/");
using (SqlCommand command = new SqlCommand("UpdateArticleContent", con))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@ArticleID", SqlDbType.VarChar).Value = currFile.Name.Replace(".asp", string.Empty);
command.Parameters.Add("@ArticleText", SqlDbType.NVarChar, -1).Value = articleText;
command.ExecuteNonQuery();
}
}
var copyFilePath = string.Format(@"{0}\{1}", articlesConvertedPath, currFile.Name);
if (File.Exists(copyFilePath))
{
File.Delete(copyFilePath);
File.Copy(currFile.FullName, copyFilePath);
}
else
{
File.Copy(currFile.FullName, copyFilePath);
}
}
con.Close();
sb.AppendLine("Finished!");
OutputTextBox.Text = sb.ToString();
}
}
It's taking the simplest of characters, like a dash or quote, and making them . 它采用最简单的字符(例如破折号或引号)制成。
I have no idea what I'm doing wrong, it seemed like it would be pretty straight forward. 我不知道我在做什么错,这似乎很简单。
Little tidbits: Running SQL Server 2008 R2 小花絮:运行SQL Server 2008 R2
Does anyone know why, with the information above, the is replacing non-alphanumeric characters? 有谁知道为什么使用以上信息替换非字母数字字符?
You guys pointed me in the right direction. 你们为我指明了正确的方向。 All I had to do in the StreamReader was Encoding.Default. 我在StreamReader中要做的只是Encoding.Default。 THANK YOU! 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.