Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 sql users

Author  Topic 

acko
Yak Posting Veteran

52 Posts

Posted - 2003-09-11 : 05:18:15
Hi everybody
I want to add sqlusers from VB6 on sql server but everything i try is going bad.
I log like sa and using ADO to connect but what to do next, how can i use stored procedure sp_addlogin, sp_grantdbaccess or ADOX to create new users and logins on sqlserver2000.

Here is what i tryed, but is not working:

Dim cnLogin As New ADODB.Connection
Dim cmd As ADODB.Command
cnLogin.Open "Provider=" & strProvider & ";User ID=" & strSQLUserID & ";Password=" & strSQLPassword & "Initial Catalog=master;Data Source=" & strServer

Set cmd = New ADODB.Command
With cmd

.ActiveConnection = cnLogin
.CommandType = adCmdStoredProc
.CommandText = "sp_addlogin"
.Parameters(1) = Trim(txtIme.Text)
.Parameters(2) = txtSifra.Text
.Parameters(3) = "database"
.Execute


.CommandText = "sp_grantdbaccess"
.Parameters(1) = Trim(txtIme.Text)
.Execute

End With
I need step by step example how to add new login and map it to database and grant him some rights.
Thanks
Thanks

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-09-11 : 12:19:43
Why aren't you doing this in a stored procedure? It would be much easier this way.

Tara
Go to Top of Page

acko
Yak Posting Veteran

52 Posts

Posted - 2003-09-11 : 12:40:45
Can you explain me How?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-09-11 : 12:42:57
What part do you need an explanation on? How to create a stored procedure? I wouldn't want to duplicate any information that can be found in SQL Server Books Online.

sp_addlogin and sp_grantdbaccess are also well documented in BOL.

What part do you need help on?

Tara
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-09-11 : 12:44:37
Here is some code that can be put in a stored procedure:


if not exists (select * from master.dbo.syslogins where loginname = N'SomeUser')
BEGIN
declare @logindb nvarchar(132), @loginlang nvarchar(132) select @logindb = N'master', @loginlang = N'us_english'
if @logindb is null or not exists (select * from master.dbo.sysdatabases where name = @logindb)
select @logindb = N'master'
if @loginlang is null or (not exists (select * from master.dbo.syslanguages where name = @loginlang) and @loginlang <> N'us_english')
select @loginlang = @@language
exec sp_addlogin N'SomeUser', 'somepassword', @logindb, @loginlang
END
GO

if not exists (select * from dbo.sysusers where name = N'SomeUser' and uid < 16382)
EXEC sp_grantdbaccess N'SomeUser', N'SomeUser'
GO

if not exists (select * from dbo.sysusers where name = N'SomeRole' and uid > 16399)
EXEC sp_addrole N'SomeRole'
GO

EXEC sp_addrolemember N'SomeRole', N'SomeUser'
GO

GRANT SELECT ON SomeTable TO SomeRole
GRANT EXECUTE ON SomeProcedure TO SomeRole



Tara
Go to Top of Page
   

- Advertisement -