I am using SQLDataAdapter to run a script. Script is as below,
ALTER TABLE dbo.Table1 SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
select Has_Perms_By_Name(N'dbo.Table1', 'Object', 'ALTER') as ALT_Per, Has_Perms_By_Name(N'dbo.Table1', 'Object', 'VIEW DEFINITION') as View_def_Per, Has_Perms_By_Name(N'dbo.Table1', 'Object', 'CONTROL') as Contr_Per BEGIN TRANSACTION
GO
ALTER TABLE dbo.Table2
DROP CONSTRAINT DF_Table2
GO
It is giving me error as Incorrect syntax near GO. If I remove GO from the query text, it works fine. But I need to execute this script only.
Here is a c# code. QueryText is a string which contains the entire query along with the SET and GO.
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(_connectionString);
SqlDataAdapter da = new SqlDataAdapter(QueryText, conn);
da.Fill(ds);
Does anyone know how to go with it.
Thanks in advance,
Vijay
GO is a marker for Management Studio or other SQLServer command lines tools and serve as final delimiter to send a batch of commands to SQL Server.
In your code, you need to replicate this behavior.
Separate the script in subparts using the GO as delimiter.
Send each part using a single command (for the ALTER TABLE) and use your SqlDataAdapter for the SELECT part.
Also you should remove the COMMIT and BEGIN TRANSACTION from the script parts and use, inside your code, the SqlTransaction object from the SqlConnection.