/// <summary> /// 核查表是否存在 /// </summary> /// <param name="tableName">表名</param> /// <returns></returns> public bool ExistTable(string tableName) { var sqlHelper = new SqlHelper.SqlHelper(); string cmdText = string.Format("SELECT Count(*) FROM SYSOBJECTS WHERE ID = OBJECT_ID('{0}') AND OBJECTPROPERTY(ID, 'IsUserTable') = 1", tableName); return(sqlHelper.ExecuteScalar(cmdText).ToInt() > 0 ? true : false); }
/// <summary> /// 创建表 /// </summary> /// <param name="tableName">表名</param> /// <returns></returns> public bool CreateTable(string tableName) { var sqlHelper = new SqlHelper.SqlHelper(); string cmdText = string.Format("\r\n CREATE TABLE {0} \r\n (\r\n\t [ID] INT IDENTITY(1,1) PRIMARY KEY \r\n ) \r\n ", tableName); return(sqlHelper.ExecuteNonQuery(cmdText) > 0 ? true : false); }
/// <summary> /// 核查字段是否存在 /// </summary> /// <param name="tableName">表名</param> /// <param name="fieldName">字段名</param> /// <returns></returns> public bool ExistField(string tableName, string fieldName) { var sqlHelper = new SqlHelper.SqlHelper(); //查询字段 string cmdTextField = string.Format("select Count(*) from syscolumns where id=object_id('{0}') and name='{1}'", tableName, fieldName); return(sqlHelper.ExecuteScalar(cmdTextField).ToInt() > 0 ? true : false); }
/// <summary> /// 添加字段 /// </summary> /// <param name="tableName">表名</param> /// <param name="fieldName">字段名</param> /// <param name="type">字段类型</param> public bool AddField(string tableName, string fieldName, string type) { string cmdText = "ALTER TABLE [" + tableName + "] ADD {0}"; switch (type.ToLower()) { case "int32": cmdText = string.Format(cmdText, "[" + fieldName + "] [int]"); break; case "string": cmdText = string.Format(cmdText, string.Concat(new object[] { "[", fieldName, "] [nvarchar] (", 256, ")" })); break; case "datetime": cmdText = string.Format(cmdText, "[" + fieldName + "] [datetime]"); break; case "boolean": cmdText = string.Format(cmdText, "[" + fieldName + "] [bit]"); break; case "bool": cmdText = string.Format(cmdText, "[" + fieldName + "] [bit]"); break; case "decimal": cmdText = string.Format(cmdText, "[" + fieldName + "] [decimal]"); break; case "nvarchar": cmdText = string.Format(cmdText, string.Concat(new object[] { "[", fieldName, "] [nvarchar] (", 256, ")" })); break; case "varchar": cmdText = string.Format(cmdText, string.Concat(new object[] { "[", fieldName, "] [varchar] (", 256, ")" })); break; case "ntext": cmdText = string.Format(cmdText, "[" + fieldName + "] [ntext]"); break; case "text": cmdText = string.Format(cmdText, "[" + fieldName + "] [text]"); break; case "bit": cmdText = string.Format(cmdText, "[" + fieldName + "] [bit]"); break; case "money": cmdText = string.Format(cmdText, "[" + fieldName + "] [money]"); break; } var sqlHelper = new SqlHelper.SqlHelper(); return(sqlHelper.ExecuteNonQuery(cmdText) == 0 ? false : true); }