using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ImportPrevuXLSClientData { public partial class Form1 : Form { private Hashtable MissingValueList = new Hashtable(); private List SheetToTablMapList = new List(); private string m_connectionString; public Form1() { InitializeComponent(); buildSheetToTableMapList(); importDocTbx.Text = @"c:\temp\Client Setup DK 120115.xlsx"; } delegate void setGridDataSourceCallback(object src); public void setGridDataSource(object src) { if (this.dataGridView1.InvokeRequired) { setGridDataSourceCallback d = new setGridDataSourceCallback(setGridDataSource); object[] parms = { src }; this.dataGridView1.Invoke(d, parms); } else { this.dataGridView1.DataSource = src; } } // delegate void DisplayMessageCallback(Exception ex); public void DisplayMessage(Exception ex) { if (this.InvokeRequired) { DisplayMessageCallback d = new DisplayMessageCallback(DisplayMessage); object[] parms = { ex }; this.Invoke(d, parms); } else { MessageBox.Show(this, ex.ToString(), "Exception in add sql data"); } } delegate void DisplayMessageStringCallback(string ex); public void DisplayMessageString(string ex) { if (this.InvokeRequired) { DisplayMessageStringCallback d = new DisplayMessageStringCallback(DisplayMessageString); object[] parms = { ex }; this.Invoke(d, parms); } else { MessageBox.Show(this, ex, "Error in sql test"); } } delegate void setEnableFormCallback(bool enabled); public void setEnableForm(bool enabled) { if (this.InvokeRequired) { setEnableFormCallback d = new setEnableFormCallback(setEnableForm); object[] parms = { enabled }; this.Invoke(d, parms); } else { this.Enabled = enabled; } } private void button1_Click(object sender, EventArgs e) { System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ProcessData)); t.Start(); } private void ProcessData() { setEnableForm( false); buildCntxStr(); if (!checkCntxStr()) { setEnableForm(true); return; } if (sqlPrepScriptTbx.Text != null && sqlPrepScriptTbx.Text != "") { string sql = System.IO.File.ReadAllText(sqlPrepScriptTbx.Text); if (! this.execSql(sql)) { setEnableForm(true); return; } } Hashtable scripts = new Hashtable(); DataTable t = new DataTable(); foreach (SheetToTableMapping map in SheetToTablMapList) { ImportPrevuXLSClientData.importXlSData d = new importXlSData(); try { t= d.GetData(importDocTbx.Text, map.XLSSheetName); } catch (Exception dds) { DisplayMessage(dds); return; } //dataGridView1.DataSource = t; int onRow = 1; if (map.TableName == "StrategicGoal2Project") { } foreach (DataRow row in t.Rows) { onRow++; map.OnlineNumber = onRow; if (onRow < map.StartOnRow) continue; bool valueset = false; SheetToTableMapping tmpMap = map.clone(); if (tmpMap.TableName == "Scenario") { } foreach (ColumnMapping cmap in tmpMap.ColumnMap) { string val = null; if (cmap.XLSColumnName != null) { if (cmap.XLSColumnNumber.HasValue) { val = row[cmap.XLSColumnNumber.Value].ToString(); } else { val = row[cmap.XLSColumnName].ToString(); } if (cmap.required.HasValue) if (cmap.required.Value && (val == "" || val == null)) val = cmap.DefaultValue; } else if (cmap.DefaultValue != null && cmap.SqlTOGetValue == null) val = cmap.DefaultValue; else if (cmap.SqlTOGetValue != null) { string sql = cmap.SqlTOGetValue; int startidx = sql.IndexOf('%'); if (startidx > 0) { int lastidx = sql.LastIndexOf('%'); if (lastidx > startidx) { string xlscolname = sql.Substring(startidx + 1, lastidx - (startidx + 1)); string colval = row[xlscolname].ToString(); if (colval == "") colval = cmap.DefaultValue; if (cmap.DataType == "string[]" && cmap.DataDelim.HasValue && colval!=null) { string[] xlsvals = colval.Split(cmap.DataDelim.Value); string sep = ""; foreach (string xlsval in xlsvals) { string sql2 = sql.Replace("%" + xlscolname + "%", xlsval.Trim().TrimStart(' ')); val += sep+ getSqlValue(sql2); sep = cmap.DataDelim.Value.ToString(); } } else { if (colval == null) colval = ""; sql = sql.Replace("%" + xlscolname + "%", colval.Trim().TrimStart(' ')); val = getSqlValue(sql); ; } } } } if (val == "") val = null; if ((val != null && cmap.DefaultValue == null) || tmpMap.AlldefaultValues) valueset = true; cmap.value = val; } if (valueset) { string compareSql = tmpMap.getSql(false); if (compareSql != null && compareSql !="") { if (!scripts.ContainsKey(compareSql)) { scripts.Add(compareSql, tmpMap); string sqlinsert = tmpMap.getSql(true); if (!execSql(sqlinsert)) { buildReport(); setEnableForm(true); return; } } } else { MissingValueList.Add(tmpMap, t); } } else break; if (tmpMap.AlldefaultValues) break; } } //prevuew db on qa server:PrevuICMCVI buildReport(); setEnableForm(true); } private bool checkCntxStr() { bool ok = true; string testcmd= "select * from AspNetUsers"; string val=this.getSqlValue(testcmd); if (val == null) { DisplayMessageString("Error connecting to sql. Please check your settings!"); ok = false; } return ok; } private void buildReport() { List lines = new List(); string columns = "xslname |totable |line number|XLSColumnName|SqlTOGetValue|TableColumnName"; lines.Add(columns); foreach (SheetToTableMapping m in MissingValueList.Keys) { string xslname = m.XLSSheetName; string totable = m.TableName; int online = m.OnlineNumber; foreach(ColumnMapping c in m.ColumnMap) { if (c.required.HasValue) if (c.required.Value) if (c.value == null) { lines.Add(xslname + "|" + totable + "|" + online.ToString() + "|" + c.XLSColumnName + "|" + c.SqlTOGetValue + "|" + c.TableColumnName); } } } System.IO.File.WriteAllLines(errorLogTbx.Text, lines.ToArray()); DataTable t = new DataTable(); foreach (string c in columns.Split('|')) t.Columns.Add(c); foreach(string line in lines) { if (line != columns) t.Rows.Add(line.Split('|')); } setGridDataSource(t); } public bool execSql(string sqlcmd) { bool ok = true; using (SqlConnection connection = new SqlConnection(this.m_connectionString)) { using (SqlCommand command = new SqlCommand(sqlcmd, connection)) { try { //Mark the Command as a text connection.Open(); command.CommandType = CommandType.Text; command.ExecuteNonQuery(); } catch (System.Data.SqlClient.SqlException sqlexception) { DisplayMessage(sqlexception); ok = false; } catch (System.Exception exception) { DisplayMessage(exception); ok = false; } finally { //close the connection if (connection.State == ConnectionState.Open) connection.Close(); } } } return ok; } public string getSqlValue(string sqlcmd) { using (SqlConnection connection = new SqlConnection(this.m_connectionString)) { using (SqlCommand command = new SqlCommand(sqlcmd, connection)) { //Create a DataTable DataTable dataTable = new DataTable(); try { //Mark the Command as a text connection.Open(); command.CommandType = CommandType.Text; //Create a SQL Adapter SqlDataAdapter adapter = new SqlDataAdapter(command); //Fill the DataSet adapter.Fill(dataTable); } catch (System.Data.SqlClient.SqlException sqlexception) { } catch (System.Exception exception) { } finally { //close the connection if (connection.State == ConnectionState.Open) connection.Close(); } if (dataTable != null) { if (dataTable.Rows.Count > 0) { return dataTable.Rows[0][0].ToString(); } } return null; } } } private void buildSheetToTableMapList() { #region Company 0 Guid CompanyID = Guid.NewGuid(); List cols = new List(); ColumnMapping c = new ColumnMapping() { TableColumnName = "Id", DefaultValue=CompanyID.ToString(), DataType="string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Name", XLSColumnName = "Company Name", DataType = "string" }; cols.Add(c); SheetToTableMapping t = new SheetToTableMapping() { TableName = "Company", XLSSheetName = "Company Info", Order = 0, ID=CompanyID, ColumnMap=cols }; SheetToTablMapList.Add(t); #endregion #region Client 1 cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", XLSColumnName= "Client Number*", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Name", XLSColumnName = "Client Name", DataType = "string" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "Client", XLSSheetName = "Clients", Order = 1, TableMappingObj = new TableMapping() { ParentID = CompanyID, ParentName = "Company", ChildName = "Client", tableName = "Company2Client" }, ColumnMap=cols }; SheetToTablMapList.Add(t); #endregion #region CompanyToClient 1a cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "ClientId", SqlTOGetValue = "select top 1 Id from Client where Name='%Client Name%'", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "CompanyId", DefaultValue = CompanyID.ToString(), DataType = "string" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "Company2Client", XLSSheetName = "Clients", Order = 1, ColumnMap = cols }; SheetToTablMapList.Add(t); #endregion #region Contacts (internal) 2 cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Type", DefaultValue="0", DataType="int" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "LastName", XLSColumnName= "Last Name", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "FirstName", XLSColumnName = "First Name", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Email", XLSColumnName = "Email", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Phone", XLSColumnName = "Phone #", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "ParentId", DefaultValue= CompanyID.ToString(), DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "ContactClassification", DefaultValue ="0", DataType = "int" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "Contact", XLSSheetName = "Internal Contacts", Order =2, ColumnMap = cols }; SheetToTablMapList.Add(t); #endregion #region Contacts (external) 3 cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Type", DefaultValue = "1", DataType = "int" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "LastName", XLSColumnName = "Last Name", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "FirstName", XLSColumnName = "First Name", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Email", XLSColumnName = "Email", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Phone", XLSColumnName = "Phone #", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "ContactClassification", DefaultValue = "0", DataType = "int" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "Contact", XLSSheetName = "External Contacts", Order = 3, ColumnMap = cols }; SheetToTablMapList.Add(t); #endregion #region Expenditure 4 cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Name", XLSColumnName = "Expenditure Category Name", DataType = "string" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "Expenditure", XLSSheetName = "Roles", Order = 4, ColumnMap = cols }; SheetToTablMapList.Add(t); #endregion #region GLAccount 5 cols = new List(); Guid GLAccountID = Guid.NewGuid(); c = new ColumnMapping() { TableColumnName = "Id", DefaultValue= GLAccountID.ToString(), DataType = "guid" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "GLNumber", DefaultValue="1", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Name", DefaultValue = "N/A", DataType = "string" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "GLAccount", XLSSheetName = "Roles", Order = 5, ColumnMap = cols, AlldefaultValues=true }; SheetToTablMapList.Add(t); #endregion #region UOM 6 cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Name", DefaultValue = "Hours", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "UOMValue", XLSColumnName = "Standard Hours Per Week", DataType = "int" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "UOM", XLSSheetName = "Roles", Order = 6, ColumnMap = cols }; SheetToTablMapList.Add(t); #endregion #region CreditDepartment 7 cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Name", XLSColumnName = "Cost Center Name*", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "CreditNumber", XLSColumnName= "Cost Center*", DataType = "string" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "CreditDepartment", XLSSheetName = "Roles", Order = 7, ColumnMap = cols }; SheetToTablMapList.Add(t); #endregion #region ExpenditureCategory 8 cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "ExpenditureId", SqlTOGetValue = "Select TOP 1 Id from Expenditure where Name='%Expenditure Category Name%'", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "GLId", SqlTOGetValue = "Select TOP 1 Id from GLAccount where Name='%GL Account*%'", DefaultValue="N/A", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "UOMId", SqlTOGetValue = "Select TOP 1 Id from UOM where UOMValue='%Standard Hours Per Week%'", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "CreditId", SqlTOGetValue = "Select TOP 1 Id from CreditDepartment where CreditNumber='%Cost Center*%'", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Type", DefaultValue="1", DataType = "int" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "UseType", DefaultValue = "1", DataType = "int" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "CGEFX", DefaultValue = "CG", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Name", XLSColumnName= "Expenditure Category Name", DataType = "string" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "ExpenditureCategory", XLSSheetName = "Roles", Order = 8, ColumnMap = cols }; SheetToTablMapList.Add(t); #endregion #region FiscalCalendar 9 cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Name", XLSColumnName= "Name", XLSColumnNumber=0, DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Type", XLSColumnName= "Type", XLSColumnNumber = 1, DataType = "cal_type" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "YearInt", XLSColumnName= "YearInt", XLSColumnNumber = 2, DataType = "int" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "QuarterInt", XLSColumnName = "QuarterInt", XLSColumnNumber = 3, DataType = "int" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "PeriodInt", XLSColumnName = "PeriodInt", XLSColumnNumber = 4, DataType = "int" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "StartDate", XLSColumnName = "StartDate", XLSColumnNumber = 5, DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "EndDate", XLSColumnName = "EndDate", XLSColumnNumber = 6, DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "SystemName", XLSColumnName = "SystemName", XLSColumnNumber = 7, DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "NonWorking", XLSColumnName = "NonWorking", XLSColumnNumber = 8, DataType = "bool" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "AdjustingPeriod", XLSColumnName = "AdjustingPeriod", XLSColumnNumber = 9, DataType = "bool" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "FiscalCalendar", XLSSheetName = "Fiscal Calendar", Order = 9, ColumnMap = cols, StartOnRow=5 }; SheetToTablMapList.Add(t); #endregion #region Team 10 cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Name", XLSColumnName = "Team", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "CompanyId", DefaultValue = CompanyID.ToString(), DataType = "string" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "Team", XLSSheetName = "Teams", Order = 10, ColumnMap = cols }; SheetToTablMapList.Add(t); #endregion #region PeopleResource 11 cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "FirstName", XLSColumnName = "First Name", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "LastName", XLSColumnName = "Last Name", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "IsActiveEmployee", DefaultValue=true.ToString(), DataType = "bool" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "TeamId", SqlTOGetValue= "select top 1 Id from Team where Name='%Team Name%'", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "ExpenditureCategoryId", SqlTOGetValue = "select top 1 Id from ExpenditureCategory where Name='%Role (Expenditure Category)%'", DataType = "string", required=true }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "StartDate", XLSColumnName = "Start Date", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "EndDate", DefaultValue=DateTime.Now.AddYears(20).ToString(), DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "EmployeeID", XLSColumnName = "Email", DataType = "string" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "PeopleResource", XLSSheetName = "Resources", Order = 11, ColumnMap = cols }; SheetToTablMapList.Add(t); #endregion #region Status 12 cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Name", XLSColumnName = "Status Name", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Color", DefaultValue="red", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Probability100", XLSColumnName = "100% Probability (y/n)", DataType = "bool" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "IsSystem", DefaultValue = false.ToString(), DataType = "bool" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "Status", XLSSheetName = "Workflow", Order = 12, ColumnMap = cols }; SheetToTablMapList.Add(t); #endregion #region Type 13 cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Name", XLSColumnName = "Project Type", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "IsSystem", DefaultValue=false.ToString(), DataType = "bool" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "PerformanceRedThreshold", DefaultValue="0.50", DataType = "decimal" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "PerformanceYellowThreshold", DefaultValue = "0.35", DataType = "decimal" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "Type", XLSSheetName = "Classifications", Order = 13, ColumnMap = cols }; SheetToTablMapList.Add(t); #endregion #region Project 14 cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "CompanyId", DefaultValue=CompanyID.ToString(), DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "ClientId", SqlTOGetValue = "Select TOP 1 Id from Client where Name='%Client%'", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "TypeId", SqlTOGetValue = "Select TOP 1 Id from Type where Name='%Classification%'", DataType = "string", required=true }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "StatusId", SqlTOGetValue = "Select TOP 1 Id from Status where Name='%Status (workflow)%'", DataType = "string", required = true }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Name", XLSColumnName = "Project Name", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "ProjectNumber", XLSColumnName = "Project number", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Color", DataType = "color" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Details", XLSColumnName = "Details", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Priority", DefaultValue="1", DataType = "int" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Probability", XLSColumnName = "Probability (if not 100%)", DataType = "decimal", DecimalScale = 4, DecimalPrecision = 5, required = true, DefaultValue = "0.0" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "IsRevenueGenerating", XLSColumnName = "Revenue Generating? (Y/N)", DataType = "bool" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Deadline", XLSColumnName = "End Date", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "HasChildren", DefaultValue = false.ToString(), DataType = "bool" }; cols.Add(c); //c = new ColumnMapping() //{ // TableColumnName = "DateEdited", // DefaultValue = DateTime.Now.ToString("HH:mm:ss"), // DataType = "string" //}; //cols.Add(c); t = new SheetToTableMapping() { TableName = "Project", XLSSheetName = "Projects", Order = 14, ColumnMap = cols }; SheetToTablMapList.Add(t); #endregion #region Team2Project 15 cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "TeamId", SqlTOGetValue = "Select Id from Team where Name in('%Team(s)%')", DataType = "string", required = true }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "ProjectId", XLSColumnName = "Select Id from Projects where ProjectNumber in('%Project number%')", DataType = "string", required = true }; cols.Add(c); t = new SheetToTableMapping() { TableName = "Team2Project", XLSSheetName = "Projects", Order = 15, ColumnMap = cols }; //SheetToTablMapList.Add(t); #endregion #region scenario 16 cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "ParentId", SqlTOGetValue = "Select top 1 Id from Project where ProjectNumber='%Project number%'", DataType = "string", required=true }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Type", DefaultValue = "2", DataType = "int" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Name", DefaultValue = "Init", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "StartDate", XLSColumnName = "Start Date", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "EndDate", XLSColumnName = "End Date", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Status", DefaultValue = "1", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "FreezeRevenue", DefaultValue = false.ToString(), DataType = "bool" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "GrowthScenario", DefaultValue = false.ToString(), DataType = "bool" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "Scenario", XLSSheetName = "Projects", Order = 16, ColumnMap = cols }; SheetToTablMapList.Add(t); #endregion #region scenario(active) 16a cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "ParentId", SqlTOGetValue = "Select top 1 Id from Project where ProjectNumber='%Project number%'", DataType = "string", required = true }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Type", DefaultValue = "9", DataType = "int" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Name", DefaultValue = "ACTUALS", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "StartDate", XLSColumnName = "Start Date", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "EndDate", XLSColumnName = "End Date", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Status", DefaultValue = "1", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "FreezeRevenue", DefaultValue = false.ToString(), DataType = "bool" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "GrowthScenario", DefaultValue = false.ToString(), DataType = "bool" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "Scenario", XLSSheetName = "Projects", Order = 16, ColumnMap = cols }; SheetToTablMapList.Add(t); #endregion #region StrategicGoal 17 cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Name", XLSColumnName = "Goal Name*", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Description", XLSColumnName = "Goal Name*", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "StartDate", XLSColumnName = "Goal Start Date*", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "EndDate", XLSColumnName = "Goal End Date*", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Color", DefaultValue = "Blue", DataType = "string" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "StrategicGoal", XLSSheetName = "Strategic Goals", Order = 16, ColumnMap = cols }; SheetToTablMapList.Add(t); #endregion #region aspusers 18 string pwdhash = "ABLkZNsOHI0e5D0NLTnryOirw2X+f1PymfNHNZL64+PTNX2fgV89EjRcgVw97LwpSw=="; string securitystamp = "0bf19d6c-968c-4a2a-966a-786f0f8092fe"; cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "FirstName", XLSColumnName = "First Name", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "LastName", XLSColumnName = "Last Name", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "UserName", XLSColumnName = "Email", DataType = "userid" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "PasswordHash", DefaultValue = pwdhash, DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "SecurityStamp", DefaultValue = securitystamp, DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Discriminator", DefaultValue = "ApplicationUser", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Email", XLSColumnName = "Email", DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "Type", DefaultValue = "1", DataType = "int" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "PreferredResourceAllocation", DefaultValue = "true", DataType = "bool" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "PreferredTotalsDisplaying", DefaultValue = "false", DataType = "bool" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "AspNetUsers", XLSSheetName = "Resources", Order = 17, ColumnMap = cols }; SheetToTablMapList.Add(t); #endregion #region Team2Project 19 cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); ColumnMapping c1 = new ColumnMapping() { TableColumnName = "TeamId", // XLSColumnName = "Team(s)", SqlTOGetValue = "select Id from Team Where Name='%Team(s)%'", DataType = "string[]", DataDelim=',' }; cols.Add(c1); c = new ColumnMapping() { TableColumnName = "ProjectId", SqlTOGetValue= "Select top 1 Id from Project where ProjectNumber='%Project number%'", DataType = "string" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "Team2Project", XLSSheetName = "Projects", Order = 19, OccursCol= c1, ColumnMap = cols }; SheetToTablMapList.Add(t); #endregion #region StrategicGoal2Project 20 cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); c1 = new ColumnMapping() { TableColumnName = "StrategicGoalId", SqlTOGetValue = "select Id from StrategicGoal Where Name='%Strategic Goals%'", DataType = "string[]", required = true, DataDelim = ',' }; cols.Add(c1); c = new ColumnMapping() { TableColumnName = "ProjectId", SqlTOGetValue = "Select top 1 Id from Project where ProjectNumber='%Project number%'", required = true, DataType = "string" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "StrategicGoal2Project", XLSSheetName = "Projects", Order = 20, OccursCol = c1, ColumnMap = cols }; SheetToTablMapList.Add(t); #endregion #region Contact2Project 21 cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); c1 = new ColumnMapping() { TableColumnName = "ContactId", SqlTOGetValue = "select Id from Contact Where FirstName+' '+LastName='%Internal Contacts%'", DataType = "string[]", required = true, DataDelim = ',' }; cols.Add(c1); c = new ColumnMapping() { TableColumnName = "ShowId", SqlTOGetValue = "Select top 1 Id from Project where ProjectNumber='%Project number%'", required = true, DataType = "string" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "Contact2Project", XLSSheetName = "Projects", Order = 21, OccursCol = c1, ColumnMap = cols }; SheetToTablMapList.Add(t); cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); c1 = new ColumnMapping() { TableColumnName = "ContactId", SqlTOGetValue = "select Id from Contact Where FirstName+' '+LastName='%External Contacts%'", DataType = "string[]", required = true, DataDelim = ',' }; cols.Add(c1); c = new ColumnMapping() { TableColumnName = "ShowId", SqlTOGetValue = "Select top 1 Id from Project where ProjectNumber='%Project number%'", required = true, DataType = "string" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "Contact2Project", XLSSheetName = "Projects", Order = 21, OccursCol = c1, ColumnMap = cols }; SheetToTablMapList.Add(t); #endregion #region PeopleResourceAllocation 22 cols = new List(); c = new ColumnMapping() { TableColumnName = "Id", DataType = "guid" }; cols.Add(c); c1 = new ColumnMapping() { TableColumnName = "PeopleResourceId", SqlTOGetValue = "select Id from PeopleResource Where FirstName+' '+LastName='%Assigned Resources (Separate with Semicolon)%'", DataType = "string[]", required = true, DataDelim = ';' }; cols.Add(c1); c = new ColumnMapping() { TableColumnName = "ScenarioId", SqlTOGetValue = "Select top 1 s.Id from Scenario s join Project p on p.Id=s.ParentId where p.ProjectNumber='%Project number%'", required = true, DataType = "string" }; cols.Add(c); c = new ColumnMapping() { TableColumnName = "ExpenditureCategoryId", SqlTOGetValue = "select ExpenditureCategoryId from PeopleResource Where FirstName+' '+LastName='%Assigned Resources (Separate with Semicolon)%'", required = true, DataType = "string" }; cols.Add(c); t = new SheetToTableMapping() { TableName = "PeopleResourceAllocation", XLSSheetName = "Projects", Order = 21, OccursCol = c1, ColumnMap = cols }; SheetToTablMapList.Add(t); #endregion } public void AddTableToTableMappingRecord(string parent,Guid parentID,string child, Guid childID) { } public void EnableProcessButton() { string file = errorLogTbx.Text; string file2 = importDocTbx.Text; button1.Enabled = false; try { System.IO.File.Create(file); if (System.IO.File.Exists(file2) && System.IO.File.Exists(file)) button1.Enabled = true; } catch (Exception dd) { } } private void button2_Click(object sender, EventArgs e) { FileDialog d = new SaveFileDialog(); DialogResult r=d.ShowDialog(); if (r == DialogResult.OK) errorLogTbx.Text = d.FileName; } private void button3_Click(object sender, EventArgs e) { FileDialog d = new OpenFileDialog(); DialogResult r = d.ShowDialog(); if (r == DialogResult.OK) importDocTbx.Text = d.FileName; } private void button4_Click(object sender, EventArgs e) { FileDialog d = new OpenFileDialog(); DialogResult r = d.ShowDialog(); if (r == DialogResult.OK) sqlPrepScriptTbx.Text = d.FileName; } private void label6_Click(object sender, EventArgs e) { } private void textBox1_TextChanged(object sender, EventArgs e) { } public void buildCntxStr() { this.m_connectionString = ""; string serverpart = "Server = "; string dbpart = "Database ="; string user_nonWindows="user id = %userid%; password=%password%"; if (checkBox1.Checked) { string cntx = serverpart + databaseServerNametbx.Text + ";" + dbpart + databaseNameTbx.Text + ";Integrated Security=SSPI;"; this.m_connectionString = cntx; } else { string cntx = serverpart + databaseServerNametbx.Text + ";" + dbpart + databaseNameTbx.Text + ";" +user_nonWindows.Replace("%userid%", dbUserIDTbx.Text).Replace("%password%", dbPwdtbx.Text); this.m_connectionString = cntx; } } private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked) { dbUserIDTbx.Text = ""; dbUserIDTbx.Enabled = false; dbPwdtbx.Text = ""; dbPwdtbx.Enabled = false; } else { dbUserIDTbx.Text = ""; dbUserIDTbx.Enabled = true; dbPwdtbx.Text = ""; dbPwdtbx.Enabled = true; } } } public class TableMapping { public string ParentName { get; set; } public string ChildName { get; set; } public Guid ParentID { get; set; } public string tableName { get; set; } } public class ColumnMapping { public string TableColumnName { get; set; } public string XLSColumnName { get; set; } public string DefaultValue { get; set; } public string DataType { get; set; } public string SqlTOGetValue { get; set; } public string value { get; set; } public int? XLSColumnNumber { get; set; } public bool? required { get; set; } public int? DecimalScale { get; set; } public int? DecimalPrecision { get; set; } public char? DataDelim { get; set; } public ColumnMapping clone() { return new ColumnMapping() { TableColumnName = this.TableColumnName, XLSColumnName = this.XLSColumnName, XLSColumnNumber = this.XLSColumnNumber, value = this.value, DataDelim = this.DataDelim, DataType = this.DataType, DecimalPrecision = this.DecimalPrecision, DecimalScale = this.DecimalScale, DefaultValue = this.DefaultValue, required = this.required, SqlTOGetValue = this.SqlTOGetValue }; } } public class SheetToTableMapping { public string TableName { get; set; } public string XLSSheetName { get; set; } public int Order { get; set; } public Guid? ID { get; set; } public bool AlldefaultValues { get; set; } public TableMapping TableMappingObj { get; set; } public int StartOnRow { get; set; } public List ColumnMap { get; set; } public int OnlineNumber { get; set; } public ColumnMapping OccursCol { get; set; } public string getSql(bool generateGUID) { string sqlr = ""; if (OccursCol != null && OccursCol.value != null) { ColumnMapping c = ColumnMap.Where(x => x.TableColumnName == OccursCol.TableColumnName).FirstOrDefault(); string[] varray = c.value.Split(c.DataDelim.Value); foreach (string val in varray) { ColumnMapping temp = c.clone(); temp.value = val; sqlr += genSql(generateGUID, temp); } } else { sqlr = genSql(generateGUID, null); } return sqlr; } private string genSql(bool generateGUID, ColumnMapping insertMap) { string sql = "Insert into " + TableName + " ("; string vals = " values ("; string sep = ""; foreach (ColumnMapping d in ColumnMap) { ColumnMapping c = d.clone(); if (insertMap != null) { if (d.TableColumnName == insertMap.TableColumnName) c = insertMap.clone(); } sql += sep + c.TableColumnName + " "; string q = ""; if (c.DataType == "string" || c.DataType == "guid" || c.DataType == "string[]") q = "'"; if (c.DataType == "guid" && (c.value == null || c.value == "") && generateGUID) c.value = Guid.NewGuid().ToString(); else if (c.DataType == "guid" && (c.value == null || c.value == "") && !generateGUID) c.value = ""; if (c.DataType == "bool") { if (c.value.ToLower() == "true" || c.value.ToLower() == "y") c.value = "1"; else c.value = "0"; } if (c.DataType == "cal_type") { if (c.value.ToLower() == "week") c.value = "0"; if (c.value.ToLower() == "month") c.value = "1"; if (c.value.ToLower() == "quarter") c.value = "2"; } if (c.required.HasValue) { if (c.required.Value && c.value == null || c.value == "") { return null; } } if (c.DataType == "color" && c.value == null) { var random = new Random(); var color = String.Format("{0:X6}", random.Next(0x1000000)); c.value = color.ToString(); q = "'"; } if (c.DataType == "color") q = "'"; string val = null; if (c.value != null) { val = c.value.Replace("'", "''"); } else { val = "NULL"; q = ""; } if (c.DataType == "decimal" && val != "NULL") { if (val.IndexOf('.') < 0) { int iv = Int32.Parse(val); double dv = (double)iv / (double)100; val = dv.ToString(); } } if (c.DataType == "userid") { int idxs = val.IndexOf('@'); if (idxs > 0) val = val.Substring(0, idxs); q = "'"; } vals += sep + q + val + q + " "; sep = ","; } return sql + ") " + vals + ")"; } public SheetToTableMapping clone() { return new SheetToTableMapping() { TableMappingObj = this.TableMappingObj, ColumnMap = this.ColumnMap, ID = this.ID, Order = this.Order, TableName = this.TableName, XLSSheetName = this.XLSSheetName, AlldefaultValues=this.AlldefaultValues, OnlineNumber=this.OnlineNumber, OccursCol=this.OccursCol }; } } }