难以找到一个好的SqlDataAdapter教程/让我的代码工作

时间:2014-01-21 10:57:43

标签: c# sqldataadapter

之前我问了一个关于如何从CSV文件到数据库进行简单插入/更新的问题。我得到了一些我在这里采用的代码。 recordList是已解析的CSV文件,作为集合:

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);

我有点盲目,我看了很多教程,但我找不到一个清楚地演示了如何使用SqlDataAdapter进行添加和更新。

根据我的阅读,您必须指定更新命令和插入命令?所以我不能 - 确定上面代码发生了什么,但我想它可以做一个简单的插入,而不需要我提供额外的指令。

我无法弄清楚在“其他”位置放入什么,尽管我前一个问题的答案给了。我需要检索并更新特定的行,但我不知道该怎么做。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

如果有人有兴趣,我已经做到了。这是无意义的版本。如果它们不存在则插入,如果已经存在,则更新现有记录。我希望将来帮助某人:

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);

答案 1 :(得分:0)

你可以从这样的东西开始,它将添加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);