Tuesday, January 3, 2023

python code to import xlsx file in mysql and validate also each column and rows before import

 import mysql.connector

import openpyxl


# Open the .xlsx file

wb = openpyxl.load_workbook('data.xlsx')

sheet = wb.active


# Connect to the MySQL database

cnx = mysql.connector.connect(user='user', password='password', host='host', database='database')

cursor = cnx.cursor()


# Validate and import the data

for row in sheet.rows:

    # Validate each cell in the row

    if row[0].value == None:

        print("Error: Missing value in column 1")

    elif row[1].value == None:

        print("Error: Missing value in column 2")

    else:

        # If the data is valid, insert it into the database

        sql = "INSERT INTO table (column1, column2) VALUES (%s, %s)"

        val = (row[0].value, row[1].value)

        cursor.execute(sql, val)


# Commit the changes to the database

cnx.commit()


# Close the connection

cnx.close()

Monday, January 2, 2023

logout from linux ssh from windows powershell

 To exit the Bash session, type "exit" without quotes and then the Enter key.


That should exit the SSH session and get you back to the PS C:\Windows\system32 prompt.


Just close the Powershell session by clicking on the X at the upper right corner of the window.

bash: apt-get: command not found CentOS 7

  Amazon Linux it's CentOS-based, which is RedHat-based. RH-based installs use yum not apt-get. Something like yum search httpd should show you the available Apache packages - you likely want yum install httpd24.

how to make sure node is installed or not in window 10 ?

 To see if Node is installed, open the Windows Command Prompt, Powershell or a similar command line tool, and type node -v . This should print the version number so you'll see something like this v0. 10.35

Saturday, December 31, 2022

What is the difference between LINQ methods that start with "As" and those that start with "To"?

 LINQ is a set of extension methods for the .NET Framework that allow developers to query data from various sources using a consistent syntax.

The methods that start with "As" generally return a new sequence that is a projection of the original sequence, with each element in the new sequence being a transformation of the corresponding element in the original sequence. These methods do not perform any conversion, but rather just change the way the data is presented. For example, the AsEnumerable() method returns the input sequence as an IEnumerable<T>, regardless of its original type.

On the other hand, the methods that start with "To" perform a conversion of the input sequence to a new type or format. These methods often involve creating a new collection or data structure to store the elements of the input sequence, and as such may have a performance impact. For example, the ToList() method converts an input sequence to a List<T>, and the ToDictionary() method converts an input sequence to a Dictionary<TKey, TValue>.

Does IEnumerable.ToList() have performance impacts ?

 ToList() creates a new List object and copies all the elements from the source IEnumerable<T> into the new List. This means that it has to iterate through all the elements in the source IEnumerable<T>, which has a time complexity of O(n), where n is the number of elements in the source IEnumerable<T>.

This can have a performance impact, especially if the source IEnumerable<T> is large and the operation is being performed frequently. In these cases, it may be more efficient to use a different approach, such as using a loop to add the elements to the List manually, or using a different data structure that allows for more efficient insertion and retrieval of elements.

However, in many cases the performance impact of ToList() will not be significant, especially if the source IEnumerable<T> is small or the operation is only being performed infrequently. It is generally a good idea to use ToList() when you need to work with a List rather than an IEnumerable<T>, as it can make the code more readable and easier to work with.

List Capacity vs Count

 That's correct! In the .NET framework, the List<T> class is implemented as an array that grows dynamically as items are added to it. The Capacity property represents the size of the underlying array, and the Count property represents the number of items that have been added to the list.


When you add an item to the list and the list is already at capacity, the List<T> class will automatically double the size of the underlying array and copy all of the items from the old array to the new one. This process is known as "resizing the array."


It's important to note that the Capacity property is not the same as the Count property. The Capacity property represents the size of the underlying array, while the Count property represents the number of items that have been added to the list. The Count property will always be less than or equal to the Capacity property.


For example, if you have a list with a capacity of 10 and you add 5 items to it, the Count property will be 5 and the Capacity property will be 10. If you then add another item to the list, the list will automatically resize the array and the Capacity property will be increased to 20. However, the Count property will still be 6.

ASP.NET Core

 Certainly! Here are 10 advanced .NET Core interview questions covering various topics: 1. **ASP.NET Core Middleware Pipeline**: Explain the...