IT Nota

  • Home
  • How To
  • .NET
  • WordPress
  • Contact

How to Use Python to Connect to SQL Server

Can Python work with SQL Server database? Some may argue that SQL Server is not the best SQL database for Python, but in most cases it actually does not matter. In my corporate experience, SQL Server may be the easiest database to use with Python. Besides, in the workplace often times we just have to use what’s available to solve our business problems.

One of the advantages of Python language is the short development time that makes it very suitable to use as a simple tool to query a database.

The easiest way to access a SQL Server database with Python is by using pyodbc. From its project description, pyodbc is an open source Python module that makes accessing ODBC databases simple.

There is another driver called pymssql, but I have not tested it and it looks like even Microsoft themselves recommend pyodbc over pymssql although try to remain somewhat neutral.

There are several python SQL drivers available. However, Microsoft places its testing efforts and its confidence in pyodbc driver.

Source: Python SQL Driver: Getting Started.

You can install it by running the pip command in the Command Prompt (Windows) or Terminal (macOS):

pip install pyodbc

Once installed, create a new python file (e.g., hrquery.py) and import the pyodbc.

Example

hrquery.py

#!python3
# hr.py - Look for HR record based on last name.
#         If no argument is passed, return total count of records.

import sys, pyodbc

conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=DB_SERVER_NAME;'
                      'Database=DATABASE_NAME;'
                      'UID=SQLID_OR_NTID;'
                      'PWD=PASSWORD;'
                      'Trusted_Connection=no;')

cursor = conn.cursor()

# Check if there is a passed argument (last name)
# If yes, search by last name, in parameterized query
# otherwise, give total count of records from HR table.

if len(sys.argv) > 1:
    lastName = sys.argv[1]

    cursor.execute('SELECT * FROM dbo.HR WHERE [Last name] = ?', lastName)
    for i in cursor:
        print(i)
else:
    totalCount = cursor.execute('SELECT COUNT(*) FROM dbo.').fetchval()
    print(f"\nTotal count in HR table: {totalCount} records\n")

Other than Driver and Trusted_Connection, you need to substitute the values of all the parameters with your own (Server, Database, UID, and PWD).

To run your program, you can launch Command Prompt or Terminal and type the following:

C:\> python.exe hrquery.py lastname

OR

C:\> python.exe hrquery.py

Using Windows Authentication

If you use your NTID or a Windows Service Account (Windows Authentication) to login to the database, then there’s a slight difference in how you setup the connection string. This is the preferred way nowadays since you don’t store your password as a clear text in your code.

You just need to remove the PWD and set the Trusted_Connection to yes so it will look like the following:

conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=DB_SERVER_NAME;'
                      'Database=DATABASE_NAME;'
                      'UID=NTID_OR_SVCACCT;'
                      'Trusted_Connection=yes;')

That’s all there is in using Python to connect to SQL Server and the program will work the same way.

Further Reading

How to Install Python on Windows Server
pyodbc
Python SQL Driver

January 12, 2022 Filed Under: How To Tagged With: Microsoft SQL Server, Python, SQL Server

How to Quickly Query LDAP in Visual Studio SSIS

At times, you need to do a quick query against LDAP, but if your access is quite restricted, sometimes instead of using SQL Server Management Studio, you can also use SSIS Project within Visual Studio to do it.

Steps

  1. Launch Visual Studio.

  2. Create a new project and double-click on Integration Services Project.

    Visual Studio New Integration Services Project

  3. Name your Project name (e.g. ITNotaQueryLDAP), then click Create button.

    Visual Studio Configure New Integration Services Project

  4. For this exercise, we use the default Package.dtsx. If it’s not opened by default, just double click it on the right pane window.

  5. Drag Data Flow Task from SSIS Toolbox window to the Control Flow window.

    Add Data Flow Task to Control Flow in SSIS

  6. Now we want to add a Connection Manager by right-clicking on a blank area under Connection Managers pane and select New ADO.NET Connection….

    Add a new ADO.NET Connection in SSIS Connection Managers

    A smaller window titled Configure ADO.NET Manager will pop up, click on the New button.

    Create a new OLE DB Connection Manager in SSIS

  7. In a Connection Manager window, for the Provider, select OLE DB Provider for Microsoft Directory Services (under .Net Providers for OleDb). Then, click OK.

    OLE DB Provider for Microsoft Directory Services - .Net Providers\SqlClient Data Provider in SSIS

  8. Once you’re back in Connection Manager window, click on the Test Connection button. Make sure you see the Test connection succeeded screen. Click both OK buttons.

    SSIS Test Connection Manager (Successful)

  9. After you clicked OK, you can see the configuration summary of your SSIS connection. Click OK again.

    SSIS Connection Manager Configuration Summary

  10. At this point, you have a Data Connection in your Connection Managers which you can rename to LDAP Data Connection.

    Rename Connection Manager to LDAP Data Connection in SSIS

  11. Next, you can either double-click on the Data Flow Task or click on the Data Flow tab and drag ADO NET Source from the left pane, under Other Sources.

    Double click on the ADO NET Source box, and select the LDAP Data Connection for ADO.NET connection manager:, SQL command for Data access mode: and use the following SQL command as an example under SQL Command text:.

    LDAP://DC=ITNota,DC=Corp,DC=com;(&(objectCategory=Person)
    (SN=Smith));SN,givenName,displayName,
    mailNickName,sAMAccountName,mail,msExchHideFromAddressLists,
    homeMDB;subtree
    

    This query specifically looks for all records for anyone with a last name of “Smith” in the domain. You need to replace the values of the DCs to your own domain and also the search string (Smith) to make it work in your situation. Click Preview button.

    SSIS ADO NET Source Editor with LDAP Query

  12. Once you clicked Preview button, You should see the first 200 rows.

    Preview of LDAP Query in SSIS

