Guest User

Untitled

a guest
Aug 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. The SqlParameter is already contained by another SqlParameterCollection - Does using() {} cheat?
  2. using (var conn = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True"))
  3. {
  4. var parameters = new SqlParameter[] { new SqlParameter("@ProductId", SqlDbType.Int ) };
  5.  
  6. using(var cmd1 = new SqlCommand("SELECT ProductName FROM Products WHERE ProductId = @ProductId"))
  7. {
  8. foreach (var parameter in parameters)
  9. {
  10. cmd1.Parameters.Add(parameter);
  11.  
  12. // cmd1.Parameters.Clear(); // uncomment to save your skin!
  13. }
  14. }
  15.  
  16. using (var cmd2 = new SqlCommand("SELECT Review FROM ProductReviews WHERE ProductId = @ProductId"))
  17. {
  18. foreach (var parameter in parameters)
  19. {
  20. cmd2.Parameters.Add(parameter);
  21. }
  22. }
  23. }
  24.  
  25. CREATE TABLE Products(
  26. ProductId int IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
  27. ProductName nvarchar(32) NOT NULL)
  28. GO
  29.  
  30. CREATE TABLE ProductReviews(
  31. ReviewId int IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
  32. ProductId int NOT NULL,
  33. Review nvarchar(128) NOT NULL)
  34.  
  35. GO
Add Comment
Please, Sign In to add comment