SQL事务不提交到数据库

时间:2015-01-16 18:25:54

标签: c# sql sql-server

我正在编写我的第一个SQL事务代码并且已经扫描了stackoverflow以确保我已经完成了它。该项目的基本思想是获取一封电子邮件,将其解析为RawDatum对象,然后将该对象的内容写入SQL数据库。我希望能够以100个批次提交新行。

我运行的代码没有错误,但实际上没有任何内容写入数据库。

我有一个本地数据库(MS SQL Server Express)。我有一些注释用于测试目的。我做错了什么?

        using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.MissionMetricsConnectionString))
        {
            connection.Open();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.Text;
            command.CommandText = @"INSERT INTO RawData (MessageUID, DateTime, Attribute, DigitalValue, AnalogValue) VALUES (@MessageUID, @DateTime, @Attribute, @DigitalValue, @AnalogValue)";
            command.Parameters.Add("@MessageUID", SqlDbType.Int);
            command.Parameters.Add("@DateTime", SqlDbType.DateTime);
            command.Parameters.Add("@Attribute", SqlDbType.Text);
            command.Parameters.Add("@DigitalValue", SqlDbType.Bit);
            command.Parameters.Add("@AnalogValue", SqlDbType.Float);
            command.Connection = connection;

            RawDatum currentDatum = null;
            int uid;
            List<int> uidsToDelete = new List<int>();
            int numLoops = 1;//(int)(ids.Length / loopSize) + 1;

            for (var loopIndex = 0; loopIndex < numLoops; loopIndex++)
            {
                int startIndex = loopIndex * loopSize;
                int endIndex = 10;// Math.Min((loopIndex + 1) * loopSize, ids.Length);
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        command.Transaction = transaction;
                        for (int i = startIndex; i < endIndex; i++)
                        {
                            msg = inbox.Fetch.MessageObject(ids[i]);
                            uid = inbox.Fetch.Uid(ids[i]);
                            if (msg.Subject == "[METRICS]")
                            {
                                currentDatum = new RawDatum(uid, msg.Date, msg.BodyText.TextStripped);
                                command.Parameters["@MessageUID"].Value = currentDatum.MessageUid;
                                command.Parameters["@DateTime"].Value = currentDatum.DateTime;
                                command.Parameters["@Attribute"].Value = currentDatum.Attribute;
                                command.Parameters["@DigitalValue"].Value = currentDatum.DigitalValue;
                                command.Parameters["@AnalogValue"].Value = currentDatum.AnalogValue;
                                int queryResult = command.ExecuteNonQuery();
                                if (queryResult != 1)
                                {
                                    throw new InvalidProgramException();
                                }
                            }
                            uidsToDelete.Add(uid);
                        }
                        transaction.Commit();
                        //Delete(uidsToDelete);
                        output[1] += uidsToDelete.Count;
                        uidsToDelete.Clear();
                    }
                    catch
                    {
                        transaction.Rollback();
                        output[0] += 1;
                        output[2] += endIndex - startIndex;
                    }
                }
            }
            connection.Close();
        }

0 个答案:

没有答案