Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have 1 method for importing files from folder but i don't have all files in that folder the files are coming from ftp to that folder.and i have table with 1 field as primary key , when i run program files successfully imported in database which are present at the time of execution of program but when new files are coming into folder from server those files not imported in database. and at all interval error comes "Violation of primary key constraint" but i can't remove primary key otherwise duplicate files saved again and again.
Help me to solve this problem.


Here is my code
C#
namespace Module1 
{
 public partial class DeviceDetails : Form 
{
 //Timer initialises// System.Windows.Forms Timer t = new System.Windows.Forms Timer();

    public DeviceDetails()
    {
        InitializeComponent();
      //  ImportAllFilesOfFolder();
    }

    //Initialize data connection componants//
    SqlConnection con = new SqlConnection(----some conn string----);
    DataTable dt;
    SqlDataAdapter dataAdap;
    SqlCommandBuilder scmd;

    private void DeviceDetails_Load_1(object sender, EventArgs e)
    {
                  this.deviceDetailsTableAdapter.Fill(this.hospitalProjectDataSet.DeviceDetails);

        t.Interval = 100000;  // interval in millisecond
        t.Tick += new EventEventHandler(t_Tick);           
        t.Enabled = true;
        t.Start();
    }

     void t_Tick(object sender, EventArgs e)
    { 
        GetData("select from DeviceDetails.csv");
        ImportAllFilesOfFolder();         
    }

Here is import method

C#
public void ImportAllFilesOfFolder() { try { SqlConnection con = new SqlConnection(----con string----); string sourceDir = @"---folder path to import from---"; var IcsvFile = Directory.EnumerateFiles(sourceDir, "*.csv");

            foreach (string currentFile in IcsvFile)
            {                   
                    StreamReader sr = new StreamReader(currentFile);
                    string line = sr.ReadLine();
                    string[] value = line.Split(',');
                    DataTable dt = new DataTable();
                    DataRow row;

                    foreach (string dc in value)
                    {
                        dt.Columns.Add(new DataColumn(dc));
                    }

                    while (!sr.EndOfStream)
                    {
                        value = sr.ReadLine().Split(',');
                        if (value.Length == dt.Columns.Count)
                        {
                            row = dt.NewRow();
                            row.ItemArray = value;
                            dt.Rows.Add(row);
                        }
                    }

                    SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
                    bc.DestinationTableName = "DeviceDetails";
                    bc.BatchSize = dt.Rows.Count;
                    con.Open();
                    bc.WriteToServer(dt);
                    bc.Close();
                    con.Close();
                }
            }


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 21-Mar-15 1:35am
v6
Comments
Sergey Alexandrovich Kryukov 21-Mar-15 6:02am    
And what's the problem?
—SA
OriginalGriff 21-Mar-15 6:02am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
So stop trying to put the entire question in the subject line, and explain in detail in the body of your question what your problem is, what you have tried, and what help you need.
Becasue at the moment I have no idea what you are having problems with!
Use the "Improve question" widget to edit your question and provide better information.

1 solution

Please see my past answer: Polling Database with Timer[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900