当前位置: 移动技术网 > 网络运营>网络>协议 > EF批量添加txt文件进入数据库某表的一列(敏感词)

EF批量添加txt文件进入数据库某表的一列(敏感词)

2020年07月29日  | 移动技术网网络运营  | 我要评论
做一个敏感词过滤系统txt文件 里面有 大量string敏感词写入SenseWord表里唔其实就是 EF的批量添加using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;namespace PiLiangJiaRu2{class Program{public static void

做一个敏感词过滤系统
txt文件 里面有 大量string敏感词
写入SenseWord表里

其实就是 EF的批量添加

数据库表如下

1是 “报恐”
2是 “涩晴”
之类的

方便快速查询

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace PiLiangJiaRu2
{
    class Program
    {
        public static  void Read(string path)
        {
            StreamReader sr = new StreamReader(path, Encoding.Default);
            String line;


            CbjGameProjectXBDB1_5Entities myContext = new CbjGameProjectXBDB1_5Entities();

            var SenWords = myContext.SensitiveWord;

            while ((line = sr.ReadLine()) != null)
            {

                SenWords.Add(new SensitiveWord() {

                    ClassTypeId = 1,

                    KeyWord = line.ToString(),
                    
                           
            }
            
            
            );
                var ss = myContext.SaveChanges();
                if (ss > 0)
                {

                    Console.WriteLine("非法词汇:{0}", line.ToString());
                }
            }
        }

   
        static void Main(string[] args)
        {

            
         
           Read("F://bkck.txt");
            Console.ReadLine();
        }
    }
}

在这里插入图片描述
Id设置为自增列 就不用管了

记得SaveChange

本文地址:https://blog.csdn.net/qq_38992403/article/details/107638134

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网