从Access数据库中删除多行 [英] Deleting Multiple Rows from an Access Database

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

问题描述

我要使用数组访问,从数据库中删除多条记录。
该阵列从文件的名称动态加载。

然后我查询数据库,看看数据库列值与数组值匹配,如果没有然后将其删除,如果匹配,则不要删除它。

问题是:
以下是code,它无论在删除的条件,其中的所有记录。

  =数组Directory.GetFiles(sdira,*,SearchOption.AllDirectories)。选择(X => Path.GetFileName(X))ToArray的()。
    fnames.AddRange(阵列);    在这里我也使用for循环,但也没有帮助我走出:(像(INT U = 0; U< arrays.length;ü++){OleDbCommand的SQLCMD =新的OleDbCommand(从表1中删除其中名称不在( '+阵列[U] +'),SqlConnection的);
   我使用这个当前的foreach(数组中的字符串名称)
   {
       OleDbCommand的SQLCMD =新的OleDbCommand(从表1中删除其中名称不是('+姓名+'),SqlConnection的);
       sqlcmd.ExecuteNonQuery(); }`


解决方案

一个问题是,您的code是混乱的。

 的String [] A = {'A.JPG','B.JPG','C.JPG}

首先,你得双在开始的时候,应该只有一种。

 的String [] A = {'A.JPG','B.JPG','C.JPG}

那么这创造了一个字符串数组的元素,

  A [0] ='A.JPG','B.JPG','C.JPG';

然后你做一个foreach在这里面natuarly ONY执行一次导致此查询:

 从表1中删除其中名称不是('A.JPG','B.JPG','C.JPG')

但是,当你加载动态数组,你可能得到这个数组

  A [0] ='A.JPG';
一个[1] ='B.JPG';
一个[1] ='C.JPG';

,这将在导致以下3查询在foreach执行3次

 其中name不是('A.JPG')从表1中删除
从表1中删除其中名称不是('B.JPG')
从表1中删除其中名称不是('C.JPG')

在第二个表是空的。

您应该改为尝试这个办法:

 的String [] =名称{A.JPG,B.JPG,C.JPG,j.jpg};
字符串allNames ='+的string.join(,,名称)+';OleDbCommand的SQLCMD =新的OleDbCommand(从表1中删除其中名称不在(+ allNames +)的SqlConnection);
sqlcmd.ExecuteNonQuery();

如果是动态创建ofcause名,这将导致下面的查询匹配测试:

 从表1中删除其中名称不是('A.JPG','B.JPG','C.JPG')

我的preferred的方式来动态填充数组是使用一个列表,而不是作为一个纯粹的数组的大小是固定的任何变化需要创建一个新的数组。

您可以遍历一个列表,eacy作为一个数组。

 列表<串GT;名称=新的List<串GT;();
//或用户var关键字
VAR名=新的List<串GT;();

然后,只需使用Add方法来添加元素,这个循环需要。

  names.Add(文件名);

那么对于串联:

 字符串allNames ='+的string.join(,,names.ToArray())+';

和你做。

或者你可以使用

 的String []文件路径= Directory.GetFiles(@C:\\ MYDIR \\,* .JPG);
字符串[] =名称filePaths.ToList()ConvertAll(N => n.Substring(n.LastIndexOf(@\\)+ 1))。ToArray的();

I want to delete multiple records from access database using array. The array is loaded dynamically from file names.

Then i query the database, and see if the database column values are matching with the array values, if not then delete it, if matches then do not delete it.

the problem is that: Following is the code that deletes all records irrespective of the where in Condition.

arrays = Directory.GetFiles(sdira, "*", SearchOption.AllDirectories).Select(x => Path.GetFileName(x)).ToArray();
    fnames.AddRange(arrays); 

    here I have use also for loop but that also didnt help me out :( like for(int u = 0; u < arrays.length; u++) { oledbcommand sqlcmd = new oledbcommand ("delete from table1 where name not in ("'+arrays[u]+"')",sqlconnection);
   I am using this one currently foreach(string name in arrays)
   {
       OleDbCommand sqlcmd = new OleDbCommand("delete from table1 where name not in ('" + name + "')", sqlconnection);
       sqlcmd.ExecuteNonQuery();                                  }`

解决方案

One problem is that your code is confusing.

string [] a = {"" 'a.jpg', 'b.jpg', 'c.jpg' "}

First, you have double " in the beginning,should only be one.

string [] a = {" 'a.jpg', 'b.jpg', 'c.jpg' "}

Then this created a string array with one element,

a[0] = "'a.jpg', 'b.jpg', 'c.jpg'";

Then you do a foreach on this which natuarly ony executes once resulting in this query:

delete from table1     where name not in ('a.jpg', 'b.jpg', 'c.jpg')

But when you load the array dynamically you probably get this array

a[0] = 'a.jpg';
a[1] = 'b.jpg';
a[1] = 'c.jpg';

which will execute 3 times in the foreach resulting in the following 3 queries

delete from table1     where name not in ('a.jpg')
delete from table1     where name not in ('b.jpg')
delete from table1     where name not in ('c.jpg')

After the second one the table will be empty.

You should try this instead:

string[] names = { "a.jpg", "b.jpg","c.jpg","j.jpg" };
string allNames = "'" + String.Join("','", names) + "'";

OleDbCommand sqlcmd = new OleDbCommand("delete from table1  where name not in (" + allNames + ")", sqlconnection); 
sqlcmd.ExecuteNonQuery(); 

Where names is created dynamically ofcause and this will result in the following query matching your test:

delete from table1     where name not in ('a.jpg', 'b.jpg', 'c.jpg')

My preferred way to dynamically fill an array is to use a list instead as a pure array is fixed in size and any change needs to create a new array.

You can loop over a list as eacy as an array.

List<string> names = new List<string>();
//or user var keyword
var names = new List<string>();

Then just use add method to add elements, loop this as needed.

names.Add(filename);

Then for the concatenation:

string allNames = "'" + String.Join("','", names.ToArray()) + "'";

And you are done.

Or you could use

string[] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.jpg");
string[] names = filePaths.ToList().ConvertAll(n => n.Substring(n.LastIndexOf(@"\") + 1)).ToArray();

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

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