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;
??????????? }
??????? }
總結
以上是生活随笔為你收集整理的sql 2005判断某个表或某个表中的列是否存在的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 威士忌怎么喝 享受威士忌的口感和醇香?
- 下一篇: 黑麦和黑苦荞的区别?