【问题标题】:SqlBulkCopy and File ArchivingSqlBulkCopy 和文件归档
【发布时间】:2013-03-30 08:28:02
【问题描述】:

我有一个进程将数据从平面文件加载到 sql 表中,然后需要立即将文件移动到存档文件夹。 但是,在运行代码时,它会导入数据但会抛出 IOException {“该进程无法访问该文件,因为它正被另一个进程使用。”}

在此过程中似乎存在一些争用。我应该在哪里以及如何避免这种情况?

 internal class Program
{
    private static void Main(string[] args)
    {
        string sourceFolder = @"c:\ImportFiles\";
        string destinationFolder = @"c:\ImportFiles\Archive\";

        foreach (string fileName in Directory.GetFiles(sourceFolder, "*.*"))
        {
            string sourceFileName = Path.GetFileName(fileName);
            string destinationFileName = Path.GetFileName(fileName) + ".arc";

            ProcessFile(fileName);

            string source = String.Concat(sourceFolder,sourceFileName);
            string destination = String.Concat(destinationFolder,destinationFileName);
            File.Move(source, destination);        
        }
    }



    static void ProcessFile(string fileName)
    {
        Encoding enc = new UTF8Encoding(true, true);
        DataTable dt = LoadRecordsFromFile(fileName, enc, ',');

        SqlBulkCopy bulkCopy = new SqlBulkCopy("Server=(local);Database=test;Trusted_Connection=True;",
                                               SqlBulkCopyOptions.TableLock);
        bulkCopy.DestinationTableName = "dbo.tblManualDataLoad";
        bulkCopy.WriteToServer(dt);
        bulkCopy.Close();

    }


    public static DataTable LoadRecordsFromFile(string fileName, Encoding encoding, char delimeter)
    {
        DataTable table = null;

        if (fileName != null &&
            !fileName.Equals(string.Empty) &&
            File.Exists(fileName))
        {
            try
            {
                string tableName = "DataImport";
                FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                List<string> rows = new List<string>();
                StreamReader reader = new StreamReader(fs, encoding);
                string record = reader.ReadLine();

                while (record != null)
                {
                    rows.Add(record);
                    record = reader.ReadLine();
                }

                List<string[]> rowObjects = new List<string[]>();
                int maxColsCount = 0;
                foreach (string s in rows)
                {
                    string[] convertedRow = s.Split(new char[] { delimeter });
                    if (convertedRow.Length > maxColsCount)
                        maxColsCount = convertedRow.Length;
                    rowObjects.Add(convertedRow);
                }

                table = new DataTable(tableName);
                for (int i = 0; i < maxColsCount; i++)
                {
                    table.Columns.Add(new DataColumn());
                }

                foreach (string[] rowArray in rowObjects)
                {
                    table.Rows.Add(rowArray);
                }
                //Remove Header Row From Import file
                DataRow row = table.Rows[0];
                row.Delete();
                table.AcceptChanges();
            }
            catch
            {
                //TODO SEND EMAIL ALERT ON ERROR
                throw new Exception("Error in ReadFromFile: IO error.");
            }
        }
        else
        {
            //TODO SEND EMAIL ALERT ON ERROR
            throw new FileNotFoundException("Error in ReadFromFile: the file path could not be found.");
        }
        return table;
    }
}   

【问题讨论】:

    标签: c# file-io sqlbulkcopy


    【解决方案1】:

    您的程序可能会保持文件处于打开状态。您应该将 FileStreamStreamReader 对象包装在 using 语句中。这会在 using 块完成时关闭这些对象。

    LoadRecordsFromFile 函数中读取文件的部分应类似于:

    ...
    string tableName = "DataImport";
    List<string> rows = new List<string>();
    using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    {
        using (StreamReader reader = new StreamReader(fs, encoding))
        {
            string record = reader.ReadLine();
            while (record != null)
            {
                rows.Add(record);
                record = reader.ReadLine();
            }
        }
    }
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-09
      • 2018-08-09
      • 2012-02-10
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多