[英]Deleting Multiple Rows from an Access Database
我想使用數組從訪問數據庫中刪除多個記錄。 該數組是從文件名動態加載的。
然后我查詢數據庫,看看數據庫列值是否與數組值匹配,如果不匹配則刪除它,如果匹配則不刪除它。
問題是:以下是刪除所有記錄的代碼,無論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(); }`
一個問題是您的代碼令人困惑。
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,實際上只有一次執行后才導致此查詢:
delete from table1 where name not in ('a.jpg', 'b.jpg', 'c.jpg')
但是當您動態加載數組時,您可能會得到此數組
a[0] = 'a.jpg';
a[1] = 'b.jpg';
a[1] = 'c.jpg';
這將在foreach中執行3次,導致以下3個查詢
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')
在第二個表之后,該表將為空。
您應該嘗試以下方法:
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();
動態創建名稱的原因是,這將導致以下查詢與您的測試匹配:
delete from table1 where name not in ('a.jpg', 'b.jpg', 'c.jpg')
動態填充數組的首選方法是使用列表,因為純數組的大小是固定的,任何更改都需要創建一個新數組。
您可以像數組一樣循環遍歷列表。
List<string> names = new List<string>();
//or user var keyword
var names = new List<string>();
然后,只需使用add方法添加元素,然后根據需要循環即可。
names.Add(filename);
然后進行串聯:
string allNames = "'" + String.Join("','", names.ToArray()) + "'";
您完成了。
或者你可以使用
string[] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.jpg");
string[] names = filePaths.ToList().ConvertAll(n => n.Substring(n.LastIndexOf(@"\") + 1)).ToArray();
發表我的評論作為答案
您的字符串沒有讀入4個條目,而是讀了一個
string names = " 'a.jpg', 'b.jpg','c.jpg','j.jpg' ";
它應該是
string[] names = { "a.jpg", "b.jpg","c.jpg","j.jpg" };
在您的每個人的計數為1之前,現在應該有4的計數與實際值
編輯:在這個解決方案中,我必須付出很多努力,但我必須承認,但是如果您想進行動態輸入,則可以執行以下操作:
string name = " 'a.jpg', 'b.jpg','c.jpg','j.jpg' ";
string[] names = name.Split(',').Select(x => x.Trim(' ').Trim('\'')).ToArray();
如果我得到更改,稍后會更新,因為修剪不是很好的atm
對於填充它,如果您希望它可枚舉,則示例可能類似於
IEnumerable<string> filelocations = Directory.GetFiles(sourceDirectory, "*", SearchOption.AllDirectories).Select(x => Path.GetFileName(x));
或用於字符串數組
string [] lok = Directory.GetFiles(sourceDirectory, "*", SearchOption.AllDirectories).Select(x => Path.GetFileName(x)).ToArray();
聽起來您未正確讀取文件或文件為空。 在對數據庫運行操作之前,應檢查數組以確保其不為空。 如果數組為空,則應刪除數據庫中的所有內容,因為沒有匹配項。
您需要像這樣定義數組:
string[] names = { "a.jpg", "b.jpg", "c.jpg", "j.jpg" };
您定義數組的方式它僅包含一個值:
" 'a.jpg', 'b.jpg','c.jpg','j.jpg' "
您注釋中的代碼將刪除所有與第一個文件都不匹配的文件。 那將是所有非“ a.jpg”文件。 下一次迭代將刪除所有與“ b.jpg”不匹配的文件,即“ a.jpg”。 這將導致一個空表。 編輯:當您手動執行數組聲明時,會生成一個良好的IN
子句,但是在獲取文件名列表時,不會生成相同的字符串。
您需要在數組上執行聯接以為where子句生成單個字符串,從而使where子句包含所有文件。 您的最終where子句應如下所示:
where name not in ('a.jpg','b.jpg','c.jpg','d.jpg')
現在您有:
where name not in ('a.jpg')
...下一次迭代
where name not in ('b.jpg')
另外,請注意IN
操作很昂貴,並且數組越長,查詢增長越快。
如果您不想創建靜態大小的數組,請嘗試使用列表
List<string> names = new List<string>();
names.Add("a.jpg");
names.Add("b.jpg");
names.Add("c.jpg");
foreach (string name in names)
{
OleDbCommand sqlcmd = new OleDbCommand("delete from table1
where name not in (" + name + ")",
sqlconnection);
sqlcmd.ExecuteNonQuery();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.