HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Dataset Copy, Change, then Write new Rows back

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
rowsnewwritedatasetbackthenchangecopy

Problem

I have this code, but I think it is too clumsy. It feels like a 12 point turn, is there a way to optimize it?

Please note that this generates new rows from existing rows.

```
//Create New Dataset
DataSet myDataSet = new DataSet();
DataTable myDataTable = myDataSet.Tables.Add("Temp");
myDataTable.Columns.Add("ID");
myDataTable.Columns.Add("JobNumber");
myDataTable.Columns.Add("DirectoryName");
myDataTable.Columns.Add("DrawingName");
myDataTable.Columns.Add("DateAdded");
myDataTable.Columns.Add("LastAccessedDate");
myDataTable.Columns.Add("LastAccessedUserName");
myDataTable.Columns.Add("ClientName");
myDataTable.Columns.Add("ContentType");
myDataTable.Columns.Add("Description");

//Read From Database
nexusDBDataSet.Drawing_Table.Clear();
drawing_TableTableAdapter.FillByJobNumber(nexusDBDataSet.Drawing_Table, OldJobcardNumber);
foreach (DataRow myRow in nexusDBDataSet.Drawing_Table.Rows)
{
//Read from database
string DirectoryName = myRow["DirectoryName"].ToString();
string DrawingName = myRow["DrawingName"].ToString();
string DateAdded = myRow["DateAdded"].ToString();
string LastAccessedDate = myRow["LastAccessedDate"].ToString();
string LastAccessedUserName = myRow["LastAccessedUserName"].ToString();
string ClientName = myRow["ClientName"].ToString();
string ContentType = myRow["ContentType"].ToString();
string Description = myRow["Description"].ToString();

//write to temp data table
DataRow DrawingDataRow = myDataTable.NewRow();
DrawingDataRow["JobNumber"] = NewJobcardNumber;
DrawingDataRow["DirectoryName"] = DirectoryName.Replace("\\" + OldJobcardNumber + "\\", "\\" + NewJobcardNumber + "\\");
DrawingDataRow["DrawingName"] = DrawingName;
DrawingDataRow["DateAdded"] = DateAdded;
DrawingDataRow["LastAccessedDate"] = LastAccessedDate;
DrawingDataRow["LastAccessedUserName"] = LastAccessedUserName;
DrawingDataRow["ClientName"] = ClientName;
DrawingDataRow["ContentType"] = ContentType;
Dr

Solution

So basically you make a copy of all rows for a specific job, replace the job number and append the changed row back to the database. Sounds like this should be doable in one loop with the help of ItemArray and no need for a temporary table:

nexusDBDataSet.Drawing_Table.Clear();
drawing_TableTableAdapter.Fill(nexusDBDataSet.Drawing_Table);
foreach (DataRow jobRow in nexusDBDataSet.Drawing_Table.Select(string.Format("JobNumber = '{0}'", OldJobcardNumber)))
{
    var newRow = nexusDBDataSet.Drawing_Table.NewRow();
    newRow.ItemArray = jobRow.ItemArray;

    string directoryName = jobRow["DirectoryName"].ToString();
    newRow["JobNumber"] = NewJobcardNumber;
    newRow["DirectoryName"] = DirectoryName.Replace("\\" + OldJobcardNumber + "\\", "\\" + NewJobcardNumber + "\\");

    nexusDBDataSet.Drawing_Table.Rows.Add(newRow);
}
// save table

Code Snippets

nexusDBDataSet.Drawing_Table.Clear();
drawing_TableTableAdapter.Fill(nexusDBDataSet.Drawing_Table);
foreach (DataRow jobRow in nexusDBDataSet.Drawing_Table.Select(string.Format("JobNumber = '{0}'", OldJobcardNumber)))
{
    var newRow = nexusDBDataSet.Drawing_Table.NewRow();
    newRow.ItemArray = jobRow.ItemArray;

    string directoryName = jobRow["DirectoryName"].ToString();
    newRow["JobNumber"] = NewJobcardNumber;
    newRow["DirectoryName"] = DirectoryName.Replace("\\" + OldJobcardNumber + "\\", "\\" + NewJobcardNumber + "\\");

    nexusDBDataSet.Drawing_Table.Rows.Add(newRow);
}
// save table

Context

StackExchange Code Review Q#62633, answer score: 2

Revisions (0)

No revisions yet.