如何删除dataGridView中的整个数据库 [英] How to delete the whole database in dataGridView

查看:84
本文介绍了如何删除dataGridView中的整个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想删除整个数据库!因为当我导入excel文件时,它会自动将其上传到数据库。如果我要上传另一个excel文件,它会将它添加到原始文件,我不希望这样!所以...我如何删除原始数据!?! :)



代码:

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:

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?

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? :)

推荐答案

所以,基本上按照代码,我所理解的是,如果表中已经有一个具有相同id,name和date的记录想要删除旧的并从excel插入新的。



为了实现上述目的,你只需要在插入之前执行一个删除语句条件包括身份证,姓名和日期,或者您也可以使用excel中的新记录更新现有记录。





是我的根据您的要求理解?
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?


这篇关于如何删除dataGridView中的整个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