Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, basically i want to delete the whole database! Because when i import an excel file it automatically uploads it to the database. If i were to upload another excel file it will add it to the original one and i don't want that! so... how do i delete the original data!?! :)

code:
C#
private void button2_Click(object sender, EventArgs e)
 {
     const string con = "Data Source = dqq5ndqef2.database.windows.net; Initial Catalog = Login; Integrated Security = False; User ID = richardjacobs97; Password = ; Connect Timeout = 15; Encrypt = False; TrustServerCertificate = False; ApplicationIntent = ReadWrite; MultiSubnetFailover = False";
     using (SqlConnection connection = new SqlConnection(con))
     {
         connection.Open();

         using (SqlTransaction transaction = connection.BeginTransaction())
         using (SqlCommand cmd = new SqlCommand("INSERT INTO StaffRota (Id, Name, Date) Values (@Id, @Name, @Date)", connection, transaction))
         {
             cmd.CommandType = CommandType.Text;

             for (int i = 0; i < dataGridView1.Rows.Count; i++)
             {
                 cmd.Parameters.Clear();

                 cmd.Parameters.AddWithValue("@Id", dataGridView1.Rows[i].Cells["Id"].Value);
                 cmd.Parameters.AddWithValue("@Name", dataGridView1.Rows[i].Cells["Name"].Value);
                 cmd.Parameters.AddWithValue("@Date", dataGridView1.Rows[i].Cells["Date"].Value);

                 cmd.ExecuteNonQuery();
             }
             transaction.Commit();
         }

         using (SqlCommand cmd = new SqlCommand("SELECT Id, Name, Date FROM StaffRota", connection))
         {
             DataTable dt = new DataTable();
             SqlDataAdapter sda = new SqlDataAdapter(cmd);
             sda.Fill(dt);
             dataGridView1.DataSource = dt;

         }
     }
 }


would the delete code be like this?

C#
private void button3_Click(object sender, EventArgs e)
{
    const string con = "Data Source = dqq5ndqef2.database.windows.net; Initial Catalog = Login; Integrated Security = False; User ID = richardjacobs97; Password = ; Connect Timeout = 15; Encrypt = False; TrustServerCertificate = False; ApplicationIntent = ReadWrite; MultiSubnetFailover = False";
    using (SqlConnection connection = new SqlConnection(con))
    {
        connection.Open();
        using (SqlCommand cmd = new SqlCommand("Select Id, Name, Date From StaffRota Values (@Id, @Name, @Date)", connection))
        {
            // delete code
            }
        }
    }
}


can anyone help? :)
Posted
Updated 23-Nov-15 10:43am
v2
Comments
[no name] 23-Nov-15 23:49pm    
what you have tired so far. i want to lit bit clear details of your requirements
F-ES Sitecore 24-Nov-15 5:18am    
The sql is just "delete from [TableName]"

Things can get trickier if you have tables with non-cascading delete relationships, but you just need to make sure you delete the data from the child tables before the parent tables.
Sums Mohs Eds 25-Nov-15 7:48am    
So, I would like to know following details. If at all you are deleting or updating on what basis or depending on what columns you would do that. I mean the where condition will have which columns?

1 solution

So, Basically as per the code, what i have understood is that if there is already a record in the table with same id, name and date you wanted to delete the old one and insert the new from the excel.

In order to achieve the above, you just have to execute a delete statement before the insert with where condition that includes id, name and date or you can also update the existing records with the new one in the excel.


Is my understanding as per your requirement?
 
Share this answer
 
Comments
Member 12161319 25-Nov-15 7:16am    
yes but i need to know the code and how i would do it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900