DataAdapter 更新方法 - 它使用哪个连接?

发布于 2024-07-25 13:08:10 字数 604 浏览 12 评论 0原文

抱歉这个可能很愚蠢的问题。 因为我在互联网上没有找到任何关于它的信息,所以它可能是完全显而易见的,而我只是盲目地看到?!

我正在尝试通过 DataAdapter.Update(dataset) 从数据集中更新数据库中的表,

但是不可能设置 DA 应该使用的连接。

DA 在哪里知道如何连接到 DB? 或者我误解了数据适配器的概念?

我当前的代码是这样的:

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

我刚刚写了这个,并开始怀疑它是如何(或是否)工作的...到目前为止我还没有测试它,因为我必须编写一些其他代码才能正确测试它

谢谢ppl,StackOVerflow FTW!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慵挽 2024-08-01 13:08:10

SqlDataAdapter 需要接收一个 SqlCommand 对象,该对象有一个与其绑定的 SqlConnection 对象。 这几乎就是等级制度被打破的方式。

至于如何这样做,可以选择将它们传递到构造函数中,以及在构造后设置各种属性。

这是一篇包含示例的 msdn 文章选择、插入、更新和删除。

自由贸易协定:

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文