CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2011
    Posts
    2

    [RESOLVED] Sql database delete problem vs2010

    Hello, Im having problems with my sql database and deleting all the records that it contains.
    If I start the application and delete all records it works fine but if I add data to the database then delete I get concurrency violations.

    a typical use case that doesnt work
    start program,
    connect to device using bluetooth
    get all data from device and add to database
    user decides they want to delete all records (concurrency violation)

    use case that does work
    start program,
    connect to device using bluetooth
    user decides they want to delete all records (works)

    here is my delete button code

    System.Data.SqlClient.SqlCommandBuilder cb;
    cb = new System.Data.SqlClient.SqlCommandBuilder(da);
    for (int i = 0; i < ds1.Tables["THCdata"].Rows.Count; i++)
    {
    ds1.Tables["THCdata"].Rows[i].Delete();
    da.Update(ds1, "THCdata");
    }

    -------------------------------------------------------------------
    if im adding data here is code i use

    System.Data.SqlClient.SqlCommandBuilder cb;
    cb = new System.Data.SqlClient.SqlCommandBuilder(da);
    string[] datT = a.Split(',');
    if (datT.Length == 5 && int.Parse(datT[0]) != -100)
    {
    DataRow dRow = ds1.Tables["THCdata"].NewRow();
    dRow[1] = datT[0];
    dRow[2] = datT[1];
    dRow[3] = datT[2];
    dRow[5] = datT[4];
    if (int.Parse(datT[3]) > 2)
    {
    dRow[4] = 1;
    }
    else
    {
    dRow[4] = 0;
    }
    ds1.Tables["THCdata"].Rows.Add(dRow);
    da.Update(ds1, "THCdata");
    }

    -----------------------------------------------------------------

    here is code i use to get data from database and send to graphs

    clearHistgraphs();

    System.Data.SqlClient.SqlCommandBuilder cb
    cb = new System.Data.SqlClient.SqlCommandBuilder(da);
    DataRow dRow;
    DateTime T = new DateTime();
    double x = 0;

    for (int i = 0; i < ds1.Tables["THCdata"].Rows.Count; i++)
    {
    dRow = ds1.Tables["THCdata"].Rows[i];
    T = ConvertFromUnixTimestamp(Int64.Parse(dRow.ItemArray.GetValue(5).ToString()));
    x = (double)new XDate(T);
    count++;
    histlistt.Add(x, int.Parse(dRow.ItemArray.GetValue(1).ToString()));
    histlisth.Add(x, int.Parse(dRow.ItemArray.GetValue(2).ToString()));
    histlistc.Add(x, int.Parse(dRow.ItemArray.GetValue(3).ToString()));
    histlistl.Add(x, int.Parse(dRow.ItemArray.GetValue(4).ToString()));
    }
    da.Update(ds1, "THCdata");
    UpdateHistGraphs();

    we dont do databases at uni until next year so Im just using tutorials and posts from forums online, Im sure its my dataset.

    Regards

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Sql database delete problem vs2010

    If you want to delete all data from a table you should not be looping through deleting records one by one.

    You should use an SQL command with a Delete query. This allows you to delete one or more records at once even all records at once if that is your desire.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Dec 2011
    Posts
    2

    Re: Sql database delete problem vs2010

    Thanks, I have changed to using commands and the software is working as it should, I had followed tutorials online which use datasets but the commands seems so much easier, i made a command that just deletes all the data and that was that, thanks

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Sql database delete problem vs2010

    You're welcome.

    You should mark your thread resolved from the thread tools at the top of the first post.
    Always use [code][/code] tags when posting code.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured