Windows PowerShell Parse Methods
Windows PowerShell parses in two modes—command mode and expression mode. In expression mode, Windows PowerShell parses as most high-level languages parse: numbers are numbers; strings need to be quoted, and so on. Expressions are things such as:
2+2
4
"Hello" + " world"
Hello world
$a = "hi"
$a.length * 13
26
When parsing in command mode, strings do not need to be quoted and everything is treated like a string except variables and things in parentheses. For example:
copy users.txt accounts.txt
users.txt and accounts.txt are treated as strings
write-host 2+2
2+2 is treated as string, not an expression to evaluate
copy $src $dest
$src and $dest are variables. Not needing to use quotation marks when working in a command shell can be very beneficial in the long run because it greatly reduces the amount of typing required.
The parsing mode is determined by the first token encountered. If the token is a number, variable, or quoted string, then the shell parses in expression mode. If the line starts with a letter, an & (ampersand), or a . (dot) followed by a space or a letter, the parsing is done in command mode.
| 2+2 | Expression mode starts with a number. |
| "text" | Expression mode starts with a quotation mark. |
| text | Command mode starts with a letter. |
| & "text" | Command mode starts with an ampersand. |
| . "file.ps1" | Command mode starts with a dot followed by a space. |
| .125 | Expression mode starts with a dot, which is followed by a number—not a space or a letter. |
| .text | Command mode starts with a dot, which is part of the command name ".text" |
It is very useful to be able to mix expressions and commands; which you can do by using parentheses. Inside parentheses, the mode discovery process starts over.
Write-Host (2+2)
2+2 is treated as an expression to evaluate and is passed to the Write-Host command.
(Get-Date).day + 2
Get-Date is treated as a command, and the result of executing it becomes the left value in the expression.
You can nest commands and expressions without restrictions.
Write-Host ((Get-Date).day + 2)
Get-Date is a command. ((Get-Date).day+2) is an expression, and Write-Host ((Get-Date).day + 2) is a command again.
Write-Host ((Get-Date) - (Get-Date).date)
The Get-Date command is used twice to determine how much time has passed since midnight (condition).
Posted in: .NET Framework MS Windows| Tags: Windows PowerShell Command Windows PowerShell parse expression mode numbers strings quoted Get-Date Write-Host conditionRunning Queries in Visual Studio
If you’ve never used SQL before, you may want to play around with it and create some sample queries before you start using it in an ASP.NET site. Most database products provide some sort of tool for testing queries. If you’re using a full version of SQL Server, you can try SQL Server Management Studio or SQL Query Analyzer. If you don’t want to use an extra tool, you can run your queries using the Server Explorer window described earlier. Just follow these steps in Visual Studio:
1. Right-click your connection, and choose New Query.
2. Choose the table (or tables) you want to use in your query from the Add Table dialog box (as shown in Figure 15-5), click Add, and then click Close.
3. You’ll now see a handy query-building window. You can create your query by adding check marks next to the fields you want, or you can edit the SQL by hand in the lower portion of the window. Best of all, if you edit the SQL directly, you can type in anything—you don’t need to stick to the tables you selected in step 2, and you don’t need to restrict yourself to Select statements.
4. When you’re ready to run the query, select Query Designer ä Execute SQL from the menu. Assuming your query doesn’t have any errors, you’ll get one of two results. If you’re selecting records, the results will appear at the bottom of the window . If you’re deleting or updating records, a message box will appear informing you how many records were affected.
SQL Basics in ADO.NET
When you interact with a data source through ADO.NET, you use SQL to retrieve, modify, and update information. In some cases, ADO.NET will hide some of the details for you or even generate required SQL statements automatically. However, to design an efficient database application with a minimal amount of frustration, you need to understand the basic concepts of SQL.
SQL (Structured Query Language) is a standard data access language used to interact with relational databases. Different databases differ in their support of SQL or add other features, but the core commands used to select, add, and modify data are common. In a database product such as SQL Server, it’s possible to use SQL to create fairly sophisticated SQL scripts for stored procedures and triggers (although they have little of the power of a full object-oriented programming language). When working with ADO.NET, however, you’ll probably use only the following standard types of SQL statements:
• A Select statement retrieves records.
• An Update statement modifies existing records.
• An Insert statement adds a new record.
• A Delete statement deletes existing records.
If you already have a good understanding of SQL, you can skip the next few sections.
Otherwise, read on for a quick tour of SQL fundamentals. nTip To learn more about SQL, use one of the SQL tutorials available on the Internet, such as the one at http://www.w3schools.com/sql. If you’re working with SQL Server, you can use its thorough Books
Online help to become a database guru.
Browsing and Modifying Databases in Visual Studio
As an ASP.NET developer, you may have the responsibility of creating the database required for a web application. Alternatively, it may already exist, or it may be the responsibility of a dedicated database administrator. If you’re using a full version of SQL Server, you’ll probably use a graphical tool such as SQL Server Management Studio to create and manage your databases.
If you don’t have a suitable tool for managing your database, or you don’t want to leave the comfort of Visual Studio, you can perform many of the same tasks using Visual Studio’s
Server Explorer window.
Here’s how you can get started. First, choose View äServer Explorer from the Visual Studio menu to show the Server Explorer window. Then, using the Data Connections node in the
Server Explorer, you can connect to existing databases or create new ones. Assuming you’ve installed the pubs database (see the readme.txt file for instructions), you can create a connection to it by following these steps:
1. Right-click the Data Connections node, and choose Add Connection. If the Choose
Data Source window appears, select Microsoft SQL Server and then click Continue.
2. If you’re using a full version of SQL Server, enter localhost as your server name. This indicates the database server is the default instance on the local computer. (Replace this with the name of a remote computer if needed.) If you’re using SQL Server Express, you’ll need to use the server name localhost\SQLEXPRESS instead, as shown in
Figure 15-2. The SQLEXPRESS part indicates that you’re connecting to a named instance of SQL Server. By default, this is the way that SQL Server Express configures itself when you first install it.
3. Click Test Connection to verify that this is the location of your database. If you haven’t installed a database product yet, this step will fail. Otherwise, you’ll know that your database server is installed and running.
4. In the Select or Enter a Database Name list, choose the pubs database. (In order for this to work, the pubs database must already be installed. You can install it using the database script that’s included with the sample code, as explained in the following section.)
If you want to see more than one database in Visual Studio, you’ll need to add more than one data connection.