//删除列 public static bool DeleteColumn(string dbname, string tableName, string colName, out string msg) { Database db = DbFunction.GetDB(dbname); string sql = string.Format("alter table {0} drop column {1} ", tableName, colName); msg = string.Empty; //检查是否存在列信息 if (!CheckColumnExist(dbname, tableName, colName)) { msg = "列" + colName + "不存在"; return(false); } try { DbFunction.ExecuteNonQuery(dbname, sql); return(true); } catch (Exception ex) { msg = ex.Message; } return(false); }
//创建表, 检查是否在数据库里存在这个表, 如果有的话, 判断是否有数据, 有数据则不允许删除 public static bool CreateTable(string dbname, string tableName, string Columns, string Key, out string msg) { msg = string.Empty; //如果已经存在就返回 if (CheckTableExist(dbname, tableName)) { msg = "表" + tableName + "已经存在"; return(false); } var sql = string.Empty; if (Key.Length > 0) { sql = string.Format("create table {0}({1} CONSTRAINT PK_{0} PRIMARY KEY({2}))", tableName, Columns, Key); } else { sql = string.Format("create table {0}({1})", tableName, Columns.Remove(Columns.Length - 1)); } try { DbFunction.ExecuteNonQuery(dbname, sql); return(true); } catch (Exception ex) { msg = ex.Message; } return(false); }
//修改表中的字段 public static bool ModifyColumn(string dbname, string tableName, string colName, string colType, string colLenght, string defaultValue, string nullable, out string msg) { Database db = DbFunction.GetDB(dbname); msg = string.Empty; //检查是否存在列信息 if (!CheckColumnExist(dbname, tableName, colName)) { msg = "列" + colName + "不存在"; return(false); } var isOraDb = db.DBType == DataBaseType.ORA; string sql = string.Format("alter table {0} alter column {1} ", tableName, colName); if (isOraDb) { sql = string.Format("alter table {0} modify ( {1} ", tableName, colName); } sql += getColTypeStr(db, colType, colLenght); //默认值 if (!string.IsNullOrEmpty(defaultValue)) { sql += string.Format(" default {0}", defaultValue); } //是否为空 if (nullable == "0") { sql += " not null"; } else { sql += " null"; } if (isOraDb) { sql += ")"; } try { DbFunction.ExecuteNonQuery(dbname, sql); return(true); } catch (Exception ex) { msg = ex.Message; } return(false); }