Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //create strings
- string connStr = "--snip--";
- string cmdStr = "select field1, field2 from table1 where field3 = @field3";
- //create objects
- SqlConnection conn = new SqlConnection(connStr);
- SqlCommand cmd = new SqlCommand(cmdStr, conn);
- //add parameters
- cmd.Parameters.AddWithValue("@field3", "someValue");
- //open and execute
- try {
- conn.Open();
- cmd.ExecuteNonQuery();
- }
- //handle SqlExceptions
- catch (SqlException ex) {
- //handle Exceptions
- }
- //handle generic exceptions
- catch (Exception ex) {
- //handle exceptions
- }
- //no matter what, make sure the connection is closed
- finally {
- if (conn.State != ConnectionState.Closed)
- conn.Close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement