Thursday, 12 September 2013

15_Sept_2013

For Each Loop:
======================





-------------------------------------
using System.IO;
using System.Data.SqlClient;
----------------------------------------------

public void Main()
        {
            // TODO: Add your code here
            try
            {
                string FileName = Dts.Variables["FileName"].Value.ToString();
                //string mm = "0" + Convert.ToString(DateTime.Now.Month);
                //mm = mm.Substring(mm.Length - 2);
                //string dd = "0" + Convert.ToString(DateTime.Now.Day);
                //dd = dd.Substring(dd.Length - 2);
                //FileName = "StataContribution_" + mm + dd + Convert.ToString(DateTime.Now.Year) + ".xlsx";
                if (File.Exists(Dts.Variables["FolderPath"].Value + FileName))
                {
                    Dts.Variables["FileCheck"].Value = 1;
                    SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Test;Data Source=.");
                    SqlDataAdapter da = new SqlDataAdapter("select * from FileDetails where FileName='" + FileName + "'", con);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    string FileId = "";
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        //SqlCommand cmd = new SqlCommand("select distinct FileId from FileDetails where FileName='" + FileName + "'", con);
                        //if (con.State == ConnectionState.Closed)
                        //{
                        //    con.Open();
                        //    FileId = cmd.ExecuteScalar().ToString();
                        //    Dts.Variables["FileId"].Value = FileId;
                        //    con.Close();
                        //}
                        Dts.Variables["FileId"].Value = 0;
                    }
                    else
                    {
                        SqlCommand cmd = new SqlCommand("INSERT INTO [Test].[dbo].[FileDetails]([FileName],[FolderPath] ,[CurrentUser],[CurrentDate])VALUES('" + FileName + "','" + Dts.Variables["FolderPath"].Value + "','" + Dts.Variables["System::UserName"].Value.ToString() + "',cast(getdate() as smalldatetime))", con);
                        if (con.State == ConnectionState.Closed)
                        {
                            con.Open();
                           int i= cmd.ExecuteNonQuery();
                            con.Close();
                            if (i > 0)
                            {
                                SqlCommand cmdc = new SqlCommand("select distinct FileId from FileDetails where FileName='" + FileName + "'", con);
                                if (con.State == ConnectionState.Closed)
                                {
                                    con.Open();
                                    FileId = cmdc.ExecuteScalar().ToString();
                                    Dts.Variables["FileId"].Value = int.Parse(FileId);
                                    con.Close();
                                }
                            }
                        }
                    }

                }
                else
                {
                    Dts.Variables["FileCheck"].Value = 0;
                    //Dts.TaskResult = (int)ScriptResults.Failure;

                }
                //Dts.Variables["FileName"].Value = FileName;

                //MessageBox.Show(Dts.Variables["FileName"].Value.ToString());
             
                Dts.TaskResult = (int)ScriptResults.Success;
            }
            catch
            {

                Dts.TaskResult = (int)ScriptResults.Failure;
            }
        }
        

No comments:

Post a Comment