[英]Check if File exists and update the table column
我有一個表 [dbo].[Missing_MyFastor],其中包含以下信息。
sdukey srcDocPath docName IsExists
44276482 \\172.17.22.159\images33\h_drive\2003\lrg03\ LRG03-FA35880406_280A-1P.TIF 0
44276483 \\172.17.22.159\images33\h_drive\2003\lrg03\ LRG03-FA35880429_280A-1P.TIF 0
44276484 \\172.17.22.159\images33\h_drive\2003\lrg03\ LRG03-FA35896269_280A-1P.TIF 0
44276485 \\172.17.22.159\images33\h_drive\2003\lrg03\ LRG03-FA35896271_280A-1P.TIF 0
44276486 \\172.17.22.159\images33\h_drive\2003\lrg03\ LRG03-FA35896276_280A-1P.TIF 0
我必須編寫 C# 腳本來檢查文件是否存在並更新 IsExists=1 如果存在。 否則忽略。 同時更新 where 子句應該在 sdukey 上。 我應該迭代每一行。 我需要幫助在每個迭代級別從 DataTable 獲取 sdukey 和文件路徑。
我的代碼:
#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
#endregion
namespace ST_241de8cb1bbf41e9bfbfa9800831fe10
{
public void Main()
{
SqlConnection con = new SqlConnection("Server=AZABCD001;Database=TRDDataMart;Trusted_Connection=True;");
SqlCommand count = new SqlCommand("select count(1) from [dbo].[Missing_MyFastor]", con);
SqlCommand query = new SqlCommand("with cte(Id,Folder_FileName) as (select row_number() Over(order by sdukey asc) RN,CAST(srcDocPath AS VARCHAR)+docName as DN from [dbo].[Missing_MyFastor]) select top 10 * from cte",con);
con.Open();
count.CommandTimeout = 0;
int x = (int)count.ExecuteScalar();
SqlDataAdapter da = new SqlDataAdapter(query);
DataTable dataTable = new DataTable();
da.Fill(dataTable);
foreach(DataRow row in dataTable.Rows)
{
for (int j = 0; j < dataTable.Columns.Count; j++)
{
Console.WriteLine(row[j].ToString() + " ");
string filepath = row[j].ToString();
if(File.Exists(filepath))
{
string q= "update [dbo].[Missing_MyFastor] set IsExists=1 WHERE sdukey="+ row[j].ToString();
SqlCommand update = new SqlCommand(q, con);
update.ExecuteNonQuery();
}
}
}
//int i = 0;
//while(i<=x)
//{
//}
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
您現在正在檢查每列值的路徑中是否存在文件(當然文件"44276482"
、 "\\172.17.22.159\images33\h_drive\2003\lrg03\"
、 "LRG03-FA35880406_280A-1P.TIF"
, 或"0"
存在)。
您需要從行中提取路徑和文件名列值,將它們組合成完整文件路徑,然后檢查完整文件路徑下的文件是否存在:
foreach (var row in dataTable.Rows)
{
string path = row["srcDocPath"].ToString();
string fileName = row["docName"].ToString();
string filePath = Path.Combine(path, fileName);
if (File.Exists(filePath))
{
string sduKey = row["sdukey"].ToString();
// Now you have the sduKey of the row with an existing file.
// Preferably store the keys in a list and update them all at once
// with a single command, instead of doing it separately in every iteration.
}
}
您沒有在 select 查詢中返回sdukey
,因此請更改 select 查詢以包含它,如下所示:
with cte(Id, sdukey, Folder_FileName) as (select row_number() Over(order by sdukey asc) RN, sdukey, CAST(srcDocPath AS VARCHAR(1000))+ docName as DN from [dbo].[Missing_MyFastor]) select top 10 * from cte
獲得具有sdukey
的結果集后,您可以在更新查詢和foreach
中使用它:
foreach (DataRow row in dataTable.Rows)
{
string path = row["Folder_FileName"].ToString();
if (File.Exists(path))
{
string sduKey = row["sdukey"].ToString();
string q = "update [dbo].[Missing_MyFastor] set IsExists=1 WHERE sdukey=" + sduKey.ToString();
SqlCommand update = new SqlCommand(q, con);
update.ExecuteNonQuery();
}
}
旁注:
在 select 查詢中,您使用cte
只是為每一行分配唯一的 id,除了我看不到它有任何用途。 您可以檢查是否可以直接從查詢中返回表數據,如下所示 -
select sdukey, CAST(srcDocPath AS VARCHAR(1000))+ docName as Folder_FileName from [dbo].[Missing_MyFastor]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.