简体   繁体   中英

Using sql tableadapters to update database C#

I am building an application for a group of friends and myself to use for DnD sessions. Part of the program involves taking all of the values that are entered for our characters, items, etc and storing them to a database. I have the database built, and am pulling from the database into the program, however I am unable to return data to the database. I have the data coming into a dataset, and all of my edits are affecting the dataset, but I cannot get anything to affect the actual source database tables.

Below I have the button that I intend to use to update items in the characters' packs. I have both dataadapter, and tableadapter methods included that I have tried.


 private void btnaddpack_Click(object sender, EventArgs e)
    {
        if (txtbxpack.Text != "")
        {
            /*connection.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "UPDATE Pack SET Item = (@ItemName)" + "WHERE Id = '" + this.lstpack.SelectedValue + "';";
            cmd.ExecuteNonQuery();
            cmd.Clone();*/

            string packitem = txtbxpack.Text;    //will take item from an textbox
            this.packTableAdapter.Insert(packitem);

            this.Validate();
            this.packBindingSource.EndEdit();
            this.packTableAdapter.Update(this.dnD_MachineDataSet.Pack);
        }


        PopulatePack();

Here is my populate code in case someone needs that:

private void PopulatePack()
    {
        using (connection = new SqlConnection(connectionString))    //this is all about opening the connection to the sqldatabase, normally it would need to be closed, but this uses idisposable, so it will close itself
        using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Pack", connection))
        {


            DataTable packtable = new DataTable();
            adapter.Fill(packtable);

            lstpack.DataSource = packtable;
            lstpack.DisplayMember = "Item";
            lstpack.ValueMember = "Id";

        }


    }

As mentioned above, all of the changes are appearing whenever I re-populate the listboxes that draw upon the dataset, hence why this is an issue of trying to get that data back into the source database. I will make the obligatory "I'm relatively new to using databases" statement as it will do no good to pretend that I am an expert.

Thanks.

In the commented code, you would need to do the following:

  1. assign the connection object to the SqlCommand object's Connection property
  2. pass the item name to your @ItemName parameter
  3. assign a parameter value to the 'Id' column in the WHERE clause
  4. remove, 'cmd.Clone();', and replace with, 'connection.Close();'

Here is what the code should look like:

connection.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandText = "UPDATE Pack SET Item = (@ItemName) WHERE Id = @ID;";
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ItemName", txtbxpack.Text);
cmd.Parameters.AddWithValue("@ID", this.lstpack.SelectedValue);
cmd.ExecuteNonQuery();
connection.Close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM