コード例 #1
0
ファイル: SampleService.cs プロジェクト: 0000duck/ImageHandle
        public int Insert(Sample sample)
        {
            const string sql1 = @"INSERT into sample(ImageType, Name, ImageSizeX, ImageSizeY, Style, ImageURL, BgImage, MainTextNumber, SmallTextNumber, IfHasSmallText, Font)  
                                VALUES(@ImageType, @Name, @ImageSizeX, @ImageSizeY, @Style, @ImageURL, @BgImage, @MainTextNumber, @SmallTextNumber, @IfHasSmallText, @Font); 
                                SELECT LAST_INSERT_ID();";

            const string sql2 = @"INSERT into imagetext(SampleId, Type, Text, Font, PositionX, PositionY, FontSize, FontOrder)  
                                        VALUES(@SampleId, @Type, @Text, @Font, @PositionX, @PositionY, @FontSize, @FontOrder)";

            using (contexto.connection)
            {
                contexto.OpenConnection();
                MySqlTransaction transaction = contexto.connection.BeginTransaction();
                MySqlCommand     cmd         = contexto.connection.CreateCommand();
                cmd.Transaction = transaction;

                try
                {
                    cmd.CommandText = sql1;
                    var parameters = new Dictionary <string, object>
                    {
                        { "ImageType", sample.ImageType },
                        { "Name", sample.Name },
                        { "ImageSizeX", sample.ImageSizeX },
                        { "ImageSizeY", sample.ImageSizeY },
                        { "Style", sample.Style },
                        { "ImageURL", sample.ImageUrl },
                        { "BgImage", sample.BgImage },
                        { "MainTextNumber", sample.MainTextNumber },
                        { "SmallTextNumber", sample.SmallTextNumber },
                        { "IfHasSmallText", sample.IfHasSmallText },
                        { "Font", sample.Font }
                    };
                    contexto.AddParams(cmd, parameters);
                    int sampleId = int.Parse(cmd.ExecuteScalar().ToString());  //insert sample
                    cmd.Parameters.Clear();
                    cmd.CommandText = sql2;
                    foreach (var item in sample.MainText)  //insert maintext
                    {
                        Dictionary <string, object> param = new Dictionary <string, object>
                        {
                            { "SampleId", sampleId },
                            { "Type", item.Type },
                            { "Text", item.Text },
                            { "Font", item.Font },
                            { "PositionX", item.PositionX },
                            { "PositionY", item.PositionY },
                            { "FontSize", item.FontSize },
                            { "FontOrder", item.Order }
                        };
                        contexto.AddParams(cmd, param);
                        cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                    }
                    foreach (var item in sample.SmallText)  //insert small text
                    {
                        var param = new Dictionary <string, object>
                        {
                            { "SampleId", sampleId },
                            { "Type", item.Type },
                            { "Text", item.Text },
                            { "Font", item.Font },
                            { "PositionX", item.PositionX },
                            { "PositionY", item.PositionY },
                            { "FontSize", item.FontSize },
                            { "FontOrder", item.Order }
                        };
                        contexto.AddParams(cmd, param);
                        cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                    }
                    transaction.Commit();
                    return(sampleId);
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw;
                }
                finally
                {
                    contexto.CloseConnection();
                }
            }
        }