aguo718
2018-01-24 06:44
采纳率: 61.9%
浏览 1.9k
已采纳

csv导入数据库,0行受影响问题

应用以下sql脚本:

 LOAD DATA LOCAL INFILE 'C:/Program Files/MySQL/MySQL Server 5.7/data/nad_content.csv' 
INTO TABLE nad_content
FIELDS TERMINATED BY ',' #分隔符
OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"'
LINES TERMINATED BY '\n' #换行
IGNORE 1 LINES

向数据库中导入记录,但是总是执行成功,0行受影响,
求大神指导

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • mango_love 2018-01-24 06:46
    已采纳

    拿去玩

     static void Main(string[] args)
            {
                int count = 0;
                string readerPath=@"C:\Users\Administrator\Desktop\readerDemo.csv";
                string writerPath=@"C:\Users\Administrator\Desktop\writeDemo.csv";
                if (File.Exists(writerPath))
                {
                    File.Delete(writerPath);
                }
                using (StreamReader reader = new StreamReader(readerPath,Encoding.UTF8))
                {
                    while (!reader.EndOfStream)
                    {
                        string line = reader.ReadLine();
                        using (StreamWriter writer = new StreamWriter(writerPath, true,Encoding.Default))
                        {
                            writer.WriteLine(line);
                        }
                        count++;
                        Console.WriteLine("正在转码第{0}行,请稍等",count);
                    }
                }
                Console.WriteLine("转码完成,共转码{0}条数据",count);
                Console.WriteLine("开始导入数据,请稍等");
             //开始向数据库导入数据
                string sql = "BULK INSERT Test.dbo.BagDataTable FROM 'C:\\Users\\Administrator\\Desktop\\writeDemo.csv' WITH(FIELDTERMINATOR=',',BATCHSIZE=100000,FIRSTROW=2)"; 
                try
                {
                    DbHelper.ExecuteSql(sql);//使用的是方法ExecuteNonQuery()方法,具体的Helper就不上传了。
                }
                catch(Exception ex)
                {
                    using (StreamWriter writerLog = new StreamWriter(@"C:\Users\Administrator\Desktop\Log.txt"))
                    {
                        writerLog.WriteLine(ex.ToString());
                    }
                }
                Console.WriteLine("数据导入完毕");
                Console.ReadKey();
            }
        }
    
    评论
    解决 无用
    打赏 举报
  • 查看更多回答(2条)

相关推荐 更多相似问题