In the code-behind (C#) of an aspx page the following
code retrieves all the files in a specified directory
and displays them in a dropdownlist. This portion of code works correctly:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs
e)
{
}
protected void Button1_Click(object sender,
EventArgs e)
{
DirectoryInfo di = new DirectoryInfo(@"C:\Test");
foreach (FileInfo f in di.GetFiles())
{
DropDownList1.Items.Add(f.Name);
}
}
}
I need some assistance with the following task:
Checking each filename in the directory to see
whether it exists in the SQL Server table tbl_ImportFileNames. If "f.name" does not exist in tbl_ImportFileNames.ImportFileName,
then I need to insert it into the table.
For example, if excelImport2003.xls exists as a filename in my directory, but not in the SQL Server table, I need to insert the filename into the SQL Server table.
This is the T-SQL (most likely needs correction) I have created in SQL Server ...
DECLARE @ImportFileName varchar(50)
set @ImportFileName = '??? f.name';
INSERT INTO tbl_ImportFileNames (ImportFileName)
SELECT ImportFileName
FROM tbl_ImportFileNames
WHERE NOT EXISTS (SELECT ImportFileName FROM
tbl_ImportFileNames
WHERE @ImportFileName =
tbl_ImportFileNames.ImportFileName);
How do I incorporate the query above into my aspx C# code-
behind? I would so appreciate any assistance!