Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > Pro VB.NET 2002/2003
Pro VB.NET 2002/2003 For advanced Visual Basic coders working .NET version 2002/2003. Beginning-level questions will be redirected to other forums, including Beginning VB.NET.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old December 8th, 2005, 02:09 PM
Friend of Wrox
Join Date: Jul 2003
Posts: 121
Thanks: 1
Thanked 0 Times in 0 Posts
Default Create SQL Server users programmatically?

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,

Old December 13th, 2005, 06:12 PM
Friend of Wrox
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts

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.
Old December 14th, 2005, 03:37 PM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

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:

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.

Old December 15th, 2005, 04:29 PM
Friend of Wrox
Join Date: Jul 2003
Posts: 121
Thanks: 1
Thanked 0 Times in 0 Posts

Thanks very much for your input


Old October 29th, 2008, 03:14 PM
Registered User
Join Date: Oct 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts

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

EXEC sys.sp_addsrvrolemember @loginame = N' DOMAIN\username, @rolename = N'sysadmin'
EXEC sp_addrolemember 'db_owner', [DOMAIN\username]

Similar Threads
Thread Thread Starter Forum Replies Last Post
Create SQL Server user programmatically? Dmitriy Pro VB Databases 1 March 8th, 2006 10:02 AM
Create SQL Server database user programmatically? Dmitriy VB.NET 3 January 3rd, 2006 12:30 PM
create SQL Server table from Access PorcupineRabbit SQL Server DTS 2 December 30th, 2005 06:11 AM
Create SQL Server database user programmatically? Dmitriy Pro VB 6 0 December 8th, 2005 02:04 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.