Manage account numbers for SQL Server programmatically

Source: Internet
Author: User
Server| programming Everyone is concerned about data security. If you don't care, you may not realize that it's easy to access sensitive data from the server. Because SQL Server has already considered this for us, we can use SQL Server's security policy directly when we develop the application.

In SQL Server, we can create a login account in Enterprise Manager and give certain permissions if we want to do it in our application?

SQL Server provides the following system procedures

One, sp_addlogin add login account

Sp_addlogin Login_id[,password[,defaultdb[,defaultlanguage]]]

LOGIN_ID account Name
Password Password
Defaultdb default database, default is master database.
DefaultLanguage Default language

Second, sp_adduser add users

Sp_adduser Login_id[,username[,rolename]]

Username the login Database alias
RoleName User-Subordinate group name

Third, sp_droplogin Delete account

Sp_droplogin login_id

Four, sp_dropuser Delete user

Sp_dropuser username

Grant grants user or group permission

GRANT permission_list on object_name to Name_list

List of permissions granted by Permission_list
object_name a table, view, or stored procedure that is granted permissions
Name_list a list of users or groups that are granted permissions

Vi. REVOKE Reclaim user or group permissions

REVOKE permission_list on object_name from Name_list

Seven, sp_password change password

sp_password [old_password,]new_password[,login_id]

The first six procedures can only be used by an SA or a person who is granted security administrators permission by the SA, and the seventh procedure may be used by ordinary users but cannot use login_id items, which are available only to those with the above permission

 

application Example:

Add to

DECLARE @login varchar (6), @pass varchar (11)

EXEC sp_addlogin @login, @pass, DatabaseName
exec sp_adduser @login, @login, public

Grant Insert,select,update,delete on Table1 to public

Delete

Revoke insert,delete on table1 from public

EXEC sp_dropuser @login
EXEC sp_droplogin @login

This code is tested in NT4+SQL7 and WIN98+SQL7 and nt4+sql6.5 through

References: SQL Server 7.0 Development Guide

Author: Water



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.