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();

                // 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))

                // 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);

                // 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);

                // 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);

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

                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();

                    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();

                        // Perform the bulk insert
                        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(''))

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


The output again is D:\domain_expiration.csv

Here’s the complete script.

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

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

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

    return $rootDomain

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

    $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


#$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

$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"

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

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.



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



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\,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\”,encryptPassword=MyPassKey -dest:appHostConfig=”8aselect” -skip:objectName=filepath,skipAction=delete

mailwizz failing with VestaCP and MariaDb in Centos 7

I recently had a frustrating problem where I couldn’t upload files into MailWizz host.  This also causes problems with command line CSV imports. I discovered I had to open up /etc/my.cnf and edit the following line to

max_allowed_packet = 110M

then in /etc/php.ini I changed the following two lines
post_max_size = 200M
upload_max_filesize = 500M

then execute

service mariadb restart

Under settings, import/export on the backend I set CLI Import Enabled to Yes and Url Import Enabled to Yes.




Watch out for IPTables and order of execution of rules

I recently need to block Barracuda network ips from accessing my mail server, MailWizz. I did the typical

iptables -A INPUT -s -j DROP

iptables -A INPUT -s -j DROP

iptables -A INPUT -s -j DROP

iptables -A INPUT -s -j DROP

iptables -A INPUT -s -j DROP

iptables -A INPUT -s -j DROP

iptables -A INPUT -s -j DROP


service iptables save

service iptables end

the problem with this setup is that those rules end up getting appended to the end and never get blocked. It’s better to go into the iptables directly. ON Centos 7, that is located in /etc/sysconfig/iptables. I added these lines directly before any of the rules were executed.


I’m also putting in a post on how to block Amazon Bots.  However you’ll need to redo the above after running these commands in Centos 7

yum install git

git clone

yum install jq

cd aws-blocker

bash aws-blocker

iptables-save | sudo tee /etc/sysconfig/iptables

service iptables save

service iptables restart

Then add the above manually and you’ll have both AWS and Barracuda blocked.