Create Table in Database from Excel Sheet

I find the SSIS Integration to be extremely buggy and annoying. I use this code to read in an excel file and create a table in my database. Substitute the connection string with your database connection. The program prompts for a filename and a tableName.

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using ClosedXML.Excel;

namespace ExcelToSQL
{
    class Program
    {
        
        static void Main(string[] args)
        {
            string filePath = "d:/defensepurchasingclean.xlsx";
            string tableName = "DefensePurchasingClean";
            string connectionString = "Data Source=DESKTOP-NK1V72;Initial Catalog=dbFPDS;Integrated Security=True";
            Console.Write("Enter File Name:  ");
            filePath = Console.ReadLine();
            Console.Write("Enter table name:  ");
            tableName= Console.ReadLine();

            try
            {
                // Read the Excel file
                var workbook = new XLWorkbook(filePath);
                var worksheet = workbook.Worksheet(1);

                // Get the column names and determine the maximum length of characters in each column
                var columnNames = worksheet.Row(1).CellsUsed().ToList();
                var columnLengths = new int[columnNames.Count()];

                for (int i = 0; i < columnNames.Count(); i++)
                {
                    var columnValues = worksheet.Column(i + 1).CellsUsed().Skip(1).Select(c => c.Value.ToString());
                    columnLengths[i] = columnValues.Max(c => c.Length);
                }

                // Create the table in SQL Server
                CreateTable(tableName, columnNames, columnLengths, connectionString);

                // Insert data into the table
                InsertData(tableName, worksheet, columnNames, connectionString);

                Console.WriteLine("Data imported successfully!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }

        static void CreateTable(string tableName, List<IXLCell> columnNames, int[] columnLengths, string connectionString)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();

                // Check if the table already exists, and truncate it if it does
                string checkTableQuery = $"IF OBJECT_ID('{tableName}', 'U') IS NOT NULL TRUNCATE TABLE {tableName}";
                SqlCommand checkTableCommand = new SqlCommand(checkTableQuery, connection);
                checkTableCommand.ExecuteNonQuery();

                // Drop the table if it exists
                string dropTableQuery = $"IF OBJECT_ID('{tableName}', 'U') IS NOT NULL DROP TABLE {tableName}";
                SqlCommand dropTableCommand = new SqlCommand(dropTableQuery, connection);
                dropTableCommand.ExecuteNonQuery();

                // Create the table
                string createTableQuery = $"CREATE TABLE {tableName} (";
                for (int i = 0; i < columnNames.Count; i++)
                {
                    string columnName = "[" + columnNames[i].Value.ToString() + "]";
                    int columnLength = columnLengths[i];
                    string dataType = $"VARCHAR({columnLength})";
                    createTableQuery += $"{columnName} {dataType}";

                    if (i < columnNames.Count - 1)
                        createTableQuery += ",";
                }
                createTableQuery += ")";
                SqlCommand createTableCommand = new SqlCommand(createTableQuery, connection);
                createTableCommand.ExecuteNonQuery();
            }
        }

        static void InsertData(string tableName, IXLWorksheet worksheet, List<IXLCell> columnNames, string connectionString)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();

                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
                {
                    bulkCopy.DestinationTableName = tableName;

                    // Map the column names in the spreadsheet to the table columns
                    foreach (IXLCell columnNameCell in columnNames)
                    {
                        string columnName = columnNameCell.Value.ToString();
                        bulkCopy.ColumnMappings.Add(columnName, columnName);
                    }

                    // Convert the worksheet data to a DataTable for bulk insert
                    DataTable dataTable = new DataTable();
                    foreach (IXLCell columnNameCell in columnNames)
                    {
                        string columnName = columnNameCell.Value.ToString();
                        dataTable.Columns.Add(columnName);
                    }

                    var rows = worksheet.RowsUsed().Skip(1); // Skip the header row

                    foreach (IXLRow row in rows)
                    {
                        var dataRow = dataTable.NewRow();
                        for (int i = 0; i < columnNames.Count; i++)
                        {
                            dataRow[i] = row.Cell(i + 1).Value.ToString();
                        }
                        dataTable.Rows.Add(dataRow);
                    }

                    try
                    {
                        // Perform the bulk insert
                        bulkCopy.WriteToServer(dataTable);
                        Console.WriteLine("Data imported successfully!");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error during bulk insert: " + ex.Message);
                    }
                }
            }
        }




    }

}


Getting Expiration Dates for all hosted websites

ChatGPT has become my new programming partner. I tried many variations of .net code to read host bindings from IIS and then run a WHOIS service to get expiration dates, including C# code. Nothing worked and I got very tired of the process. To be clear, it takes knowledge of programming and architecture to get CHATGPT to create the code base you want.

So I ran APPCMD from a command prompt and found that the easiest way to get the bindings. Then I prompted CHATGPT to create various Powershell functions to read the root domain and get the registrar and expiration date for each domain. I then instructed CHATGPT to write code to create a CSV file sorted by expiration date and write out the domain, expiration date, and name of the Registrar.  D:\sites.xml  and D:\domain_expiration.csv are hard coded in the powershell script. The powershell script also uses the whois service which took several prompts to get CHATGPT to tell me how to install it.

Start with this appcmd

appcmd list sites /xml &gt;d:\sites.xml

Many of us don’t use PowerShell very often and often forget how to run it. I use the developer version of powershell for visual studio 2022.

I ran these commands directly with just a cut and paste. You first need to install the whois utility using the Chocolatey package manager.

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

choco install whois

Here’s the powershell script to run. To run this script I copied the script below into a file called ExpirationDates.ps1. To run the file from PowerShell

./ExpirationDates.ps1

The output again is D:\domain_expiration.csv

Here’s the complete script.

function Get-RootDomain {
    param (
        [Parameter(Mandatory = $true)]
        [string]$Domain
    )

    if ([string]::IsNullOrWhiteSpace($Domain)) {
        return ""
    }

    $rootDomain = $Domain -replace "^.*?(([^.]+)\.[^.]+)$", '$1'

    return $rootDomain
}



function Get-DomainExpirationDate {
    param (
        [Parameter(Mandatory = $true)]
        [string]$Domain
    )

    $whoisResponse = whois $Domain 2>$null
#Write-Host $whoisResponse
    $expiryPatterns = @(
        "(?m)Registry Expiry Date:(.+)",
        "(?m)Registrar Registration Expiration Date:(.+)",
        "(?m)Expiration Date:(.+)",
        "(?m)Renewal Date:(.+)"
    )

    $expirationDate = $null
    foreach ($pattern in $expiryPatterns) {
        $match = [regex]::Match($whoisResponse, $pattern)
        if ($match.Success) {
            $expirationString = $match.Groups[1].Value.Trim()

            # Extract the date portion from the expiration string
            $datePattern = "\d{4}-\d{2}-\d{2}"
            $dateMatch = [regex]::Match($expirationString, $datePattern)
            if ($dateMatch.Success) {
                $expirationDate = $dateMatch.Value
            }

            break
        }
    }


#$registrationPattern = "(?m)Registrar:(.+)"
#    $registrationMatch = [regex]::Match($whoisResponse, $registrationPattern)
#    if ($registrationMatch.Success) {
#        $registrationCompany = $registrationMatch.Groups[1].Value.Trim()
#    }


            $registrationCompanyPattern = "(?m)Registrar:\s*([\s\S]*?Registrar)"
            $registrationCompanyMatch = [regex]::Match($whoisResponse, $registrationCompanyPattern)
            $registrationCompany = ""
            if ($registrationCompanyMatch.Success) {
                $registrationCompany = $registrationCompanyMatch.Groups[1].Value.Trim()
            }



    $result = [PSCustomObject]@{
        Domain = $Domain
        ExpirationDate = $expirationDate
        RegistrationCompany = $registrationCompany
    }

    return $result



    #return $expirationDate
}

$xmlPath = "D:\sites.xml"
$outputPath = "D:\domain_expiration.csv"

# Read XML from the file
$xmlContent = Get-Content $xmlPath -Raw

# Load XML content
$xml = $xmlContent

# Get the SITE nodes
$sites = $xml.SelectNodes("//SITE")

# Create an array to store the domain names
$domainArray = @()

# Loop through each SITE node
foreach ($site in $sites) {
    $bindings = $site.GetAttribute("bindings")

    # Extract the domain names from the bindings attribute
    $domainNames = $bindings -split ","
    foreach ($domainName in $domainNames) {
        $domain = ($domainName -split ":")[2].Trim()
        #$domain = ($domain -split "\.")[1..($domain -split "\.").Length] -join "."
 	#$domain = $domain -replace ".*?\.", ""
 #$domain = $domain -replace ".*?(\w+\.\w+)$", '$1'
#$domain = $domain -replace ".*?((?:[\w-]+\.)+[\w-]+)$", '$1'

        
     if (![string]::IsNullOrWhiteSpace($domain)) {
	$rootDomain = Get-RootDomain -Domain $domain
        if (![string]::IsNullOrWhiteSpace($rootDomain) -and $domainArray -notcontains $rootDomain) {
            Write-Host $domain + ":" + $rootDomain
            $domainArray += $rootDomain
        }

 }
    }
}

$domainArray = $domainArray | Select-Object -Unique
# Create an array to store the domain name and expiration date
$domainExpirationArray = @()

# Loop through each domain and retrieve the expiration date
foreach ($domain in $domainArray) {
    $expirationDate = Get-DomainExpirationDate -Domain $domain
    $domainExpirationArray += [PSCustomObject]@{
        Domain = $domain
        ExpirationDate = $expirationDate.ExpirationDate
        RegistrationCompany=$expirationDate.RegistrationCompany
    }
}

$domainExpirationArray = $domainExpirationArray | Sort-Object -Property ExpirationDate, Domain, RegistrationCompany

# Export the domain name and expiration date to a CSV file
$domainExpirationArray | Export-Csv -Path $outputPath -NoTypeInformation

Write-Host "CSV file generated: $outputPath"

Moving SSIS Packages from Windows Server 2012 R2 to Windows Server 2019

I tried opening the designer for a package on an old SSIS dtproj from Windows Server 2012R2 on Windows Server 2019. I kept getting mysterious failures.  So I solved it by creating a new SSIS Integration Services project then adding each package individually.  Before I did this, I opened the code and changed the connection strings and used windows authentication and (local) instead of the server name.

I was wondering why, so I asked CHATGPT the following question:

In order to use SSIS packages on a Windows 19 server from windows server 2012 R2, why did I have to create a new project then Add Existing Packages. Why did the old solution fail every time I tried to open a package in the designer?

CHATGPT actually answered the question.

There could be several reasons why the old solution failed to open SSIS packages in the designer. One possibility is that the version of SQL Server Data Tools (SSDT) being used on the Windows Server 2012 R2 machine is not compatible with the version of SQL Server Integration Services (SSIS) used to create the SSIS packages.

In this case, creating a new project in SSDT and then adding the existing packages would allow the packages to be opened and edited in the newer version of SSDT.

Another possible reason could be that the old solution was corrupted or had missing dependencies that prevented it from opening the packages properly. Creating a new project would ensure that all the necessary dependencies are present and that the project is not corrupted.

It’s also worth noting that different versions of SQL Server may use different formats for SSIS packages, which could cause compatibility issues when opening older packages in a newer version of SSIS. Creating a new project and adding the existing packages ensures that the packages are converted to the correct format for the new version of SSIS.

Reporting Services Configuration for sending mail

we have a standard IIS SMTP server installed. One of its sending default accounts is Network Service.  Reporting Services has to be configured to use it.

Open the Reporting Services Configuration Manager and go to Service Account. Change it to Network Service.  You will be prompted to save an encryption key with a password.

Go to Email Settings. Put in the sending email address, Use SMTP server is chosen for you, and the SMTP Server ip is 127.0.0.1

Go to the root of the tree on the left, stop and restart the service.

You’re going to need this for Subscription Services. On the SMTP server in iis choose properties, delivery outbound relay and uncheck limit connections to.   My emails kept getting queued for failing to do so.

 

Migrate Reporting Services

Use Report Server Configuration Manager on source server, click on Encryption keys and backup. You’ll need these on the destination server

use Report Server Configuration Manager  and stop the service on both servers

detach ReportServer and ReportServerTempDb on both servers

I advise on the Destination Server making a backup of these two database

Copy the ReportServer and ReportServerTempDb  and the encryption key file to the new server and attach them in Sql Server management studio

Start the service

Click on Encryption Keys, restore and restore the key file generated on the source  server.

Subscription users are going to be different, you have to update the users to the new server. They’re in the format of ServerName\username

select * from subscriptions  in ReportServer database.  Run a simple update query.

Done

 

Adding CLR assemblies after Server Migration

the CLR assemblies are brought across with the database. However they need to be added to the sys.trusted_assemblies to actually run

  1. Go to the database you have the assemblies in. It’s different for each database using CLR assemblies.

List them with sys.assemblies. You need the clr_name field

select * from sys.assemblies

List your CLR assembly files with

select * from sys.assembly_files

you need the hash field

Then run this script for each assembly

–run from create assembly after from copied to @asmbin
USE master;
GO
DECLARE @clrName nvarchar(4000) = ‘clr_name’ –this name is quoted
DECLARE @asmBin varbinary(max) = hash  –no quotes here paste the entire value as is.
DECLARE @hash varbinary(64);

SELECT @hash = HASHBYTES(‘SHA2_512’, @asmBin);

EXEC sys.sp_add_trusted_assembly @hash = @hash,
@description = @clrName;

 

go

Copying Integration Service Packages to new server

Location of file on Windows Server 2012 c:\program files\Microsoft SQL Server\120\DTS\Binn

I listed the packages with this SQL Query then created a batch file with the commands below

select * from msdb.dbo.sysssispackages

Exporting SSIS Packages to File:

dtutil /SQL ExportPackage /COPY FILE;C:\Packages\ExportPackage.dtsx

Importing SSIS Package to SQL:

dtutil /file C:\Packages\ImportPackage.dtsx /copy sql;ImportPackage

I recommend opening the files in a text editor and making sure the connection strings are not pointing to an old server.

Also before importing the files, create a visual studio project. Import in all the packages. Go to the properties of the project, choose configuration, build and choose the version of sql server you’re using. Rebuild all the DTS packages.

You do have the option of creating a new visual studio project and importing in all the packages and performing an in place upgrade. However some of these were written in an older version and hard to upgrade. I just used the SQL Server versioning in VisualCron, our Job scheduler, and in some cases 32 bit mode.

MsDeploy Skip website directory contents

First, I created a list of the sites I needed then a simple batch file containing the MSDeploy commands. Be careful of virtual directories. I had virtual directories that went to some large document repositories. The code I’m giving you only handles the root directory of the site and its physical subdirectories.

appcmd list vdir> c:/sites.text

With some editing of the file, I created an import format that I could use in Excel then used the concatenate excel function to build the msdeploy lines below.

MsDeploy is now a separate install. Once installed you will find it at c:\program files\iis\Microsoft Web Deploy V3. Run your commands from this directory.

It’s a great deal easier to directly transfer all the website files first. I personally use FileZilla between servers.  Once done here’s the two commands I use to export a site then import it to a new site. The example site here is 8aSelect and I kept the archivedirectory in c:\resources

Command to export a site and not include the contents of the site itself

msdeploy -verb:sync -source:apphostconfig=”8aselect” -dest:archivedir=c:\resources\8aselect.zip,encryptPassword=MyPassKey -skip:objectName=dirPath,absolutePath=”d:\\websites\\8aselect”

Command to import a site and skip deleting existing files

msdeploy -verb:sync -source:archivedir=”c:\resources\8aselect.zip”,encryptPassword=MyPassKey -dest:appHostConfig=”8aselect” -skip:objectName=filepath,skipAction=delete

Copying CLR assemblies across servers

I recently had an Sql Server 2014 r2 server I needed to migrate to Sql Server 2019 r2 with a set of CLR assemblies on it. My plan is to detach each database and reattach them on the new box. The assemblies are transferred along with the database, but they won’t run. I went through the following steps to run them.

Step 1. Click on Programmability, Assemblies under the Database in Sql Server Management Studio.

Step 2: Right click and choose Script Assembly as Create to New Query Editor Window

you’ll get something that looks like this

CREATE ASSEMBLY [Microsoft.SqlServer.Types]

from   XXXXXX

with permission_set=unsafe

Step #3, run the query select * from sys.assemblies you’ll get something that looks like this

name principal_id assembly_id clr_name
Microsoft.SqlServer.Types 4 1 microsoft.sqlserver.types, version=15.0.0.0, culture=neutral, publickeytoken=89845dcd8080cc91, processorarchitecture=msil

Step #4 You have to make the assemblies trusted. Run the following code. Substitute the XXXXX where indicated and the clr_name from above

–run from create assembly after from copied to @asmbin
USE master;
GO
DECLARE @clrName nvarchar(4000) = ‘microsoft.sqlserver.types, version=15.0.0.0, culture=neutral, publickeytoken=89845dcd8080cc91, processorarchitecture=msil’
DECLARE @asmBin varbinary(max) = XXXXXXXX
DECLARE @hash varbinary(64);

SELECT @hash = HASHBYTES(‘SHA2_512’, @asmBin);

EXEC sys.sp_add_trusted_assembly @hash = @hash,
@description = @clrName;

go

Step #5. Here’s the query to list the trusted assemblies (I put XXXX for hash,  you can take that out)

SELECT ‘XXXX’ as hash, description, create_date
FROM sys.trusted_assemblies

run as

SELECT hash, description, create_date
FROM sys.trusted_assemblies

with these results

hash description create_date
XXXX microsoft.sqlserver.types, version=15.0.0.0, culture=neutral, publickeytoken=89845dcd8080cc91, processorarchitecture=msil 1/2/2023