class bcpHelper
    {
        public static StreamWriter SW_log { get; set; }
        static string bcpExePath = Environment.GetEnvironmentVariable("programfiles")+
            @"\Microsoft SQL Server\100\Tools\Binn\bcp.exe";

        public static void BatchInsertProcess(string dbName,string svName,string sourceFile,string targetTable,
            string errorFile,string outBcpFile)
        {
           //-c则要求要插入的文件编码为ascii编码,-w则为Unicode
           string args = "["+dbName+"]" + ".." +"["+ targetTable+"]" + " in \"" + sourceFile + "\"" +
                    " -S " + svName + " -T -w -t \",\" -e \""+errorFile+"\" -o \""+outBcpFile+"\"";
           shellHelper.executeShellCmd(bcpExePath, args);
         
            //判断bcp是否成功
           FileInfo fi = new FileInfo(errorFile);
           if (fi.Length == 0)
           {
               Console.WriteLine("Error:Fail to process bcp!");
               txtWriteHelper.AppendLog(SW_log, "Error:Fail to process bcp!");
           }
        }
    }
posted on 2017-12-19 10:32  笨nora  阅读(596)  评论(0编辑  收藏  举报