Isidro Padilla

Isidro Padilla

  • 1.4k
  • 328
  • 19.6k

I can not dropping view with SMO

Apr 22 2020 7:33 AM
I can not Dropping View with SMO
Let's see, after googling a lot and going to 2 of the most
important C # forums I still don't understand why to delete
a View from a SQL Server database I have to create it and
then delete it, on the contrary, it won't delete a view
existing in any of the ways I've tried.
There must be some reason for it that I cannot understand.
On the other hand, what sense does what Microsotf does in
its example of the following link where it creates it and
immediately deletes it. Something that I consider absurd.
Link: https://docs.microsoft.com/ca-es/sql/relational-databases/server-management-objects-smo/tasks/creating-altering-and-removing-views?view=sql-server-2017&viewFallbackFrom=aps-pdw-2016-au7
private void BtnBorrarVista_Click(object sender, EventArgs e)
{
      try
      {
               if (MessageBox.Show("Está intentando borrar la Vista " + textBoxBorrarVista.Text + " ¿Está seguro de que quiere eliminarla?",                      "¡Atención!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
               {
                     m_Server = new Server(cmbServerName.Text);
                     if (!string.IsNullOrWhiteSpace(cmbServerName.Text))
                     {
                           if (m_Server != null && !string.IsNullOrWhiteSpace(cmbDbName.Text))
                           {
                                    Database myNewDatabase = m_Server.Databases[cmbDbName.Text];
                                    if (myNewDatabase != null)
                                    {
                                             Microsoft.SqlServer.Management.Smo.View view;
                                             view = new Microsoft.SqlServer.Management.Smo.View(myNewDatabase, textBoxBorrarVista.Text, "Sales");
                                             //=================Create view. Why? ===========================
                                             view.TextHeader = "CREATE VIEW [Sales]." + textBoxBorrarVista.Text + " AS";
                                             view.TextBody = "SELECT h.SalesOrderID, d.OrderQty FROM Sales.SalesOrderHeader AS h INNER JOIN                                                          Sales.SalesOrderDetail AS d ON h.SalesOrderID = d.SalesOrderID";
                                             //Create the view on the instance of SQL Server.
                                             view.Create();
                                             MessageBox.Show("La vista de la 'Tabla' 'Sales' de " + cmbDbName.Text + " se creó satisfactoriamente ");
                                             //==============================================================
                                             //Checking view 
                                             MessageBox.Show("Check view: " + view);
                                             if (view != null)
                                             {
                                                   view.Drop();
                                                   MessageBox.Show("La Vista " + textBoxBorrarVista.Text + " se borró con éxito");
                                             }
                                       }
                              }
                        }
                  }
                  else
                  {
                        MessageBox.Show("Operación abortada. La Vista " + textBoxBorrarVista.Text + " no se ha borrado");
                  }
            }
               catch (Exception ex)
            {
               MessageBox.Show(ex.Message.ToString());
         }
}

Answers (4)