Sensitive data in database should be encrypted to protect it from malicious access. Do this in application is hard and costly. Utilizing the user-defined function technology supported by database management system, data encryption could be done separately and transparently for application. The principle and system architecture of encrypting database content with user-defined function is introduced, and the method handling long object encrypting is discussed specially. Experiments show that the proposed method is easy to implement, effective in system running and application development, and applicable for both new developing and old developed application.