I am trying to build the Web Page which can be used to run the TSQL scripts online.
My DB has so many databases and I am using the System Administrator account to execute these scripts.
After building the web page,
I tried to run the following command, which is very simple and it works perfectly in SSMS.
USE MyDB2
GO
CREATE PROCEDURE [dbo].MyProc
AS
BEGIN
INSERT INTO
dbo.Table1
(
FieldA
, FieldB
, FieldC
)
VALUES
(
'AAA'
, 'BBB'
, 'CCC'
)
END
But I am getting the error message:
'CREATE/ALTER PROCEDURE' must be the first statement in a query batch.
My c# code to execute the posted script is here:
DataTable dt = new DataTable();
try
{
using (SqlConnection cn = new SqlConnection("MyConnString"))
{
cn.Open();
SqlCommand cmd = new SqlCommand(sql, cn);
using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
if (dr.HasRows)
{
dt.Load(dr);
}
dr.Close();
}
cn.Close();
}
}
catch (Exception ex)
{
//Log Error
}
If I removed, USE statement, it was ok, but it created the object in the default DB which is MyDB1. How I can tweak my code to run it in MyDB2?
USE
is an SSMS command, not a SQL statement.
Change your connect string to specify the appropriate database.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments