Thursday, 12 September 2013

16_Sept_2013 - DCR

private void CreateEmailBody()
        {
            string Comments = string.Empty;
            string emailbody = string.Empty;
            int flag = 0;
            int sncount = Convert.ToInt32(Dts.Variables["SNCount"].Value.ToString());
            int notescount = Convert.ToInt32(Dts.Variables["NotesCount"].Value.ToString());
            int emailindicator = 0;

            int results = (sncount - notescount);
            if ((results == 0) && (sncount > 0) && (notescount > 0))//rows match
                emailindicator = 1;
            Dts.Variables["EmailIndicator"].Value = emailindicator;
            if (emailindicator == 0)
            {
                Dts.Variables["ErrorCounter"].Value = Convert.ToInt32(Dts.Variables["ErrorCounter"].Value.ToString()) + 1;
                if (sncount == 0)
                {
                    Comments = "Snapshot table has no rows.";
                    flag = 1;
                }
                if (@notescount == 0)
                {
                    if (flag == 1)
                    {
                        Comments = "Both Notes and Snapshot tables have no rows.";
                    }
                    else
                    {
                        Comments = "Notes table has no rows.";
                    }
                }
                if (results < 0 && notescount > 0 && sncount > 0)
                {
                    Comments = "Number of rows in Notes table exceeds rows in Snapshot table by <b>" + -(results) + "</b> rows.";
                }

                if (results > 0 && notescount > 0 && sncount > 0)
                {
                    Comments = "Number of rows in Snapshot table exceeds rows in Notes table by <b>" + results + "</b> rows.";
                }
                emailbody = "<p><b>Feedname:</b> " + Dts.Variables["Table"].Value.ToString() + "<br>" +
                          "<b>Package Run Time:</b> " + GetParsedDate(DateTime.Now) + "<br>" +
                          "<b>Error Message:</b> " + Comments + "<br><br></p>";
                Dts.Variables["EmailBody"].Value = Dts.Variables["EmailBody"].Value + emailbody;
            }
            else
            {
                /* *
                emailbody = "<p><b>Feedname:</b> " + Dts.Variables["Table"].Value.ToString() + "<br>" +
                          "<b>Package Run Time:</b> " + GetParsedDate(DateTime.Now) + "<br>" +
                          "<b>Status:</b> Success" + "<br>" +
                          "<b>Message:</b> " + " Insert completed sucessfully.<br><br></p>";
                Dts.Variables["EmailBody"].Value = Dts.Variables["EmailBody"].Value + emailbody;
                 * */
            }

        }

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;
            }
        }
        

14_Sept_2013






========
using System.IO;
using System.Data.SqlClient;

        public void Main()
        {
            // TODO: Add your code here
            try
            {
                string FileName = string.Empty;
                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;
            }
        }

Tuesday, 10 September 2013

Sept12_2013

if not exists ( select fileid from  FileDetails where FileName='G:\New folder\StataContribution_20130905.xlsx')
insert into FileDetails values('G:\New folder\StataContribution_20130905.xlsx','G:\New folder\StataContribution','Shyam',GETDATE())

--select fileid from FileDetails where FileName='G:\New folder\StataContribution_20130905.xlsx'
select * from FileDetails 

Thursday, 5 September 2013

=============
CREATE TABLE [dbo].[StateConrtibution](
 [LoanAmt] [int] NULL,
 [FileId] [int] NULL
) ON [PRIMARY]
==========
CREATE TABLE [dbo].[FileDetails](
[FileID] [int] IDENTITY(1,1) NOT NULL,
[FileName] [varchar](50) NOT NULL,
[FolderPath] [varchar](150) NOT NULL,
[CurrentUser] [varchar](50) NOT NULL,
[CurrentDate] [datetime] NOT NULL
) ON [PRIMARY]
===============
Variables:
Check Int32 0
FileId Int32 0
FileName String StataContribution_20130903.xlsx
FolderPath G:\New folder\

======================
Sript Task:
Read write variables: User::FileName User::FolderPath

public void Main()
        {
            // TODO: Add your code here
            string FileName = string.Empty;
            //Str st = new Str();
            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";

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

        }
====================
Execute SQL Task:
--------------------------
REsult Set: Single Row

if not exists ( select fileid from  FileDetails where FileName=?)
INSERT INTO [Test].[dbo].[FileDetails]
           ([FileName]
           ,[FolderPath]
           ,[CurrentUser]
           ,[CurrentDate])
     VALUES
           (?,?,?,?)

select fileid from FileDetails where FileName=?


ParameterMapping:
-------------------------
User::FileName Input varchar 0 -1
User::FileName Input varchar 1 -1
User::FolderPath Input varchar 2 -1
System::CreatorName Input varchar 3 -1
System::StartTime Input Date 4 -1
User::FileName Input varchar 5 -1


Result Set:
--------------
0 FieldID

======================
Execute SQL Task: (2)
--------------------------
REsult Set: Single Row

select count(*) FROM [Test].[dbo].[StateConrtibution]
where  [FileId]=?




ParameterMapping:
-------------------------
User::FileId Input Long 0 -1

Result Set:
--------------
0 User::Check

======================
Presedence Constraint:
-----------------------
Expression and Constraint
Success
@Check<=0

-Logical And


======================


DFT:
Excel Source:
Rt.Click on Connection Manager->Expressions-->ServerName as @[User::FolderPath] +@[User::FileName]-->ok
Derived Column ->
FieldID , add new column,,@[User::Field], int, Map..
===================

Tuesday, 3 September 2013

=============
CREATE TABLE [dbo].[StateConrtibution](
[LoanAmt] [int] NULL,
[FileId] [int] NULL
) ON [PRIMARY]
==========
CREATE TABLE [dbo].[FileDetails](
[FileID] [int] IDENTITY(1,1) NOT NULL,
[FileName] [varchar](50) NULL
) ON [PRIMARY]

GO
===============
Variables:
FileId Int32 0
FileName String G:\New folder\StataContribution_20130903.xlsx

======================
Sript Task:
Read write variables: User::FileName

public void Main()
        {
            // TODO: Add your code here
            string FileName = string.Empty;
           // "G:\New folder\StataContribution_20130903.xlsx"
            FileName = "G:\\New folder\\StataContribution_"+Convert.ToString(DateTime.Now.Year)+"0"+Convert.ToString(DateTime.Now.Month)+"0"+Convert.ToString(DateTime.Now.Day)+".xlsx";
            Dts.Variables["FileName"].Value = FileName;
            MessageBox.Show(Dts.Variables["FileName"].Value.ToString());
            Dts.TaskResult = (int)ScriptResults.Success;

        }
====================
Execute SQL Task:
--------------------------
REsult Set: Single Row
SQL Stmt: insert into FileDetails values(?)

select MAX(fileid)fileid from FileDetails

ParameterMapping:
-------------------------
User::FileName Input varchar 0 -1
Result Set:
--------------
0 FieldID

======================
DFT:
Excel Source:
Rt.Click on Connection Manager->Expressions-->ServerName as @[User::FileName]-->ok
Derived Column ->
FieldID , add new column,,@[User::Field], int, Map..
===================
if not exists ( select COUNT (1) from  FileDetails where FileName='Naresh.xls')
insert into FileDetails values('Naresh.xls')

select MAX(fileid)fileid from FileDetails
select * from FileDetails 
Hello..Test