Question

Sorry for the probably stupid question. Since I found nothing about it on the internets, its probably totally obvious and I'm just to blind to see?!

I'm trying to update a table in a database from a dataset via DataAdapter.Update(dataset)

But there is no possiblity to set the connection, the DA should use.

Where does the DA know how to connect to the DB? Or do I misunderstand the concept of the dataadapter?

my current code is like:

protected DataSet UpdateDataSet(DataSet ds)
{
   DataSet dsChanges = new DataSet();
   SqlDataAdapter da = new SqlDataAdapter();

   dsChanges = ds.GetChanges();

   //Update DataSet
   da.Update(dsChanges);

   ds.Merge(dsChanges);
   return ds;
}

I just wrote this and became suspicious how (or if) it works... I havent tested it so far, since I gotta write some other code before I can test it properly

Thank you ppl, StackOVerflow FTW!

Was it helpful?

Solution

A SqlDataAdapter needs to take in a SqlCommand object, which has a SqlConnection object tied to it. That's pretty much how the hierarchy breaks down.

As for how you go about doing that, there are options for passing them into the constructor, as well as setting the various properties after construction.

Here's an msdn article with examples of selecting, inserting, updating, and deleting.

FTA:

public static SqlDataAdapter CreateCustomerAdapter(
    SqlConnection connection)
{
    SqlDataAdapter adapter = new SqlDataAdapter();

    // Create the SelectCommand.
    SqlCommand command = new SqlCommand("SELECT * FROM Customers " +
        "WHERE Country = @Country AND City = @City", connection);

    // Add the parameters for the SelectCommand.
    command.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
    command.Parameters.Add("@City", SqlDbType.NVarChar, 15);

    adapter.SelectCommand = command;

    // Create the InsertCommand.
    command = new SqlCommand(
        "INSERT INTO Customers (CustomerID, CompanyName) " +
        "VALUES (@CustomerID, @CompanyName)", connection);

    // Add the parameters for the InsertCommand.
    command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
    command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 
        40, "CompanyName");

    adapter.InsertCommand = command;

    // Create the UpdateCommand.
    command = new SqlCommand(
        "UPDATE Customers SET CustomerID = @CustomerID, " + 
        "CompanyName = @CompanyName " +
        "WHERE CustomerID = @oldCustomerID", connection);

    // Add the parameters for the UpdateCommand.
    command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
    command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 
        40, "CompanyName");
    SqlParameter parameter = command.Parameters.Add(
        "@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
    parameter.SourceVersion = DataRowVersion.Original;

    adapter.UpdateCommand = command;

    // Create the DeleteCommand.
    command = new SqlCommand(
        "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);

    // Add the parameters for the DeleteCommand.
    parameter = command.Parameters.Add(
        "@CustomerID", SqlDbType.NChar, 5, "CustomerID");
    parameter.SourceVersion = DataRowVersion.Original;

    adapter.DeleteCommand = command;

    return adapter;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top