943,929 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 6008
  • C# RSS
Oct 18th, 2007
0

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

Expand Post »
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...

C# Syntax (Toggle Plain Text)
  1.  
  2. private void btnCreate_Click_1(object sender, EventArgs e)
  3. {
  4. SqlConnection sqlConn = new SqlConnection("Connection String HERE");
  5. Microsoft.SqlServer.Management.Common.ServerConnection serverConn = new Microsoft.SqlServer.Management.Common.ServerConnection(sqlConn);
  6. Server svrSql = new Server(serverConn);
  7. Database db = svrSql.Databases["DBTest"];
  8. StoredProcedure spNew = new StoredProcedure(db, "StoredProc");
  9. spNew.TextMode = false;
  10. spNew.AnsiNullsStatus = false;
  11. spNew.QuotedIdentifierStatus = false;
  12.  
  13. spNew.TextBody = "Select * from TestTable";
  14. spNew.Create();
  15.  
  16. svrSql = null;
  17. }
  18.  
  19. private void btnDestroy_Click(object sender, EventArgs e)
  20. {
  21.  
  22. SqlConnection sqlConn = new SqlConnection("Connection String HERE");
  23. Microsoft.SqlServer.Management.Common.ServerConnection serverConn = new Microsoft.SqlServer.Management.Common.ServerConnection(sqlConn);
  24. Server svrSql = new Server(serverConn);
  25.  
  26.  
  27. Database db = svrSql.Databases["DBTest"];
  28. StoredProcedure spNew = new StoredProcedure(db, "StoredProc");
  29. spNew.Drop();
  30. svrSql = null;
  31.  
  32. }

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.

???
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
blacklocist is offline Offline
87 posts
since Apr 2006
Oct 18th, 2007
0

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

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.

C# Syntax (Toggle Plain Text)
  1. private void btnDestroy_Click(object sender, EventArgs e)
  2. {
  3.  
  4. SqlConnection sqlConn = new SqlConnection("Connection String HERE");
  5. Microsoft.SqlServer.Management.Common.ServerConnection serverConn = new Microsoft.SqlServer.Management.Common.ServerConnection(sqlConn);
  6. Server svrSql = new Server(serverConn);
  7.  
  8.  
  9. Database db = svrSql.Databases["DBTest"];
  10. StoredProcedure spNew = new StoredProcedure(db, "StoredProc");
  11.  
  12. //HERE IS THE KEY!
  13. spNew.Refresh();
  14.  
  15. spNew.Drop();
  16. svrSql = null;
  17.  
  18. }
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
blacklocist is offline Offline
87 posts
since Apr 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Compiling Problems code snippets
Next Thread in C# Forum Timeline: Anyone using dotnet 3.0





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC