0

我已经重新设计了我的项目 3 次,最后我想在我的应用程序失控之前问一下。我正在构建一个复杂的系统监控和报告生成应用程序。我一直在尝试找出哪种数据库方法最适合我的情况,我认为它是 SQL Server CE,因为我使用的是一台工作笔记本电脑,安装东西可能需要长达 1 周的时间。如果我应该使用更友好的应用程序,我愿意接受建议。

假设我坚持使用 SQL Server CE,将记录插入到.sdf有很多列的文件中的最佳方法是什么?例如,数据库中的表可以包含 100 列。它现在工作正常,但插入的每一行似乎需要大约 1 秒。代码如下所示(最终会有更多列):

con.Open();

SqlCeTransaction trans = con.BeginTransaction();

String insertRecord = "INSERT INTO AS400_Objects(projectName, obBuildObjectName, " +
                    "obBuildLibrary, obBuildType, obBuildExtendedAttribute, obBuildFormatLevel, " +
                    "obBuildUserAttribute, obBuildChangeDate, obBuildCreationDate, " +
                    "obBuildCreatorSystem, obBuildCreatorUserProfile, obBuildJournal, " +
                    "obBuildJournalStatus, obBuildLastUsedDate, obBuildObjectSize, obBuildOwner, " +
                    "obBuildRestoreDate, obBuildSaveDate, obBuildSourceFile, " +
                    "obBuildSourceFileUpdatedDate, obBuildTextDescription, obBuildSystemVersion)" +
                    "VALUES(@projectName, @obBuildObjectName, @obBuildLibrary, @obBuildType, " +
                    "@obBuildExtendedAttribute, @obBuildFormatLevel, @obBuildUserAttribute, " +
                    "@obBuildChangeDate, @obBuildCreationDate, @obBuildCreatorSystem, " +
                    "@obBuildCreatorUserProfile, @obBuildJournal, @obBuildJournalStatus, " +
                    "@obBuildLastUsedDate, @obBuildObjectSize, @obBuildOwner, @obBuildRestoreDate, " +
                    "@obBuildSaveDate, @obBuildSourceFile, @obBuildSourceFileUpdatedDate, " +
                    "@obBuildTextDescription, @obBuildSystemVersion)";

SqlCeCommand cmd = new SqlCeCommand(insertRecord, con);
cmd.Transaction = trans;

cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@projectName", packageName);
cmd.Parameters.AddWithValue("@obBuildObjectName", od[i].getValue(ObjectDescription.NAME).ToString());
cmd.Parameters.AddWithValue("@obBuildLibrary", od[i].getValue(ObjectDescription.LIBRARY).ToString());
cmd.Parameters.AddWithValue("@obBuildType", od[i].getValue(ObjectDescription.TYPE).ToString());
cmd.Parameters.AddWithValue("@obBuildExtendedAttribute", od[i].getValue(ObjectDescription.EXTENDED_ATTRIBUTE).ToString());
cmd.Parameters.AddWithValue("@obBuildFormatLevel", "");
cmd.Parameters.AddWithValue("@obBuildUserAttribute", od[i].getValue(ObjectDescription.USER_DEFINED_ATTRIBUTE).ToString());
cmd.Parameters.AddWithValue("@obBuildChangeDate", changeDate);
cmd.Parameters.AddWithValue("@obBuildCreationDate", creationDate);
cmd.Parameters.AddWithValue("@obBuildCreatorSystem", od[i].getValue(ObjectDescription.CREATOR_SYSTEM).ToString());
cmd.Parameters.AddWithValue("@obBuildCreatorUserProfile", od[i].getValue(ObjectDescription.CREATOR_USER_PROFILE).ToString());
cmd.Parameters.AddWithValue("@obBuildJournal", od[i].getValue(ObjectDescription.JOURNAL).ToString());
cmd.Parameters.AddWithValue("@obBuildJournalStatus", journalStatus);
cmd.Parameters.AddWithValue("@obBuildLastUsedDate", lastUsedDate);
cmd.Parameters.AddWithValue("@obBuildObjectSize", od[i].getValue(ObjectDescription.OBJECT_SIZE).ToString());
cmd.Parameters.AddWithValue("@obBuildOwner", od[i].getValue(ObjectDescription.OWNER).ToString());
cmd.Parameters.AddWithValue("@obBuildRestoreDate", restoreDate);
cmd.Parameters.AddWithValue("@obBuildSaveDate", saveDate);
cmd.Parameters.AddWithValue("@obBuildSourcefile", od[i].getValue(ObjectDescription.SOURCE_FILE).ToString());
cmd.Parameters.AddWithValue("@obBuildSourceFileUpdatedDate", sourceFileUpdatedDate);
cmd.Parameters.AddWithValue("@obBuildTextDescription", od[i].getValue(ObjectDescription.TEXT_DESCRIPTION).ToString());
cmd.Parameters.AddWithValue("@obBuildSystemVersion", od[i].getValue(ObjectDescription.SYSTEM_LEVEL).ToString());

cmd.ExecuteNonQuery();
trans.Commit(CommitMode.Immediate);

cmd.Dispose();

con.Close();
con.Dispose();

基于这种情况,我可以做些什么来提高查询的性能?我来自 AS400 大型机世界,1 秒将给你数百行,如果不是数千行的话。该应用程序的最终结果是它将创建一个输出文件发送到某个地方,然后双击该文件会将所有这些数据加载到远程机器上。我看过SqlCEAdapter,但无法找到一个很好的解释来说明如何使用它来适应我的情况。

提前致谢!

4

1 回答 1

0

你听说过数据库规范化吗?这避免了数据的重复,并减少了每个 INSERT 所需的列数。如果没有 CommitMode.Immediate,该过程也会运行得更快。最后,您可以使用 SqlCeUpdateableRecord 和 TableDirect 绕过查询进程或进行快速插入。

于 2012-05-28T06:55:46.973 回答