.net 筆記

學習.net
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

通過表名判斷數據庫表是否存在

Posted on 2007-10-04 10:00  陳偉  阅读(232)  评论(2)    收藏  举报

  //判斷表是否存在
        private string TableNull(string report_name)
        {
            //.........................................................modify 1..............................
            string reportname = report_name;
            SqlConnection myConnection = new SqlConnection(config);
            string mySelectQuery = "select count(*) AS Expr1 from INFORMATION_SCHEMA.TABLES where table_name='" + reportname+"'";//判断表是否存在的SQL语句
            SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);
            myConnection.Open();
            if ((int)myCommand.ExecuteScalar() == 0)
            {
                myConnection.Close();
                return "0";
            }
            else
            {
                myConnection.Close();
                return "1";
            }

        }