检查文件是否存在并更新 table 列

Check if File exists and update the table column

我有一个 table [dbo].[Missing_MyFastor],其中包含以下信息。

sdukey                            srcDocPath                           docName            IsExists
44276482    \172.17.22.159\images33\h_drive03\lrg03\    LRG03-FA35880406_280A-1P.TIF    0
44276483    \172.17.22.159\images33\h_drive03\lrg03\    LRG03-FA35880429_280A-1P.TIF    0
44276484    \172.17.22.159\images33\h_drive03\lrg03\    LRG03-FA35896269_280A-1P.TIF    0
44276485    \172.17.22.159\images33\h_drive03\lrg03\    LRG03-FA35896271_280A-1P.TIF    0
44276486    \172.17.22.159\images33\h_drive03\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_drive03\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 查询中 returning 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 分配给每一行,除此之外我没有看到它有任何用途。您可以检查是否可以直接 return 来自查询的 table 数据,如下所示-

select sdukey, CAST(srcDocPath AS VARCHAR(1000))+ docName as Folder_FileName from  [dbo].[Missing_MyFastor]