protected override int ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) { DataTable table = new CsvDataTable().FileToDataTable(this.SourceFile, true); string originalKey = keys[this.KeyFieldName].ToString(); DataRow[] foundRows = table.Select(this.KeyFieldName + "=" + originalKey); string newKey = string.Empty; //we should find only 1 row if (foundRows.Length != 1) { throw new Exception("Invalid update operation. Please make sure that data contains unique key records"); } bool raiseKeyFieldUpdate = false; foundRows[0].BeginEdit(); for (int c = 0; c < table.Columns.Count; c++) { if ((table.Columns[c].Caption == this.KeyFieldName) && (values[table.Columns[c].Caption] != null)) { if (foundRows[0][c].ToString() != values[table.Columns[c].Caption].ToString()) { newKey = values[table.Columns[c].Caption].ToString(); raiseKeyFieldUpdate = true; } } if (values[table.Columns[c].Caption] != null) { foundRows[0][c] = values[table.Columns[c].Caption]; } } foundRows[0].EndEdit(); string operationId = this.FireBeforePerforming(this, "rowupdated", null); //todo-operationName as property to remove hardcoding string operationIdkfupdated = string.Empty; if (raiseKeyFieldUpdate) { operationIdkfupdated = this.FireBeforePerforming(this, "keyfieldupdated", null); } new CsvDataTable().DataTableToFile(table, this.sourceFile); Dictionary <string, object> Param = new Dictionary <string, object>(); Param.Add("UniqueKeyNameOfThisSetOfData", this.uniqueKeyNameOfThisSetOfData); Param.Add("oldKey", originalKey); Param.Add("newKey", newKey == string.Empty? originalKey:newKey); this.FireAfterPerforming(this, operationId, Param); if (raiseKeyFieldUpdate) { this.FireAfterPerforming(this, operationIdkfupdated, Param); } return(1); }
// Get data from the underlying data source. // Build and return a DataView, regardless of mode. protected override IEnumerable ExecuteSelect(DataSourceSelectArguments selectArgs) { IEnumerable dataList = null; // Open the .csv file. if (File.Exists(this.SourceFile)) { DataTable data = new CsvDataTable().FileToDataTable(this.SourceFile, this.IncludesColumnNames); #region Commented //DataTable data = new DataTable(); //// Open the file to read from. //using (StreamReader sr = File.OpenText(this.SourceFile)) //{ // // Parse the line // string s = ""; // string[] dataValues; // DataColumn col; // // Do the following to add schema. // dataValues = sr.ReadLine().Split(','); // // For each token in the comma-delimited string, add a column // // to the DataTable schema. // foreach (string token in dataValues) // { // col = new DataColumn(token, typeof(string)); // data.Columns.Add(col); // } // // Do not add the first row as data if the CSV file includes column names. // if (!IncludesColumnNames) // data.Rows.Add(CopyRowData(dataValues, data.NewRow())); // // Do the following to add data. // while ((s = sr.ReadLine()) != null) // { // dataValues = s.Split(','); // data.Rows.Add(CopyRowData(dataValues, data.NewRow())); // } //} #endregion Commented data.AcceptChanges(); DataView dataView = new DataView(data); if (selectArgs.SortExpression != String.Empty) { dataView.Sort = selectArgs.SortExpression; } dataList = dataView; } else { throw new System.Configuration.ConfigurationErrorsException("File not found, " + this.SourceFile); } if (null == dataList) { throw new InvalidOperationException("No data loaded from data source."); } return(dataList); }