public SyncResult AddTable(AddTableEntity table) { SyncResult result = new SyncResult(); try { bool isSuccess = SQLHelper.ExecuteSqlByAddTable(table); if (isSuccess) { result.result = 1; } else { result.result = 0; result.message = "执行未成功"; } } catch (Exception ex) { result.result = 0; result.message = ex.ToString(); } return result; }
public static bool ExecuteSqlByAddTable(AddTableEntity table) { string baseSQL = @"CREATE TABLE [{0}]( {1} "; string indexName = string.Format("PK_{0}", table.TableName); string columnStr = string.Empty; string pkStr = @" CONSTRAINT [{2}] PRIMARY KEY CLUSTERED ( {3} )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]"; string pkCol = string.Empty; bool isExitsPK = false; for (int i = 0; i < table.Columns.Count; i++) { ColumnItem item = table.Columns[i]; if (item.IsPrimaryKey) { isExitsPK = true; columnStr += string.Format("{0} NOT NULL,", GetColumnTypeStr(item.Name, item.Type, item.GetLegth(), item.GetLegth1())); pkCol += string.Format(" [{0}],", item.Name); } else { columnStr += string.Format(" {0} ,", GetColumnTypeStr(item.Name, item.Type, item.GetLegth(), item.GetLegth1())); } } string sql = string.Empty; if (isExitsPK) { sql = string.Format(baseSQL + pkStr, table.TableName, columnStr.TrimEnd(','), indexName, pkCol.TrimEnd(',')); } else { sql = string.Format(baseSQL + ")", table.TableName, columnStr.TrimEnd(',')); } int result = SQLHelper.ExecuteNonQuery(GetConnection(), CommandType.Text, sql, null); if (result == -1) { return true; } else { return false; } }