That’s all there is to it.

Of course, there’s a caveat of using this as you can only view the first 200 rows of your result. This is not intended to be used as a solution of any deployment other than a workaround when you are limited with what you can access and you need to find a few records from your LDAP/Domain Services.

January 7, 2022 Filed Under: How To Tagged With: ActiveDirectory, ETL, Microsoft, SSIS

Basic Security Practice with Password

After one client was notified that her passwords were found in a data leak and may compromise the accounts from her iPhone:

iPhone warning for compromised passwords

Compromised...

Some of your passwords have appeared in a data leak, putting those accounts at high risk of compromise. iPhone can help...

It turns out, other than the password was to easy to guess, it was also due to password reuse. Of course the immediate action you need to take is to follow its recommendations to change all the affected passwords.

iPhone security recommendations for compromised passwords

But what needs to be explored further is why people still use the same password for their accounts although it’s been proven that this cause more damage when they get hacked. This issue stems from the fact that most non-technical people still resort to using memory to store their passwords, so they’re limited to either one short, easy to guess password or a bit more complicated one but then it’s used in every other accounts or even worse short and easy and reused everywhere. It bears repeating that this is a really dangerous practice, so we need to stop using the same password for two or more accounts.

So if we need to have a unique password for each account, it will be overly difficult (if not impossible) to create those passwords, much less memorizing them. That’s where a password manager comes handy.

Although password manager is already in use for a long time, there’s still a big gap between the technical and non-tech people. The latter, even if they have the password manager installed, still hesitate to use it, just because old habit dies hard or maybe they feel intimidated to try something new.

Nobody should feel intimidated using a password manager. If anything, using a password manager can actually make your life easier.

Here’s a simple way to get started. In this post, we’ll use 1Password as our password manager of choice. However, this is not the only one available. You can always choose your own password manager that suits your need. The principle is going to be the same.

By using a password manager, instead of memorizing so many easy-to-guess passwords, you just need to create one long passphrase and use that as your “master key” that you need to guard closely.

To increase the security of the master password, make that a passphrase that is easy to remember and makes sense to you. For example, you can use an unusual sentence such as, “What book do I like? The answer is Romeo and Juliet.” It is really up to you whether you want to use punctuation, capitalization or not. The point is, the more complex it is, the better as long as you can remember it.

Once you decided on the master password, install 1Password app (both on your phone and PC). If you want to make it even easier, don’t forget to install the browser extensions.

One useful shortcut to remember is CTRL + \ for Windows (or Command + \ on Mac) that you can use to enter your user id and password on a login page. But in order to use this feature, you need to have your 1Password unlocked first. Otherwise, 1Password will ask you to type in your master password to open it first.

Further Reading

“This password has appeared in a data leak” notice on iPhone
Get started with 1Password
Copy and fill passwords into apps that don’t work with 1Password

December 1, 2021 Filed Under: How To Tagged With: Information Security

How to Fix Failed to Decrypt Protected XML Node Error in SSIS

One morning, you discovered your SSIS package failed to run with the following error message:

Started: 5:00:03 AM
Error: 2021-10-08 05:00:04.62
Code: 0xC0016016
Source: SSIS_Package_ITNota
Description: Failed to decrypt protected XML node "DTS:Password" with error 0x8009000B "Key not valid for use in specified state.". You may not be authorized to access this information. This error occurs when there is a cryptographic error. Verify that the correct key is available.
End Error

