Guest User

Untitled

a guest
Jul 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. public DbCommand RecycledParameters(string sql, IList<DbParameter> parameters)
  2. {
  3. var result = db.GetSqlStringCommand(sql);
  4. foreach(DbParameter p in parameters)
  5. {
  6. db.AddInParameter(result, p.ParameterName, p.DbType, p.Value);
  7. }
  8. return result;
  9. }
  10.  
  11. System.Data.Common.DbCommand command = new System.Data.SqlClient.SqlCommand();
  12. System.Data.Common.DbCommand command1 = new System.Data.SqlClient.SqlCommand();
  13.  
  14. command1.Parameters.AddRange(command.Parameters.Cast<System.Data.Common.DbParameter>().ToArray());
  15.  
  16. private DbParameterCollection cloneParms(DbCommand commandWithParms)
  17. {
  18. return ObjectCopier.Clone<DbParameterCollection>(commandWithParms.Parameters);
  19. }
  20.  
  21. public static class ObjectCopier
  22. {
  23. /// <summary>
  24. /// Perform a deep Copy of the object.
  25. /// </summary>
  26. /// <typeparam name="T">The type of object being copied.</typeparam>
  27. /// <param name="source">The object instance to copy.</param>
  28. /// <returns>The copied object.</returns>
  29. public static T Clone<T>(T source)
  30. {
  31. if (!typeof(T).IsSerializable)
  32. {
  33. throw new ArgumentException("The type must be serializable.", "source");
  34. }
  35.  
  36. // Don't serialize a null object, simply return the default for that object
  37. if (Object.ReferenceEquals(source, null))
  38. {
  39. return default(T);
  40. }
  41.  
  42. IFormatter formatter = new BinaryFormatter();
  43. Stream stream = new MemoryStream();
  44. using (stream)
  45. {
  46. formatter.Serialize(stream, source);
  47. stream.Seek(0, SeekOrigin.Begin);
  48. return (T)formatter.Deserialize(stream);
  49. }
  50. }
  51. }
  52.  
  53. // Copy parameters from cmd1 to cmd2
  54. // Creates an array with new parameters
  55. var nsp = cmd1.Parameters.Cast<ICloneable>().Select(x => x.Clone() as SqlParameter).Where(x => x != null).ToArray();
  56. // Copy parameters into another command
  57. cmd2.Parameters.AddRange(nsp);
  58.  
  59. Dim p1 As SqlClient.SqlParameter = CType(CType(p, ICloneable).Clone, SqlClient.SqlParameter)
Add Comment
Please, Sign In to add comment