User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 456,554 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,485 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 2879 | Replies: 1 | Solved
Reply
Join Date: Apr 2006
Posts: 87
Reputation: blacklocist is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
blacklocist blacklocist is offline Offline
Junior Poster in Training

C# failing to drop stored procedure from SQL Server using SMO

  #1  
Oct 18th, 2007
Hi all,

I have been trying to drop a stored proc for sometime using SMO. I can create with ease but can't seem to drop. Any ideas? I thought it might be permission but for testing used SA account. No luck...

private void btnCreate_Click_1(object sender, EventArgs e)
{
SqlConnection sqlConn = new SqlConnection("Connection String HERE");
Microsoft.SqlServer.Management.Common.ServerConnection serverConn = new Microsoft.SqlServer.Management.Common.ServerConnection(sqlConn);
Server svrSql = new Server(serverConn);
Database db = svrSql.Databases["DBTest"];
StoredProcedure spNew = new StoredProcedure(db, "StoredProc");
spNew.TextMode = false;
spNew.AnsiNullsStatus = false;
spNew.QuotedIdentifierStatus = false;

spNew.TextBody = "Select * from TestTable";
spNew.Create();

svrSql = null;
}

private void btnDestroy_Click(object sender, EventArgs e)
{

SqlConnection sqlConn = new SqlConnection("Connection String HERE");
Microsoft.SqlServer.Management.Common.ServerConnection serverConn = new Microsoft.SqlServer.Management.Common.ServerConnection(sqlConn);
Server svrSql = new Server(serverConn);


Database db = svrSql.Databases["DBTest"];
StoredProcedure spNew = new StoredProcedure(db, "StoredProc");
spNew.Drop();
svrSql = null;

}

I keep geting a error of "You cannot execute this operation since the object has not been created". It is created, I can execute it, I can modify it, heck I can even delete it in SQL Management Studio with no problems.

???
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2006
Posts: 87
Reputation: blacklocist is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
blacklocist blacklocist is offline Offline
Junior Poster in Training

Re: C# failing to drop stored procedure from SQL Server using SMO

  #2  
Oct 18th, 2007
Man this sucks. It's always after I post a question I then answer it. You have to use the spNew.Refresh(). It believe it asks the SQL Server the current status of the stored proc. Then you are allowed to delete it with ease.

private void btnDestroy_Click(object sender, EventArgs e)
{

SqlConnection sqlConn = new SqlConnection("Connection String HERE");
Microsoft.SqlServer.Management.Common.ServerConnection serverConn = new Microsoft.SqlServer.Management.Common.ServerConnection(sqlConn);
Server svrSql = new Server(serverConn);


Database db = svrSql.Databases["DBTest"];
StoredProcedure spNew = new StoredProcedure(db, "StoredProc");

//HERE IS THE KEY!
spNew.Refresh();

spNew.Drop();
svrSql = null;

}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 5:26 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC