Skip to content
This repository has been archived by the owner on Sep 19, 2019. It is now read-only.

Release Tntp.CodingExercise #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Tntp.CodingExercise.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.21005.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tntp.CodingExercise", "Tntp.CodingExercise\Tntp.CodingExercise.csproj", "{8558773B-0ACD-4608-9750-4A883FA3DCC2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8558773B-0ACD-4608-9750-4A883FA3DCC2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8558773B-0ACD-4608-9750-4A883FA3DCC2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8558773B-0ACD-4608-9750-4A883FA3DCC2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8558773B-0ACD-4608-9750-4A883FA3DCC2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Binary file added Tntp.CodingExercise.v12.suo
Binary file not shown.
46 changes: 46 additions & 0 deletions Tntp.CodingExercise/App_Start/BundleConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;
using System.Web.UI;

namespace Tntp.CodingExercise
{
public class BundleConfig
{
// For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkID=303951
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/WebFormsJs").Include(
"~/Scripts/WebForms/WebForms.js",
"~/Scripts/WebForms/WebUIValidation.js",
"~/Scripts/WebForms/MenuStandards.js",
"~/Scripts/WebForms/Focus.js",
"~/Scripts/WebForms/GridView.js",
"~/Scripts/WebForms/DetailsView.js",
"~/Scripts/WebForms/TreeView.js",
"~/Scripts/WebForms/WebParts.js"));

// Order is very important for these files to work, they have explicit dependencies
bundles.Add(new ScriptBundle("~/bundles/MsAjaxJs").Include(
"~/Scripts/WebForms/MsAjax/MicrosoftAjax.js",
"~/Scripts/WebForms/MsAjax/MicrosoftAjaxApplicationServices.js",
"~/Scripts/WebForms/MsAjax/MicrosoftAjaxTimer.js",
"~/Scripts/WebForms/MsAjax/MicrosoftAjaxWebForms.js"));

// Use the Development version of Modernizr to develop with and learn from. Then, when you’re
// ready for production, use the build tool at http://modernizr.com to pick only the tests you need
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/modernizr-*"));

ScriptManager.ScriptResourceMapping.AddDefinition(
"respond",
new ScriptResourceDefinition
{
Path = "~/Scripts/respond.min.js",
DebugPath = "~/Scripts/respond.js",
});
}
}
}
18 changes: 18 additions & 0 deletions Tntp.CodingExercise/App_Start/RouteConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Routing;
using Microsoft.AspNet.FriendlyUrls;

namespace Tntp.CodingExercise
{
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
}
}
}
7 changes: 7 additions & 0 deletions Tntp.CodingExercise/Bundle.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<bundles version="1.0">
<styleBundle path="~/Content/css">
<include path="~/Content/bootstrap.css" />
<include path="~/Content/Site.css" />
</styleBundle>
</bundles>
187 changes: 187 additions & 0 deletions Tntp.CodingExercise/Classes/WriteToSQL.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace Tntp.CodingExercise.Classes
{
public class WriteToSQL
{
public static string _ConnectionString;

public static int WriteToSql(DataSet dSet)
{
_ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["TNTP"].ConnectionString;

//Using "Upsert Method" to insert only new entries reference: http://www.databasejournal.com/features/mssql/article.php/3739131/UPSERT-Functionality-in-SQL-Server-2008.htm

#region temp tables string
//Make temp tables in sql server that matches with production tables
string tmp = " CREATE TABLE #developers ([ID] [nvarchar](256) NOT NULL, " +
" [FirstName] [nvarchar](100) NULL, " +
" [aka] [nvarchar](100) NULL, " +
" [Title] [nvarchar](100) NULL, " +
" [LastName] [nvarchar](100) NULL, " +
" [BirthDateTime] [datetime] NULL, " +
" [DeathDateTime] [datetime] NULL); " +

" CREATE TABLE #awards ([ID] [nvarchar](256) NOT NULL, " +
" [FirstName] [nvarchar](100) NULL, " +
" [LastName] [nvarchar](100) NULL, " +
" [BirthDateTime] [datetime] NULL, " +
" [AwardName] [nvarchar](512) NULL, " +
" [Year] [int] NULL, " +
" [GivenBy] [nvarchar](512) NULL); " +

" CREATE TABLE #contribs ([ID] [nvarchar](256) NOT NULL, " +
" [FirstName] [nvarchar](100) NULL, " +
" [LastName] [nvarchar](100) NULL, " +
" [BirthDateTime] [datetime] NULL, " +
" [ContribName] [nvarchar](100) NULL) ";
#endregion

#region inserting into SQL
using (SqlConnection conn = new SqlConnection(_ConnectionString))
{
conn.Open();

//Execute the command to make a temp table
SqlCommand cmd = new SqlCommand(tmp, conn);
try
{
#region creating temp tables and SqlBulkCopy DataTable into temp tables
cmd.ExecuteNonQuery();

//BulkCopy the data in the DataTable to the temp table
using (SqlBulkCopy bulkdev = new SqlBulkCopy(conn))
{
bulkdev.DestinationTableName = "#developers";
bulkdev.WriteToServer(dSet.Tables["Developers"]);
}

using (SqlBulkCopy bulkawa = new SqlBulkCopy(conn))
{
bulkawa.DestinationTableName = "#awards";
bulkawa.WriteToServer(dSet.Tables["Awards"]);
}

using (SqlBulkCopy bulkcon = new SqlBulkCopy(conn))
{
bulkcon.DestinationTableName = "#contribs";
bulkcon.WriteToServer(dSet.Tables["Contribs"]);
}
#endregion

#region comparing temp tables with production table and insert if not exists and scope new row count
string mergeSql = " MERGE INTO Developers AS Target " +
" USING #developers AS Source " +
" ON " +
" Target.ID=Source.ID " +
" WHEN NOT MATCHED THEN " +
" INSERT (ID, FirstName, aka, Title, LastName, BirthDateTime, DeathDateTime) " +
" VALUES (Source.ID, Source.FirstName, Source.aka, Source.Title, Source.LastName, Source.BirthDateTime, " +
" Source.DeathDateTime);" +
" SELECT @Rows_dev=@@ROWCOUNT; " +

" MERGE INTO Awards AS Target " +
" USING #awards AS Source " +
" ON " +
" Target.ID=Source.ID " +
" WHEN NOT MATCHED THEN " +
" INSERT (ID, FirstName, LastName, BirthDateTime, AwardName, Year, GivenBy) " +
" VALUES (Source.ID, Source.FirstName, Source.LastName, Source.BirthDateTime, " +
" Source.AwardName, Source.Year, Source.GivenBy);" +
" SELECT @Rows_award=@@ROWCOUNT; " +

" MERGE INTO Contribs AS Target " +
" USING #contribs AS Source " +
" ON " +
" Target.ID=Source.ID " +
" WHEN NOT MATCHED THEN " +
" INSERT (ID, FirstName, LastName, BirthDateTime, ContribName) " +
" VALUES (Source.ID, Source.FirstName, Source.LastName, Source.BirthDateTime, " +
" Source.ContribName); " +
" SELECT @Rows_con=@@ROWCOUNT; ";

cmd.CommandText = mergeSql;
SqlParameter Rows_dev = cmd.Parameters.Add("@Rows_dev", SqlDbType.Int);
SqlParameter Rows_award = cmd.Parameters.Add("@Rows_award", SqlDbType.Int);
SqlParameter Rows_con = cmd.Parameters.Add("@Rows_con", SqlDbType.Int);
Rows_dev.Direction = ParameterDirection.Output;
Rows_award.Direction = ParameterDirection.Output;
Rows_con.Direction = ParameterDirection.Output;



cmd.ExecuteNonQuery();
#endregion
HttpContext.Current.Session["Dev_inserted"] = (int)Rows_dev.Value;
HttpContext.Current.Session["Awa_inserted"] = (int)Rows_award.Value;
HttpContext.Current.Session["Con_inserted"] = (int)Rows_con.Value;

int a = (int)HttpContext.Current.Session["Dev_inserted"];
int b = (int)HttpContext.Current.Session["Awa_inserted"];
int c = (int)HttpContext.Current.Session["Con_inserted"];

#region dropping temp tables
cmd.CommandText = "DROP TABLE #developers; DROP TABLE #awards; DROP TABLE #contribs;";
cmd.ExecuteNonQuery();
#endregion

}
catch
{
return 0;
}
finally
{
conn.Close();
conn.Dispose();
cmd.Dispose();
}
}
#endregion

#region assign counts to session variable
using (SqlConnection conn = new SqlConnection(_ConnectionString))
{
conn.Open();

SqlDataReader SqlReader = null;
string sqlstr = " SELECT " +
" (SELECT COUNT (*) FROM Developers) AS Developer_Count, " +
" (SELECT COUNT (*) FROM Contribs) AS Contrib_Count, " +
" (SELECT COUNT (*) FROM Awards) AS Award_Count " +
" FROM Developers ";



SqlCommand SqlCommand = new SqlCommand(sqlstr, conn);
try
{
SqlReader = SqlCommand.ExecuteReader();
while (SqlReader.Read())
{
HttpContext.Current.Session["Developer_Count"] = Convert.ToInt32(SqlReader["Developer_Count"].ToString());
HttpContext.Current.Session["Contrib_Count"] = Convert.ToInt32(SqlReader["Contrib_Count"].ToString());
HttpContext.Current.Session["Award_Count"] = Convert.ToInt32(SqlReader["Award_Count"].ToString());

}
}
catch
{
return 1;
}
finally
{
conn.Close();
}
}
#endregion

return 0;
}
}
}
Loading