-1

I want to retrieve text files data from directory to particular database fields in sql. Here is a code:

try
{
    FolderBrowserDialog fBrowser = new FolderBrowserDialog();
    //create instance of folder browser to navigate to desired folder to compress files
    DialogResult result = fBrowser.ShowDialog();
    //process this if user clicks OK button
    if (result == DialogResult.OK)
    {
       //string strPath stores chosen path
       strPath = fBrowser.SelectedPath;
       //put that path in the textbox1
       txtSource.Text = strPath;
    }
    //set current directory to be the one you navigated to 
    //(this is also the folder that will store the compressed file)
    Directory.SetCurrentDirectory(strPath);
    //get contents of directory stored in "strPath"
    DirectoryInfo di = new DirectoryInfo(strPath);
    //create array that holds requested files from folder stored in "di" variable
    DirectoryInfo[] rgFiles = di.GetDirectories("*.*");
    //move through DirectoryInfo array and store in new array of fi
    foreach (DirectoryInfo fi in rgFiles)
    {
       checkedListBox1.Items.Add(fi.Name); //add folders located into listbox
    }
}

Here the directory contains no. of images and txt files, if we select the checklistbox then all the data from txt files should be added to particular sql database related to their datatypes. Can any one help me out

Rahul Gokani
  • 1,688
  • 5
  • 25
  • 43
mohammed
  • 11
  • 5

1 Answers1

0

To check if the directory has files, you can make use of a System.IO class called "Directory" and it's method "GetFiles". All you need to do is pass in the Path of the directory. It will return you a String Array of the files in that directory. Details can be found here:

http://msdn.microsoft.com/en-us/library/07wt70x2.aspx

Your best best is to create a Stored Procedure in your database that can handle the incoming request, and insert the data into the correct tables. Here's an introduction to Sql-Server stored procedures:

http://msdn.microsoft.com/en-us/library/aa174792(v=sql.80).aspx

Then you will need to create a connection to your database in your C# code:

http://msdn.microsoft.com/en-us/library/s4yys16a(v=vs.71).aspx

Finally you will need to called your stored procedure using the connection you've just created and passing the parameters from your C# code, which has been answered many times on StackOverflow:

How to execute a stored procedure within C# program

Community
  • 1
  • 1
Tom Bowen
  • 8,214
  • 4
  • 22
  • 42