简体   繁体   中英

Using SQL Server CE to insert to a database

I've redesigned my project 3 times, and finally figured I'd ask before my application gets too far out of hand. I'm building a complicated system monitoring, and report generating application. I've been trying to figure out which database method is the best for my situation, and I think it is SQL Server CE, because I am on a work laptop, which can take up to 1 week to get stuff installed on. If there is a more friendly application I should be using, I am open to suggestions.

Assuming that I stick with using SQL Server CE, what is the best method for inserting records into the .sdf file that has many columns? For example, the table in the DB can contain 100 columns. It works fine as it is now, but seems to take about 1 second for every row inserted. The code looks like this (eventually there will be more columns):

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();

Based on this situation, is there something I can do to increase the performance of the query? I come from the AS400 mainframe world, where 1 second will give you hundreds if not thousands of rows. The end result of the application is that it will create an output file to be sent somewhere, and then double clicking the file will load all of this data onto the remote machine. I've looked at SqlCEAdapter , but haven't been able to find a good explanation on how to use it to fit my situation.

Thanks in advance!

Have you heard of database normalization? This avoids duplication of data, and reduces the number of columns required by each INSERT. The process will also run much faster without CommitMode.Immediate. Finally, you can use SqlCeUpdateableRecord and TableDirect to bypass the query processsor for fast inserts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM