c#/ sqlite /内存不足/事务/ csv

时间:2018-02-27 09:05:00

标签: c# sqlite csv transactions out-of-memory

我该如何解决这个问题? 程序如下。

  1. 导入csv文件
  2. 将数据插入sqlite(100万行)
  3. 异常是“System.Data.SQLite.OutOfMemoryException” 我使用“beginTransaction”和“Commit” 而CSV文件内存为118MB 我尝试了一切。但我不能这个问题

    这是我的源代码。 请帮忙。

    OpenFileDialog file = new OpenFileDialog();
    file.Filter = "(*.csv)|*.csv";
    file.ShowDialog();
    
    string filename = file.FileName;
    string fileNameNoEx = Path.GetFileNameWithoutExtension(filename);
    
    for (int j = 0; j < ListBox.Items.Count; j++)
    {
        if (fileNameNoEx.Equals(ListBox.Items[j]))
        {
            return;
        }
    }
    
    DirectoryInfo di = new DirectoryInfo("C:/sqlite");
    FileInfo fi = new FileInfo("C:/sqlite/SQLiteEx.db");
    if (!di.Exists)
    {
        di.Create();
    }
    if (!fi.Exists)
    {
        SQLiteConnection.CreateFile("C:/sqlite/SQLiteEx.db");
    }
    
    SQLiteConnection conn = new SQLiteConnection("data source = C:/sqlite/SQLiteEx.db;PRAGMA synchronous = OFF; PRAGMA journal_mode = WAL;");
    conn.Open();
    string sql = "";
    
    StreamReader sr = new StreamReader(filename);
    int i = 0;
    
    using (SQLiteTransaction tr = conn.BeginTransaction())
    {
        while (!sr.EndOfStream)
        {
            string line = sr.ReadLine();
            string[] cols = line.Split(',');
            string fieldName = "";
            string colData = "";
    
            if (i == 0)
            {
                for (int j = 0; j < cols.Length; j++)
                {
                    if (j == 0)
                    {
                        fieldName = "'" + cols[0] + "' TEXT";
                    }
                    else
                    {
                        fieldName = fieldName + ",'" + cols[j] + "' TEXT";
                    }
                }
                sql = "CREATE TABLE IF NOT EXISTS " + fileNameNoEx + "(" + fieldName + ");";
                SQLiteCommand command = new SQLiteCommand(sql, conn);
                int result = command.ExecuteNonQuery();
            }
            else
            {
                for (int j = 0; j < cols.Length; j++)
                {
                    if (j == 0)
                    {
                        colData = cols[0];
                    }
                    else
                    {
                        colData = colData + "," + cols[j];
                    }
                }
    
                sql = "INSERT INTO " + fileNameNoEx + " VALUES (" + colData + ");";
                SQLiteCommand command = new SQLiteCommand(sql, conn);
                int result = command.ExecuteNonQuery();
            }
            i++;
        }
        tr.Commit();
    }
    sr.Close();
    conn.Close();
    

0 个答案:

没有答案