public ActionResult TableEdit_SqlServer(FormCollection collection) { string empty = string.Empty; string empty2 = string.Empty; RoadFlow.Platform.DBConnection dBConnection = new RoadFlow.Platform.DBConnection(); DataTable dataTable = new DataTable(); IDbConnection dbConnection = null; List <string> list = new List <string>(); RoadFlow.Data.Model.DBConnection dBConnection2 = null; bool flag = false; List <string> systemDataTables = RoadFlow.Utility.Config.get_SystemDataTables(); empty = base.Request.QueryString["dbconnid"]; empty2 = base.Request.QueryString["tablename"]; if (MyExtensions.IsNullOrEmpty(empty2)) { empty2 = "NEWTABLE_" + Tools.GetRandomString(5); flag = true; } if (MyExtensions.IsGuid(empty) && !MyExtensions.IsNullOrEmpty(empty2)) { dBConnection2 = dBConnection.Get(MyExtensions.ToGuid(empty)); if (dBConnection2 != null) { dbConnection = dBConnection.GetConnection(dBConnection2); if (dbConnection != null) { if (dbConnection.State != ConnectionState.Open) { dbConnection.Open(); } dataTable = dBConnection.GetTableSchema(dbConnection, empty2, dBConnection2.Type); list = dBConnection.GetPrimaryKey(dBConnection2, empty2); } } } if (flag) { empty2 = ""; } if (dataTable.Rows.Count == 0) { DataRow dataRow = dataTable.NewRow(); dataRow["f_name"] = "ID"; dataRow["t_name"] = "int"; dataRow["is_null"] = 0; dataRow["isidentity"] = 1; dataTable.Rows.Add(dataRow); list.Add("ID"); } base.ViewBag.PrimaryKeyList = list; base.ViewBag.IsAddTable = flag; base.ViewBag.tableName = empty2; if (collection != null) { if (dBConnection2 == null) { base.ViewBag.ClientScript = "alert('未找到数据连接!');"; return(View(dataTable)); } string[] array = (base.Request.Form["f_name"] ?? "").Split(','); string text = base.Request.Form["tablename"]; string oldtablename = base.Request.Form["oldtablename"]; string text2 = base.Request.Form["delfield"] ?? ""; StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder2 = new StringBuilder(); if (systemDataTables.Find((string p) => p.Equals(oldtablename, StringComparison.CurrentCultureIgnoreCase)) != null) { base.ViewBag.ClientScript = "alert('您不能修改系统表!');"; return(View(dataTable)); } string[] array2; if (flag) { stringBuilder.Append("CREATE TABLE [" + text + "] ("); oldtablename = text; } else { foreach (string constraint in dBConnection.GetConstraints(dBConnection2, oldtablename)) { stringBuilder.Append("ALTER TABLE [" + oldtablename + "] DROP CONSTRAINT [" + constraint + "];"); } StringBuilder stringBuilder3 = new StringBuilder(); array2 = text2.Split(','); foreach (string text3 in array2) { if (!MyExtensions.IsNullOrEmpty(text3) && dataTable.Select("f_name='" + text3 + "'").Length != 0) { stringBuilder3.Append("[" + text3 + "],"); } } if (stringBuilder3.Length > 0) { stringBuilder.Append("ALTER TABLE [" + oldtablename + "] DROP COLUMN " + stringBuilder3.ToString().TrimEnd(',') + ";"); } } List <string> list2 = new List <string>(); array2 = array; foreach (string text4 in array2) { string text5 = base.Request.Form[text4 + "_name1"]; string text6 = base.Request.Form[text4 + "_type"]; string text7 = base.Request.Form[text4 + "_length"]; string b = base.Request.Form[text4 + "_isnull"]; string b2 = base.Request.Form[text4 + "_isidentity"]; string b3 = base.Request.Form[text4 + "_primarykey"]; string text8 = base.Request.Form[text4 + "_defaultvalue"]; string b4 = base.Request.Form[text4 + "_isadd"]; if (!MyExtensions.IsNullOrEmpty(text5) && !MyExtensions.IsNullOrEmpty(text6)) { string text9 = string.Empty; switch (text6) { case "varchar": case "nvarchar": text9 = text6 + "(" + ((!MyExtensions.IsInt(text7)) ? "50" : ((MyExtensions.ToInt(text7) == -1) ? "MAX" : text7)) + ")"; break; case "char": text9 = text6 + "(" + (MyExtensions.IsInt(text7) ? text7 : "50") + ")"; break; case "datetime": case "text": case "uniqueidentifier": case "int": case "money": case "float": text9 = text6; break; case "decimal": text9 = text6 + "(" + (MyExtensions.IsNullOrEmpty(text7) ? "18,2" : text7) + ")"; break; } string text10 = ("1" == b) ? " NULL" : " NOT NULL"; string text11 = ("1" == b2) ? " IDENTITY(1,1)" : ""; bool flag2 = "1" == b4; if ("1" == b3) { list2.Add(text5); } if (flag) { stringBuilder.Append("[" + text5 + "] "); stringBuilder.Append(text9); stringBuilder.Append(" " + text10); stringBuilder.Append(" " + text11); if (!MyExtensions.IsNullOrEmpty(text8)) { stringBuilder.Append(" DEFAULT " + text8); } if (!text4.Equals(array.Last(), StringComparison.CurrentCultureIgnoreCase)) { stringBuilder.Append(","); } } else { if (!flag2 && MyExtensions.IsNullOrEmpty(text11) && dataTable.Select("f_name='" + text4 + "' and isidentity=1").Length != 0) { stringBuilder.Append("ALTER TABLE [" + oldtablename + "] DROP COLUMN [" + text4 + "];"); stringBuilder.Append("ALTER TABLE [" + oldtablename + "] ADD [" + text5 + "] " + text9 + text11 + text10 + ";"); } else if (!MyExtensions.IsNullOrEmpty(text11) && !flag2) { stringBuilder.Append("ALTER TABLE [" + oldtablename + "] DROP COLUMN [" + text4 + "];ALTER TABLE [" + oldtablename + "] ADD [" + text5 + "] int NOT NULL IDENTITY(1,1);"); } else { if (flag2) { stringBuilder.Append("ALTER TABLE [" + oldtablename + "] ADD [" + text5 + "] " + text9 + text11 + text10 + ";"); } else { stringBuilder.Append("ALTER TABLE [" + oldtablename + "] ALTER COLUMN [" + text4 + "] " + text9 + text11 + text10 + ";"); } if (!flag2 && !text4.Equals(text5, StringComparison.CurrentCultureIgnoreCase)) { stringBuilder.Append("EXEC sp_rename N'[" + oldtablename + "].[" + text4 + "]', N'" + text5 + "', 'COLUMN';"); } } if (!MyExtensions.IsNullOrEmpty(text8)) { stringBuilder.Append("ALTER TABLE [" + oldtablename + "] ADD CONSTRAINT [DF_" + text + "_" + text4 + "] DEFAULT (" + text8 + ") FOR [" + text4 + "];"); } } } } if (flag) { if (list2.Count > 0) { stringBuilder.Append(", PRIMARY KEY ("); foreach (string item in list2) { stringBuilder.Append("[" + item + "]"); if (!item.Equals(list2.Last())) { stringBuilder.Append(","); } } stringBuilder.Append(")"); } stringBuilder.Append(")"); } else { if (list2.Count > 0) { stringBuilder2.Append("ALTER TABLE [" + text + "] ADD CONSTRAINT [PK_" + text + "] PRIMARY KEY ("); foreach (string item2 in list2) { stringBuilder2.Append("[" + item2 + "]"); if (!item2.Equals(list2.Last())) { stringBuilder2.Append(","); } } stringBuilder2.Append(");"); } if (!text.Equals(oldtablename, StringComparison.CurrentCultureIgnoreCase)) { stringBuilder.Append("EXEC sp_rename '" + oldtablename + "', '" + text + "';"); } } string text12 = stringBuilder.ToString(); string msg; bool flag3 = dBConnection.TestSql(dBConnection2, text12, out msg, false); if (flag3 && stringBuilder2.Length > 0) { flag3 = dBConnection.TestSql(dBConnection2, stringBuilder2.ToString(), out msg, false); } string text13 = "TableEdit_SqlServer?dbconnid=" + empty + "&tablename=" + text + "&connid=" + empty + "&appid=" + base.Request.QueryString["appid"] + "&tabid=" + base.Request.QueryString["tabid"] + "&s_Name=" + base.Request.QueryString["s_Name"]; if (flag3) { RoadFlow.Platform.Log.Add("修改表结构成功-" + dBConnection2.Name + "-" + oldtablename, text12, RoadFlow.Platform.Log.Types.数据连接); base.ViewBag.ClientScript = "alert('保存成功!');window.location='" + text13 + "';"; return(View(dataTable)); } RoadFlow.Platform.Log.Add("修改表结构失败-" + dBConnection2.Name + "-" + oldtablename, text12, RoadFlow.Platform.Log.Types.数据连接); base.ViewBag.ClientScript = "alert('保存失败-" + msg.Replace("'", "") + "!');window.location='" + text13 + "';"; return(View(dataTable)); } return(View(dataTable)); }