ASP.NET Web Pages – SQL Syntax

At times you will need to access a database to perform tasks such as updating and removing information. Here we will go over the most commonly used SQL statements such as reading from database, updating database and deleting database entries.

Connecting to Database

To connect to a database you use the Database.Open command then in brackets you give the database name. The database must be in the app_data folder (unless you specified it somewhere else in the web.config).



The database name must be wrapped in quotation marks; you do not specify the database extension.

SQL – Select Statement

The select statement will read data from a database. The syntax is as follows:

"SELECT columnname FROM tablename";

To select all the columns you use an asterisk:

"SELECT * FROM tablename";

SQL – Insert Statement

The insert statement will insert data into the database:

"INSERT INTO tablename (column 1, 2, 3 5) VALUES (@1, @2)";

SQL – Update Statement

"Update tablename set column1=@1, column2=@2";

SQL – Delete Statement

"DELETE FROM tablename WHERE column=value";

SQL – Where

The where clause is used to filter records like this:

"SELECT  * FROM tablename WHERE columnname = 'value';

The examples shown here are for ASP.NET Razor / Web Pages, so you do need the quotation marks and semi-colon.