简体   繁体   English

将文本从文件导入NVARCHAR(MAX)列; 字符

[英]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.

相关问题 使用C#从NVarchar(Max)列流式传输数据 - Streaming data from a NVarchar(Max) column using c# Linq - 从 SQL 服务器中的 nvarchar 类型列中查找最大值 - Linq - Find max value from a nvarchar type column in SQL Server 从 nvarchar 转换为 varbinary(max) - Conversion from nvarchar to varbinary(max) 覆盖长文本字符串的流利 NHibernate nvarchar(MAX) 而不是 nvarchar(255) - Override for fluent NHibernate for long text strings nvarchar(MAX) not nvarchar(255) 将nvarchar(MAX)列添加到我的审核表中 - Adding a nvarchar(MAX) Column to my audit table 不允许从数据类型 nvarchar 到 varbinary(max) 的隐式转换,上传文件 - Implicit conversion from data type nvarchar to varbinary(max) is not allowed , uploading a file 从大文本文件导入数据库时​​获取SystemOutOfMemoryException的出路 - A way out from getting SystemOutOfMemoryException while importing from large text file into database LINQ使用.Contains()搜索nvarchar(MAX)列非常慢 - LINQ Search nvarchar(MAX) column extremely slow using .Contains() 从两个字符之间的字符串获取文本 - Getting text from string between two characters 导入文件后从字符串中删除多余的字符 - Remove redundant characters from string after importing a file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM