当前位置: 首页>>代码示例>>C#>>正文


C# DataGrid.Read方法代码示例

本文整理汇总了C#中DataGrid.Read方法的典型用法代码示例。如果您正苦于以下问题:C# DataGrid.Read方法的具体用法?C# DataGrid.Read怎么用?C# DataGrid.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DataGrid的用法示例。


在下文中一共展示了DataGrid.Read方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Import

        public override DataGrid Import()
        {
            if (!Settings.FormatterType.IsDefined()) throw new InvalidOperationException("A Table Name or Sql Query was not specified.");

            DataGrid dataGrid = new DataGrid();
            Columns.Where(match => !Excluded.Contains(match.ColumnName)).ForEach(c => dataGrid.Columns.Add(c));
            dataGrid.Read(MediaConfiguration.Reader);

            Log.Info(String.Format("Importing {0} Rows to Database...", dataGrid.Rows.Count));

            string sqlQuery;
            if (Settings.FormatterType.IsSqlTable())
            {
                sqlQuery = String.Format("INSERT INTO {0} ({1}) VALUES ({2})"
                    , Table()
                    , Columns.Select(c => c.DbColumnName()).JoinStrings(",")
                    , Columns.Select(c => c.DbParamName()).JoinStrings(","));
            }
            else if (Settings.FormatterType.IsSqlQuery())
            {
                sqlQuery = Settings.SqlQuery;
            }
            else
            {
                throw new NotSupportedException(String.Format("Formatter Type '{0}' Is Not Supported.", Settings.FormatterType.DisplayName()));
            }

            using (DbContext context = new DbContext(Settings.ConnectionString))
            {
                context.Database.CommandTimeout = 300;
                using (DbContextTransaction transaction = context.Database.BeginTransaction())
                {
                    if (Settings.FormatterType.IsSqlTable() && Settings.AlwaysTruncate)
                    {
                        Log.Info("Truncating Table...");
                        context.Database.ExecuteSqlCommand(String.Format("TRUNCATE TABLE {0}", Table()));
                        Log.Info("Done Truncating Table.");
                    }

                    if (!Settings.PreEvent.IsNullOrBlank())
                    {
                        Log.Info("Executing Pre-event...", new LogAttachment("Sql", Settings.PreEvent));
                        Log.Trace(Settings.PreEvent);
                        context.Database.ExecuteSqlCommand(Settings.PreEvent);
                        Log.Info("Done Executing Pre-event.");
                    }

                    foreach (DataGridRow row in dataGrid.Rows)
                    {
                        int rowIndex = row.Index() + 1;
                        SqlParameter[] sqlParams = null;
                        try
                        {
                            Log.Trace(String.Format("Importing Row '{0}' to Database.", rowIndex));
                            sqlParams = Columns.Select(c => c.DbParam(row[c.ColumnName])).ToArray();
                            context.Database.ExecuteSqlCommand(sqlQuery, sqlParams);
                        }
                        catch (Exception exception)
                        {
                            string error = String.Format("An error ocurred while importing Row '{0}' to Database.", rowIndex);

                            LogAttachments attachments = new LogAttachments();
                            attachments.Add(new LogAttachment("Sql", sqlQuery));
                            if (sqlParams != null)
                            {
                                attachments.AddRange(sqlParams.Select(p => new LogAttachment(p.ParameterName, p.Value.FormatString())));
                            }

                            Log.Error(error, exception, attachments.ToArray());

                            throw new AbortException(error, exception);
                        }
                    }

                    if (!Settings.PostEvent.IsNullOrBlank())
                    {
                        Log.Info("Executing Post-event...");
                        Log.Trace(Settings.PostEvent);
                        context.Database.ExecuteSqlCommand(Settings.PostEvent);
                        Log.Info("Done Executing Post-event.");
                    }

                    transaction.Commit();
                }
            }

            Log.Info("Done importing to Database.");

            return dataGrid;
        }
开发者ID:onesimoh,项目名称:Andamio,代码行数:90,代码来源:DatabaseFormatter.cs


注:本文中的DataGrid.Read方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。