Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private TimeSpan ImportMembersSQL(IList<DocMember> members)
- {
- using (var db = new DocEntities())
- {
- db.Database.CreateIfNotExists();
- }
- using (var connection = Database.DefaultConnectionFactory.CreateConnection("DocEntities"))
- {
- connection.Open();
- using (var transaction = connection.BeginTransaction())
- using (var insertMemberCommand = connection.CreateCommand())
- using (var insertItemCommand = connection.CreateCommand())
- using (var getIdentityCommand = connection.CreateCommand())
- {
- insertMemberCommand.CommandText =
- "INSERT INTO DocMembers(Name, MemberType, AssemblyName) VALUES(@name, @memberType, @assemblyName)";
- insertMemberCommand.AddParameter("@name", DbType.String);
- insertMemberCommand.AddParameter("@memberType", DbType.String);
- insertMemberCommand.AddParameter("@assemblyName", DbType.String);
- insertMemberCommand.Transaction = transaction;
- insertMemberCommand.Prepare();
- insertItemCommand.CommandText =
- "INSERT INTO DocItems(PartType, PartName, Text, Member_Id) VALUES (@partType, @partName, @text, @memberId)";
- insertItemCommand.AddParameter("@partType", DbType.String);
- insertItemCommand.AddParameter("@partName", DbType.String);
- insertItemCommand.AddParameter("@text", DbType.String);
- insertItemCommand.AddParameter("@memberId", DbType.Int32);
- insertItemCommand.Transaction = transaction;
- insertItemCommand.Prepare();
- getIdentityCommand.CommandText = "SELECT @@IDENTITY AS Id";
- getIdentityCommand.Transaction = transaction;
- getIdentityCommand.Prepare();
- var sw = Stopwatch.StartNew();
- foreach (var m in members)
- {
- insertMemberCommand.Parameters["@name"].Value = m.Name;
- insertMemberCommand.Parameters["@memberType"].Value = m.MemberType;
- insertMemberCommand.Parameters["@assemblyName"].Value = m.AssemblyName;
- insertMemberCommand.ExecuteNonQuery();
- m.Id = Convert.ToInt32(getIdentityCommand.ExecuteScalar());
- foreach (var item in m.Items)
- {
- insertItemCommand.Parameters["@partType"].Value = item.PartType;
- insertItemCommand.Parameters["@partName"].Value = item.PartName;
- insertItemCommand.Parameters["@text"].Value = item.Text;
- insertItemCommand.Parameters["@memberId"].Value = m.Id;
- insertItemCommand.ExecuteNonQuery();
- item.Id = Convert.ToInt32(getIdentityCommand.ExecuteScalar());
- }
- Progress++;
- }
- transaction.Commit();
- sw.Stop();
- return sw.Elapsed;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement