Guest User

Untitled

a guest
Sep 10th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. Why does a connection have to be open for a nonquery but not to fill a dataset?
  2. SqlConnection cw_con = new SqlConnection("Server=Server;Database=Database;User=User;password=password");
  3. SqlCommand cmd = new SqlCommand("SELECT * FROM Example WHERE value = value");
  4. cmd.Connection = cw_con;
  5.  
  6. //Create DataSet
  7. DataSet cw_ds = new DataSet("cw_ds");
  8. SqlDataAdapter da = new SqlDataAdapter();
  9. da.SelectCommand = cmd;
  10. //Execute Command and Fill DataSet
  11. da.Fill(cw_ds);
  12. cw_ds.Clear();
  13.  
  14. SqlConnection cw_con = new SqlConnection("Server=Server;Database=Database;User=User;password=password");
  15. cw_con.Open();
  16. SqlCommand cmd = new SqlCommand("UPDATE example SET value = value WHERE value = value");
  17. cmd.Connection = cw_con;
  18. cmd.ExecuteNonQuery();
  19. cw_con.Close();
  20.  
  21. SqlCommand InsertCmd = new ("Insert statement", cw_con)
  22.  
  23. cw_con.Open();
  24.  
  25. foreach (var newitem in collection)
  26. {
  27. --Set Parameters
  28. UpdateCmd.ExecuteNonQuery();
  29. }
  30.  
  31. cw_con.Close();
Add Comment
Please, Sign In to add comment