Chapter 36. Accessing databases

 

This chapter covers

  • Using the .NET Framework to connect to a database
  • Querying databases with PowerShell
  • Adding, deleting, and modifying databases with PowerShell
  • Running stored procedures with PowerShell

It’s not uncommon to need to access something that’s in some kind of a database, so that’s what we’ll show you in this chapter. Now, as with many other chapters, we need to set some boundaries for what we’re going to cover (and not cover). This chapter won’t teach you the SQL query language, nor will it teach you how to create, maintain, or manage a database. This chapter is about using PowerShell to access the data that’s in a database and nothing more. We also assume you already have the necessary database drivers installed on your computer and that you can physically connect to whatever database you need. You don’t need any particular commands or extensions—you don’t need to install SQL Server’s PowerShell stuff, for example—but if you’re accessing a database such as Oracle or MySQL, you’ll need the appropriate drivers installed for .NET.

Something else to keep in mind is that there are no native database access commands in PowerShell. Instead, we’ll be using the underlying .NET Framework classes, which means the information in this chapter will look a lot like .NET Framework programming. Right now, PowerShell has no help for that.

36.1. Native SQL vs. OLEDB

36.2. Connecting to data sources

36.3. Querying data

36.4. Adding, changing, and deleting data

36.5. Calling stored procedures

36.6. A module to make it easier

36.7. Summary