从数据库中删除所有记录 [英] Delete All Record From Database

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

问题描述

你好

删除数据库中的所有记录,我使用此代码

hello

to delete all record in the database I use this code

public static void RemoveOnline()
       {
           try
           {

               SqlConnection connection = new SqlConnection();
               connection.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename='D:\New Code\WinDB.mdf';Connect Timeout=30;User Instance=True;Integrated Security=SSPI";
               connection.Open();


               SqlCommand command1 = new SqlCommand("DELETE FROM Online  ", connection);

               command1.ExecuteNonQuery();




               connection.Close();


           }
           catch (SqlException e)
           {
               MessageBox.Show(e.Message);
           }
       }



发生遗w时我会调用先前的代码
如下



I call previous code when close widow is happened
as follow

private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
        {
            StopServer();
            ConnectToDataBase.RemoveOnline();
        }




但它不起作用
并删除任何内容
怎么了? :S

感谢




but its dose not work
and delete nothing
what is the wrong ? :S

thanks

推荐答案

您的代码一目了然.当您调用ExecuteNonQuery时,请将返回值放入变量中.因此,将您的代码修改为
Your code looks valid at a glance. When you call the ExecuteNonQuery, take the return value into a variable. So modify your code to
...
int rowsaffected = command1.ExecuteNonQuery();
...


现在使用调试器,检查rowsaffected的值是什么.它应该是行的总数.

如果是这样,那么您实际上正在删除所有行,因此在这种情况下的下一个问题是,您是否会将mdf文件作为应用程序的一部分复制到目录D:\New Code\中?例如,mdf文件是项目的一部分,并且定义为在编译应用程序时复制到输出目录吗?如果是这种情况,那么实际上每次编译时都会重置"数据库,这可能就是修改丢失"的原因.
另请注意Mark Nischalke的回答.最好使用TRUNCATE,但在使用DELETE 语句测试了实际删除了多少行之后,请进行修改.


Now using debugger, check what is the value of rowsaffected. It should be the total amount of rows.

If that''s true then you''re actually deleting all of the rows so the next question in that case would be, do you copy the mdf file as part of your application to the directory D:\New Code\? For example is the mdf file part of your project and defined to be copied to the output directory when you compile the app? If that is the case, then you actually ''reset'' the database every time you compile and that could be the reason why the modification is ''lost''.

Also note the answer from Mark Nischalke. It''s better to use TRUNCATE, but make that modification after you have tested how many rows are actually deleted using the DELETE statement.


命令
DELETE FROM Online


是正确的,但我更喜欢使用截断 [


is correct, but i prefer to use TRUNCATE[^] (see my comment to Mark Nischalke anser).

In my opinion, the problem is in the below procedure:

private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
        {
            StopServer();
            ConnectToDataBase.RemoveOnline();
        }


停止服务器时,如何连接到服务器并删除数据?


When you stop server, how do you want to connect to it and delete data?


最好使用
It would be better to use TRUNCATE[^]. It is faster, uses less resources and is the recommended practice for removing all rows from a table.


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

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