p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Pro VB.NET 2002/2003 (http://p2p.wrox.com/forumdisplay.php?f=74)
-   -   Create SQL Server users programmatically? (http://p2p.wrox.com/showthread.php?t=35935)

Dmitriy December 8th, 2005 02:09 PM

Create SQL Server users programmatically?
 
Hello,
I have a question for everybody:
How to create SQL Server's database user programmatically and how to assign the role (or permission level) for new or an existing user?
Any code examples or referrals will be gladly appreciated.
Thanks in advance,
   -Dmitriy[V]



BrianWren December 13th, 2005 06:12 PM

In the case of Oracle, there is a program for talking to the database textually called SQLPlus. Usin gthis program which looks much like NotePad, you can manipulate the database directly. You can retrieve information (records, table definitions, account stati, etc.).

You can also create users using language specific to Oracle.

If you create a connection to an Oracle DB in VB, you can do exactly the same thing by passing textual instructions over the connection, using a command object.

I’m quite sure the SQL Server is exactly the same.

It is important that the connection be made using the login of the person having the approriate authority level for what the actions will be, but I presume you will be able to do that.

Again, in Oracle, if you are going to make objects for the person to use (such as tables, stored procedures, etc), the connection will need to be established impersonating that user if they are to have authority over the objects. I presume it is similar in SQL Server.

planoie December 14th, 2005 03:37 PM

Go into MS SQL Query Analyzer, log into your favorite MSSQL DB, then enter "sp_help" in the query window (with the default install of the analyzer you can also hit ALT-F1 as a shortcut). You'll see a list of all the objects in the database. When on the 'master' DB you'll see all the master objects such as stored procedures used for system administration tasks.

Among those sprocs are these two:
sp_addlogin
sp_adduser

Those look like they may be part of what you need. A little more exploring around the SP_HELP list should find you some more.

A couple more helpful hints on some built in sprocs:
'sp_help' will show you the schema details of an object. Example:
    sp_help MyTable
    sp_help MyStoredProcedure
'sp_helptext' will show the text of a stored procedure. Example:
    sp_helptext MyStoredProcedure
    sp_helptext sp_helptext

You can highlite a db object name (table, sproc) and hit ALT-F1 and it will display the object's schema. If you work with sprocs a lot, you can add a similar shortbut by going to Tools->Customize and adding "sp_helptext" to one of the empty shortcut keys. Then you can highlite a sproc name and just hit the shortcut key to see the text of the sproc.

-Peter

Dmitriy December 15th, 2005 04:29 PM


Thanks very much for your input

-Dmitriy


navyjax2 October 29th, 2008 03:14 PM

See http://www.experts-exchange.com/Prog..._22856885.html

Code:

CREATE LOGIN [DOMAIN\username] FROM WINDOWS WITH DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english]
GO
EXEC sys.sp_addsrvrolemember @loginame = N' DOMAIN\username, @rolename = N'sysadmin'
EXEC sp_addrolemember 'db_owner', [DOMAIN\username]




All times are GMT -4. The time now is 07:54 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.