I have a table [dbo].[Missing_MyFastor] which has the information as below.
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
I have to write C# script to check if file exists and update the IsExists=1 if exists. Else ignore. while updating the where clause should be on sdukey. I should iterate on each row. I need help in fetching sdukey and file path from the DataTable at each iteration level.
Mycode:
#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;
}
}
}
You are now checking if a file exists in the path of the value of each column (of course neither of the files "44276482"
, "\\172.17.22.159\images33\h_drive\2003\lrg03\"
, "LRG03-FA35880406_280A-1P.TIF"
, or "0"
exists).
You need to extract the path and the file name column values from the row, combine them to a full file path and then check if the file at the full file path exists:
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.
}
}
You are not returning the sdukey
in your select query so change the select query to include it like below:
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
Once you have the resultset having the sdukey
you can utilize that in the update query and in the 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();
}
}
On side note:
In the select query you are using the cte
just to assign the unique id to each row, other than that I dont see any use of it. You can check if you can directly return the table data from the query something like below-
select sdukey, CAST(srcDocPath AS VARCHAR(1000))+ docName as Folder_FileName from [dbo].[Missing_MyFastor]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.