question

WerVardyGestern-6547 avatar image
0 Votes"
WerVardyGestern-6547 asked AmeliaGu-msft edited

Using Sql Command in C# Visual Studio

Why does the SQL Command not Delete the Data from the Database, in the properties i have already set the sql command text on the Delete Comand. Is the sql Command used rightly?


public Form1()
{
InitializeComponent();
sqlConnection1.Open();
}

     private void recreateXML_Click(object sender, EventArgs e)
     {

         dataSet11.Clear();
         sqlDataAdapter1.Update(dataSet11);

         String filePath = "";
         openFileDialog1.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*";

         if (openFileDialog1.ShowDialog() == DialogResult.OK)
         {
             filePath = openFileDialog1.FileName;

             dataSet11.ReadXml(filePath);
             sqlCommand1.ExecuteNonQuery();
             sqlDataAdapter1.Update(dataSet11);
         }
     }

     private void updateButton_Click(object sender, EventArgs e)
     {
         sqlDataAdapter1.Update(dataSet11);
     }

     private void deleteButton_Click(object sender, EventArgs e)
     {
         foreach(DataGridViewRow row in dataGridView1.SelectedRows)
         {
             dataGridView1.Rows.Remove(row);
         }
         sqlDataAdapter1.Update(dataSet11);
     }
 }

}

sql-server-generaldotnet-csharpdotnet-ml-big-data
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What DELETE command? I can't see any.

0 Votes 0 ·
vb2ae avatar image
0 Votes"
vb2ae answered

I do not see in your code where you define the update and delete commands for the sql data adapter

You can use the SQLCommandBuilder class to create the update and delete commands. Of course you could also do it yourself

https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommandbuilder?view=dotnet-plat-ext-6.0

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SimpleSamples avatar image
0 Votes"
SimpleSamples answered

You are deleting the records in the DataGridView but not the DataSet bound to it. It has been a very long time since I have used a SqlDataAdapter but you need to use the command in the SqlDataAdapter.DeleteCommand Property.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.