Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.67 KB | None | 0 0
  1. //create strings
  2. string connStr = "--snip--";
  3. string cmdStr = "select field1, field2 from table1 where field3 = @field3";
  4. //create objects
  5. SqlConnection conn = new SqlConnection(connStr);
  6. SqlCommand cmd = new SqlCommand(cmdStr, conn);
  7. //add parameters
  8. cmd.Parameters.AddWithValue("@field3", "someValue");
  9. //open and execute
  10. try {
  11.     conn.Open();
  12.     cmd.ExecuteNonQuery();
  13. }
  14. //handle SqlExceptions
  15. catch (SqlException ex) {
  16.     //handle Exceptions
  17. }
  18. //handle generic exceptions
  19. catch (Exception ex) {
  20.     //handle exceptions
  21. }
  22. //no matter what, make sure the connection is closed
  23. finally {
  24.     if (conn.State != ConnectionState.Closed)
  25.         conn.Close();
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement