利用sqlserver 提供的bcp.exe插入数据

class bcpHelper
    {
        public static StreamWriter SW_log { get; set; }
        static string bcpExePath = Environment.GetEnvironmentVariable("programfiles")+
            @"Microsoft SQL Server100ToolsBinncp.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!");
           }
        }
    }
原文地址:https://www.cnblogs.com/nora/p/8064265.html