Question

I asked a question before about how I can do a simple insert/update from a CSV file into a database. I was given some code that I have adopted here. The recordList is the parsed CSV file as a collection:

SqlDataAdapter dataAdpater = new SqlDataAdapter(
    "SELECT * FROM Cats WHERE UniqueCatName = @UniqueCatName", "data source=localhost;initial catalog=Kitties;integrated security=True;MultipleActiveResultSets=True;");

DataTable testTable = new DataTable();

  dataAdpater.Update(testTable);

foreach (var record in recordList)
{
    dataAdpater.SelectCommand.Parameters.AddWithValue("@UniqueCatName", record.UniqueCatName);

    int rowsAdded = dataAdpater.Fill(testTable);
    if (rowsAdded == 0)
    {
        testTable.Rows.Add(
            record.UniqueCatName,
            record.Forename,
            record.Surname
            );
    }
    else
    {

    }
}

dataAdpater.Update(testTable);

I'm kinda going in blind, I've looked at a ton of tutorials but I couldn't find one that clearly demonstrated how to use the SqlDataAdapter for both adding and updating.

From what I read, you have to specify an update command and an insert command? So I'm not -entirely- sure what's going on with the code above, but I guess it is able to do a simple insert without me giving extra instructions.

I couldn't figure out what to put in the 'else' bit though that the answerer to my previous question gave. I need to retrieve and update the particular row, but I don't know how to do that. Any ideas?

Was it helpful?

Solution

If anyone is interested, I have done it. This is the unrefactored version. It inserts if they don't exist, and updates existing records if they already do. I hope it helps someone out in the future:

SqlDataAdapter dataAdpater = new SqlDataAdapter(
    "SELECT * FROM Cats WHERE URN = @URN", "data source=...");

dataAdpater.InsertCommand = new SqlCommand("INSERT INTO Cats VALUES (@URN, @Forename, @Middlename, @Surname)", new SqlConnection("data source=..."));

dataAdpater.UpdateCommand = new SqlCommand("UPDATE Cats SET Forename=@Forename, Middlename=@Middlename, Surname=@Surname WHERE URN=@URN", new SqlConnection("data source=..."));

DataTable testTable = new DataTable();

dataAdpater.Update(testTable);

dataAdpater.SelectCommand.Parameters.Add("@URN", SqlDbType.NVarChar, 256, "URN");

dataAdpater.InsertCommand.Parameters.Add("@URN", SqlDbType.NVarChar, 256, "URN");
dataAdpater.InsertCommand.Parameters.Add("@Forename", SqlDbType.NVarChar, 256, "Forename");
dataAdpater.InsertCommand.Parameters.Add("@Middlename", SqlDbType.NVarChar, 256, "Middlename");
dataAdpater.InsertCommand.Parameters.Add("@Surname", SqlDbType.NVarChar, 256, "Surname");

dataAdpater.UpdateCommand.Parameters.Add("@URN", SqlDbType.NVarChar, 256, "URN");
dataAdpater.UpdateCommand.Parameters.Add("@Forename", SqlDbType.NVarChar, 256, "Forename");
dataAdpater.UpdateCommand.Parameters.Add("@Middlename", SqlDbType.NVarChar, 256, "Middlename");
dataAdpater.UpdateCommand.Parameters.Add("@Surname", SqlDbType.NVarChar, 256, "Surname");

foreach (var record in recordList)
{
    dataAdpater.SelectCommand.Parameters["@URN"].Value = record.URN;

    int rowsAdded = dataAdpater.Fill(testTable);
    if (rowsAdded == 0)
    {
        var newRow = testTable.NewRow();

        newRow["URN"] = record.URN;
        newRow["Forename"] = record.Forename;
        newRow["MiddleName"] = record.MiddleName;
        newRow["Surname"] = record.Surname;

        testTable.Rows.Add(newRow);
    }
    else
    {
        foreach (DataRow row in testTable.Rows)
        {
            if (row[1].ToString() == record.URN)
            {
                row["Forename"] = record.Forename;
                row["MiddleName"] = record.MiddleName;
                row["Surname"] = record.Surname;
            }
        }
    }
}

dataAdpater.Update(testTable);

OTHER TIPS

You could start off with something like this which will add all records from RecordList:

string connectionString = ....;
SqlDataAdapter  adapter = new SqlDataAdapter("Select * From Cats", connectionString);

// The command builder will generate the Add, Update and Delete commands
// based on the select command
SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(adapter);

DataTable testTable = new DataTable("Cats");
adapter.Fill(testTable);  // retrieve all existing rows

// Add each record from recordList
foreach (var record in recordList)
{
    // TODO - Handle duplicates
    testTable.Rows.Add(
        record.UniqueCatName,
        record.Forename,
        record.Surname
    );
}

adapter.Update(testTable);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top