dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: C# DataAdapter.Update not inserting after merge

  1. #1
    Join Date
    Aug 2007
    Posts
    1

    Question C# DataAdapter.Update not inserting after merge

    I'm writing a little app to get data from a CSV file generated everyday by a vendor and insert it into a Table in our SQL Server database. I can get the data fine, and I can easily merge it with another dataset I pull from the existing table (which is exactly the same except for an Identity Column as PK). ADO.NET v 2.0.

    When I call SqlDataAdapter.Update, no error is generated, but it makes no changes as well. I have one row present in the Database and can use DataAdapter.Update to delete it just fine. It just won't insert.

    Please Help! Code below:

    Code: ( text )


    using (OleDbConnection cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\MyPath\\;Extended Properties='text;HDR=Yes;FMT=Delimited'"))

    {
    string sql = "SELECT * from MyCSV.csv";
    OleDbDataAdapter csvRdr = new OleDbDataAdapter(sql, cn);
    DataSet ds = new DataSet();

    cn.Open();

    try
    {
    csvRdr.Fill(ds, "Table");
    cn.Close();
    using (SqlConnection cnn = new SqlConnection("Data Source=DB;Initial Catalog=XX;User ID=usr;Password=pwd"))
    {
    string sql_2 = "Select * from DBTable";
    SqlDataAdapter da = new SqlDataAdapter(sql_2, cnn);
    SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(da);
    DataSet ds2 = new DataSet();
    da.TableMappings.Add("DBTable", "Table");
    Console.WriteLine(sql_2);
    cnn.Open();
    da.Fill(ds2, "Table");
    ds2.Merge(ds.Tables["Table"]);
    ds2.AcceptChanges();
    //This works ds2.Tables["Table"].Rows[0].Delete();
    //Looks Fine Console.Write(ds2.GetXml());
    da.Update(ds2, "Table");
    cnn.Close();
    }
    }
    )



    Thanks.

    -Mike
    Last edited by mcasey0827; August 23rd, 2007 at 09:58 AM.

  2. #2
    Join Date
    Jan 2005
    Posts
    3

    Re: C# DataAdapter.Update not inserting after merge

    Hi,

    At the beginning you make changes in DataSet.

    In the next step you make "AcceptChanges" on your DataSet. This operation erases all logical changes in DataSet and there is nothing to do for SqlDataAdapter there.

    Afterall you make "Update" ...

    Try to remove "AcceptChanges" and all should be fine.

    Greetings.

  3. #3
    Join Date
    Dec 2007
    Location
    South Africa
    Posts
    263

    Re: C# DataAdapter.Update not inserting after merge

    mmhh your code is a little bit messy, but look at how i did the Operations that you want to do and look at how clean the code is

    http://www.codeproject.com/KB/cs/NTier.aspx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)