This is typically caused by a simple setting that is too often overlooked during deployment.

In general, you do not want to save any connection ID and password in your package so you can inject the value from variables that you store either in a database or if you use a Package Deployment Model, then in an XML saved in the *.dtsConfig file.

So what you want to do before you build the package for PROD deployment, you want to set the package property Protection Level to DontSaveSensitive and you set all the values for your variables in your configuration file.

Steps to Set SSIS Package Property Level

  1. With your SSIS package opened, under the Control Flow tab, on a blank area, right-click your mouse and select Properties or press ALT+ENTER.

  2. Under Properties, go to Security, and look for ProtectionLevel. Check the value. By default, it’s set to EncryptSensitiveWithUserKey.

    SSIS Package Properties under Control Flow Tab

    The ProtectionLevel should be set to DontSaveSensitive.

    SSIS Package Properties Security Protection Level

Save the package and if the configuration is already setup, it’s ready to be deployed.

Further Reading

Failed to decrypt protected XML node “DTS:Password” with error 0x8009000B “Key not valid for use in specified state”

November 1, 2021 Filed Under: How To Tagged With: ETL, Microsoft, SSIS

Basic Configuration in Git

After installing Git, you may want to do some basic configuration so you can have a proper versioning history and collaborate with others in a shared repository.

First thing, you want to do before anything is to set your name and email.

You can open Git Bash on Windows or Terminal for macOS.

Replacing the First, Last and email address with your own, type the following:

git config --global user.name "First Last"
git config --global user.email [email protected]

This will be useful when you collaborate with others to see who has made what changes in the repository.

The next one, you want to set how the end of the line is treated in Git. This is often overlooked by many and usually does not pose any issues if everyone uses the same system or you just use Git by yourself. However if you check in and out your code using different platforms, it can cause an issue.

A bit of explanation with the end of the line for each OS here. On Windows an end of line consists of two characters \r\n which is a carriage return followed by a line feed. macOS on the other hand, only uses \n (line feed) and no carriage return. As such, whenever someone checks code in or out, Git needs to know how to treat the line ends so everyone will have consistent experience. So we have to configure the core.autocrlf accordingly.

Windows

For Windows, typically when you accept default settings from Git installer, it will set it automatically to true. But just in case you checked the wrong option or you wanted to be sure, you can configure it manually.

Type the following in Git Bash:

git config --global core.autocrlf = true

macOS

For macOS, type the following in Terminal:

git config --global core.autocrlf = input

Git global config for core.autocrlf in macOS Terminal

Now you can verify all your configuration settings by typing this command:

git config --global --list

If you found any typo, you can always fix it by running the same command line with the correct information.

That’s it. This configuration typically is done just once.

Next, while optional, you might want to check this post if you want to use your favorite text editor as the default.

Further Reading

How to Use Your Favorite Text Editor in Git
Git git-config Documentation
How to Install Git

October 14, 2021 Filed Under: How To Tagged With: Git

« Previous Page
Next Page »
Buy me a coffee Support this site
Buy Me a Coffee?

Categories

  • .NET
  • Coding
  • Cybersecurity
  • Database
  • How To
  • Internet
  • Multimedia
  • Photography
  • Programming
  • Resources
  • Review
  • Tips and Tricks
  • Uncategorized
  • Use Case
  • WordPress
  • Writing

Recent Posts

  • How to Setup WordPress Installation on Linux
  • How to View Stored Procedure Code in SQL Server
  • How to Find a String in SQL Server Stored Procedures
  • How to Remove Cached Credentials without Rebooting Windows
  • ESP Work Automation: Empowering Enterprises with Streamlined Workflows and Operational Efficiency

Recent Posts

  • How to Setup WordPress Installation on Linux
  • How to View Stored Procedure Code in SQL Server
  • How to Find a String in SQL Server Stored Procedures
  • How to Remove Cached Credentials without Rebooting Windows
  • ESP Work Automation: Empowering Enterprises with Streamlined Workflows and Operational Efficiency

Tags

.NET .NET Core AdSense ASP.NET Cdonts Dll Classic ASP Code Editor ETL FSharp Genesis Framework Git Google HP Asset Manager HTML5 Hugo IIS Information Security Internet Internet Information Services iOS JAMStack Linux macOS Microsoft Microsoft SQL Server MVC PHP PowerShell Python Simple Mail Transfer Protocol Smtp Server SQL SQL Server SSIS SSMS SSRS Sublime Text Visual Studio Visual Studio Code VPN Windows Windows 8 Windows 10 Windows 2012 Windows Server

Copyright © 2011-2026 IT Nota. All rights reserved. Terms of Use | Privacy Policy | Disclosure