将文本从文件导入NVARCHAR(MAX)列;获得角色的

时间:2017-01-17 23:51:58

标签: c# asp.net sql-server sql-server-2008-r2

我们正在将文章中的文章内容移动到我们的content_articles表上的NVARCHAR(MAX)列中。内容通过一个简单的存储过程插入:

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

可以在此处找到抓取文章文件,文本,然后调用存储过程的函数:

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();
}
}

它采用最简单的字符,如短划线或引号,并使它们成为 。

我不知道我做错了什么,好像很直接。

小花絮:运行SQL Server 2008 R2

有没有人知道为什么,根据上述信息, 正在替换非字母数字字符?

1 个答案:

答案 0 :(得分:0)

你们指出了我正确的方向。我在StreamReader中所做的就是Encoding.Default。谢谢!