Sign in to follow this  

[.net] Mono.Data.SqliteClient dies in the middle of a bulk insert

This topic is 3592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a function that inserts all of the values from a CSV file into a database. after 8 lines an Exception is thrown saying 'SQL Logic error or missing database' on the first column. It works fine until then????? why would this happen... the SQL generated looks normal. Could it be trying to insert loads of values maybe broke it? I am using Mono.Data.SqliteClient on .NET 2.0. heres the function:
    public bool ReadMeterCSV(string file, int pk)
    {
        IDbCommand ts_cmd;
        IDataReader ts_reader;
        string datestamp = "0.0";
        bool data_present;

        ts_cmd = _conn.CreateCommand();
        ts_cmd.CommandText = "SELECT timestamp FROM Curtail_Readings WHERE meterID = " + Convert.ToString(pk);
        ts_reader = ts_cmd.ExecuteReader();
        if(ts_reader.Read())data_present = true;
        else data_present = false;
        ts_cmd.Dispose();
        ts_cmd = null;

        if(data_present)
        {
            ts_cmd = _conn.CreateCommand();
            ts_cmd.CommandText = "SELECT MAX(timestamp) FROM Curtail_Readings WHERE meterID = " + Convert.ToString(pk);
            ts_reader = ts_cmd.ExecuteReader();
            if(ts_reader.Read())
                datestamp = ts_reader.GetString(0);
            ts_cmd.Dispose();
            ts_cmd = null;
        }
        string crash_str = "";
        int crash_i = 0;
        try
        {
            using(StreamReader sr = new StreamReader(file))
            {
                using(IDbTransaction trans = _conn.BeginTransaction())
                {
                    using(IDbCommand cmd = _conn.CreateCommand())
                    {
                        string line;
                        int deltaSec;
                        cmd.CommandText = "INSERT INTO Curtail_Readings(meterID,timestamp,value) VALUES(" + Convert.ToString(pk) + ",?,?)";

                        // first line is format data
                        line = sr.ReadLine();
                        deltaSec = Convert.ToInt32(line.Split(',')[3]);

                        IDbDataParameter param1 = cmd.CreateParameter();
                        IDbDataParameter param2 = cmd.CreateParameter();  

                        cmd.Parameters.Add(param1);
                        cmd.Parameters.Add(param2);

                        while((line = sr.ReadLine()) != null) 
                        {
                            string[] cols = line.Split(',');
                            DateTime timestamp = DecodeDate(cols[0]);

                            if((data_present && (timestamp > DecodeDate(datestamp))) || !data_present)
                            {
                                for(int i = 1;i < cols.Length;i++)
                                {
                                    param1.Value = Convert.ToString(timestamp);
                                    param2.Value = cols[i];
                                    SqliteCommand sqlite_cmd = (SqliteCommand)cmd;
                                    crash_str = sqlite_cmd.BindParameters2();
                                    crash_i = i;
                                    cmd.ExecuteNonQuery();
                                    timestamp = timestamp.AddSeconds(deltaSec);
                                }
                                MessageBox.Show("Loop");
                            }
                        }

                    }

                    MessageBox.Show("Now Finishing");

                    trans.Commit();
                }
            }
        }
        catch(Exception e)
        {
            MessageBox.Show("Unable to read file in loop " + Convert.ToString(crash_i) + ":\n" + file + "\n Error: '" + e.ToString() + "'\n" + crash_str + ".","Error!");
        }

        return true;
    }

Share this post


Link to post
Share on other sites

This topic is 3592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this