How can I check if a View exists in a Database?

前端 未结 10 1698
无人共我
无人共我 2021-01-31 07:00

I have some SQL code that needs to be executed if a certain View exists in a database. How would I go about checking if the View exists?

EDIT: The DBMS being used is Mic

10条回答
  •  执念已碎
    2021-01-31 07:08

    To expand on Kevin's answer.

        private bool CustomViewExists(string viewName)
        {
            using (SalesPad.Data.DataConnection dc = yourconnection)
            {
                System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(String.Format(@"IF EXISTS(select * FROM sys.views where name = '{0}')
                    Select 1
                else
                    Select 0", viewName));
                cmd.CommandType = CommandType.Text;
                return Convert.ToBoolean(dc.ExecuteScalar(cmd));
            }
        }
    

提交回复
热议问题