Scenario :
I am creating a admin tool in C# which will restore a full sql server backup in a server and apply the transaction logs. After db come up. I have run a few scripts shown below.
use master
EXEC sp_addlogin '<destsid>', '<sidadmpass>','<dbname>'
use <dbname>
EXEC sp_grantdbaccess '<destsid>'
EXEC sp_addrolemember 'db_owner', '<destsid>'
EXEC sp_change_users_login 'Update_One','<destsid>','<destsid>'
use master
EXEC sp_grantdbaccess '<destsid>'
EXEC sp_addrolemember 'db_owner', '<destsid>'
use msdb
EXEC sp_grantdbaccess '<destsid>'
EXEC sp_addrolemember 'db_owner', '<destsid>'
use model
EXEC sp_grantdbaccess '<destsid>'
EXEC sp_addrolemember 'db_owner', '<destsid>'
use tempdb
EXEC sp_grantdbaccess '<destsid>'
EXEC sp_addrolemember 'db_owner', '<destsid>'
use master
grant all on xp_cmdshell to <destsid>
EXEC sp_addsrvrolemember '<destsid>', 'serveradmin'
EXEC sp_addsrvrolemember '<destsid>', 'dbcreator'
EXEC sp_addsrvrolemember '<destsid>', 'bulkadmin'
use <dbname>
if object_id('sp_change_sapuser') is not null
drop procedure sp_change_sapuser
create procedure sp_change_sapuser @oldid sysname, @newid sysname
as begin
declare @oldid_uid smallint
declare @newid_uid smallint
declare @object sysname
declare @object_full nvarchar(999)
set @oldid_uid = user_id(@oldid)
set @newid_uid = user_id(@newid)
if @oldid_uid is not null and @newid_uid is not null
begin
declare object_cursor cursor local for
select name from sysobjects where((xtype='U' and name <> 'dtproperties') or (xtype='V' and name not in ('syssegments','sysconstraints')) or (xtype='P' and name not like 'dt_%') or (xtype='D' and name not like 'DF__dtpropert%') or (xtype in ('FN','TF','IF'))) and @oldid_uid = uid
open object_cursor
fetch next from object_cursor into @object
while @@fetch_status=0
begin
set @object_full = user_name(@oldid_uid) + '.' + @object
exec sp_changeobjectowner @object_full, @newid
fetch next from object_cursor into @object
end
end
else
if @oldid_uid is null
begin
print '*** old database user does not exist ***'
end
if @newid_uid is null
begin
print '*** new database user does not exist ***'
end
end
exec sp_change_sapuser '<sourcesid>', '<destsid>'
use master
EXEC sp_grantlogin 'AMR\<destsid>adm'
EXEC sp_grantlogin 'AMR\SAPService<dbname>'
EXEC sp_defaultdb 'AMR\<destsid>adm','<dbname>'
EXEC sp_defaultdb 'AMR\SAPService<dbname>','<dbname>'
EXEC sp_addsrvrolemember 'AMR\<destsid>adm', 'sysadmin'
EXEC sp_addsrvrolemember 'AMR\SAPService<dbname>', 'sysadmin'
EXEC xp_sqlagent_proxy_account 'SET', '<servername>', 'SAPMssXPUser',N'<sidadmpass>'
use master
EXEC sp_addlogin '<destsid>', '<sidadmpass>', <dbname>
use <dbname>
EXEC sp_change_users_login 'Update_One','<destsid>','<destsid>'
EXEC sp_grantdbaccess '<destsid>'
EXEC sp_addrolemember 'db_owner', '<destsid>'
use master
EXEC sp_grantdbaccess '<destsid>'
EXEC sp_addrolemember 'db_owner', '<destsid>'
use msdb
EXEC sp_grantdbaccess '<destsid>'
EXEC sp_addrolemember 'db_owner', '<destsid>'
use model
EXEC sp_grantdbaccess '<destsid>'
EXEC sp_addrolemember 'db_owner', '<destsid>'
use tempdb
EXEC sp_grantdbaccess '<destsid>'
EXEC sp_addrolemember 'db_owner', '<destsid>'
use master
grant all on xp_cmdshell to <destsid>
EXEC sp_addsrvrolemember '<destsid>', 'serveradmin'
EXEC sp_addsrvrolemember '<destsid>', 'dbcreator'
EXEC sp_addsrvrolemember '<destsid>', 'bulkadmin'
--EXEC xp_sqlagent_proxy_account 'SET', '<servername>', 'SAPMssXPUser', N'<sidadmpass>'
EXEC sp_grantlogin '<destsid>adm'
EXEC sp_grantlogin 'SAPService<dbname>'
EXEC sp_defaultdb '<destsid>adm','<dbname>'
EXEC sp_defaultdb 'SAPService<dbname>','<dbname>'
EXEC sp_addsrvrolemember '<destsid>adm', 'sysadmin'
EXEC sp_addsrvrolemember 'SAPService<dbname>', 'sysadmin'
Note : values given will be replaced with actual values.
While running these scripts few may throw exceptions. But i want to continue even though some scripts throws error. And also i have removed GO(batch terminator) because i cannot add separate dll into server. Please suggest some idea to get this problem solved.
Details :
Programming lang : C#
Db – Sql Server 2005,08
You can’t just miss out the
GOs. For some statements (e.g.CREATE PROCEDURE), it is important that they exist in their own separate batch.GOis an instruction to client tools (e.g. SSMS) that it should send the preceding text (back to the previousGOor the start of the script, whichever is later) to the server as a batch. So if you’re sending SQL to the server through code, you ought to do the same thing – or have each batch stored as separate pieces of text so you don’t have to worry about parsing the text (think about multi-line string literals that may contain the word GO).For each batch (originally delimited by
GOs in your original script), you need to send that as a separate batch to the server by callingExecuteNonQueryor similar – then create a newSqlCommandobject (or replace the text of the existing one) with the next batch to be executed.You might then decide to deal with any errors the come from the SQL execution in your C# code, and just move to setting up the next batch and executing it.