Hi,PRA
I have created sample code which fullfill Requirement.
C#
private void button1_Click(object sender, EventArgs e)
{
string Constr = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(Constr))
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fileName = Path.GetFileName(openFileDialog1.FileName);
string filePath = openFileDialog1.FileName;
File.Copy(filePath, @"D:\Users\" + fileName);
string strQuery = "insert into FileNameTest (FileName) values(@FileName)";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.AddWithValue("@FileName", fileName);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
private void button2_Click(object sender, EventArgs e)
{
string Constr = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(Constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM FileNameTest", con))
{
DataTable dt = new DataTable();
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt);
for (int i = dt.Rows.Count - 1; i >= 0; i--)
{
DataRow dr = dt.Rows[i];
string[] filePaths = System.IO.Directory.GetFiles(@"D:\Users\");
foreach (string filePath in filePaths)
{
if (dr["FileName"].ToString() == Path.GetFileName(filePath))
{
//Logic Here...
}
}
}
}
}
}
}
I hope help you.