Updating SQL Database from DataGridView in C#

后端 未结 2 1039
心在旅途
心在旅途 2021-01-27 00:27

There\'s a few tutorials out there on this, but I\'m assuming I must have implemented something wrong, because the code I followed from combined tutorials is not working as it s

相关标签:
2条回答
  • 2021-01-27 00:50

    Well, thanks to more Googling and combing through other questions here on this site, I found someone answered a similar question that made me realize I was in fact missing something simple. So in the event someone else runs into this issue, I thought I'd post the highly possible similar mistake I was making that was resulting in this issue.

    So, what I didn't realize, was that Visual Studio was making a temp version of my database in the \bin\debug folder. (If there's a way to turn that functionality off, I'd love to hear about it, because I think that's rather stupid) For whatever reason, if you open up in the Sever Explorer to find your .mdf file, manually adding or deleting information in that database is reflected in the program when run, because it looks for this file, makes a copy of it, and then works from there while you're running your application for testing purposes. The disappointing fact, is that (again, unless I missed where to keep this from happening) it doesn't transversely copy these changes you make to the temp version, over to the primary .mdf file you created in the first place.

    So, this is why I saw no changes, because I was looking at the primary database file where it was in fact not working from. Once I found out how to locate the temp version from within Visual Studio, and I queried that file database, I did in fact see the changes. For clarity, multiple versions of what I tried originally were in fact working. So, all of the solutions I tried above aside from the one where I mentioned it produced an error due to a lack of the SqlCommandBuilder call, in fact do update the temporary database.

    As a side note, coming from a Visual Studio, C#, SQL newbie perspective, I'm surprised with the extensive tutorials and information on SQL Databases and DataGridView, that this isn't noted in the process of tutorials. Even Youtube videos I watch didn't make this explicitly clear that when they "proved" the updates were happening, they didn't make it obvious they were checking the temp database, not the primary one.

    0 讨论(0)
  • 2021-01-27 01:12

    It looks to me like you're never setting a SQL update statement, only select statements.

    You'll need to add something in like:

    sda.UpdateCommand = "UPDATE TABLE SET ..."
    

    Or create a new dataAdapter/command to handle your update

    Once you have that in place, calling update on sda should work.

    Revised:

    If it were me, I would use SqlCommand for your update statement.

    private void UpButton_Click(object sender, EventArgs e)
    {
    
        try
        {
            using(con = new SqlConnection(Properties.Settings.Default.SchoolConnectionString))
            {
                con.Open();
                string sqlCommand = "Update (Table) set value=@Value where id=@ID";
                SqlCommand cmd = new SqlCommand(sqlCommand, con);
                cmd.Parameters.AddWithValue("@Value", updatedValue);
                cmd.Parameters.AddWithValue("@ID", idOfRowToUpdate);
                int rowsAffected = cmd.ExecuteNonQuery();
                if(rowsAffected == 1)
                {
                    MessageBox.Show("Information Updated", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                con.Close();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    

    Things to note:

    You'll need to get the values you're updating from your table and replace it for UpdatedValue and idOfRowToUpdate.

    You'll need to form the UPDATE command for your table.

    the "Using" brackets help properly dispose of your connection object.

    Technically with the using brackets con.Close() isn't required. I do it just in case.

    Revision #2:

    Ok, I did some more research into SqlDataAdapter and how it functions in regards to a datatable, it looks like this is much easier than I thought.

    You should be able to drop in this new code to the UpButton_Click method and have it work:

    try
        {
            DataTable newDT = dt.GetChanges();
            if(newDT != null)
            {
                sda.Update(newDT);
            }
            MessageBox.Show("Information Updated", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    

    According to This StackOverflow Article, this should be all you need to make the update.

    0 讨论(0)
提交回复
热议问题