zoukankan      html  css  js  c++  java
  • sql 2005判断某个表或某个表中的列是否存在

    /// <summary>
            /// 判断是否存在某表的某个字段
            /// </summary>
            /// <param name="tableName">表名称</param>
            /// <param name="columnName">列名称</param>
            /// <returns>是否存在</returns>
            public static bool ColumnExists(string tableName, string columnName)
            {
                string sql = "select count(1) from syscolumns where [id]=object_id('" + tableName + "') and [name]='" + columnName + "'";
                object res = GetSingle(sql);
                if (res == null)
                {
                    return false;
                }
                return Convert.ToInt32(res) > 0;
            }

    /// <summary>
            /// 表是否存在
            /// </summary>
            /// <param name="TableName"></param>
            /// <returns></returns>
            public static bool TabExists(string TableName)
            {
                string strsql = "select count(*) from sysobjects where id = object_id(N'[" + TableName + "]') and OBJECTPROPERTY(id, N'IsUserTable') = 1";
                //string strsql = "SELECT count(*) FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[" + TableName + "]') AND type in (N'U')";
                object obj = GetSingle(strsql);
                int cmdresult;
                if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                {
                    cmdresult = 0;
                }
                else
                {
                    cmdresult = int.Parse(obj.ToString());
                }
                if (cmdresult == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }

  • 相关阅读:
    软件新人问题解答(一)
    项目上线流程
    面试题:如何测试登录功能
    软件测试人员在工作中如何运用Linux
    认识接口测试
    刚入职的新人如何快速了解公司业务
    软件测试人员每天的工作日常
    面试题分析(二)如何避免漏测
    [谨记]记线上事故并分析原因
    面试经验分享:遇到不会回答的问题怎么回答才好
  • 原文地址:https://www.cnblogs.com/soundcode/p/2511092.html
Copyright © 2011-2022 走看看