Databases in Visual Studio
Database in VisualStudio An MS SQL database are built in Visual studio The Name can be something like ”(localdb)\Projects” or ””(localdb)\v11.0” Visible through the view -> SQL Server Object Explorer From here you can Create new database instances (e.g. new databases) Within a database you can create tables, triggers, constraints (e.g. foreign keys) etc. Within a table you can insert, delete, update rows OR query information (select) using SQL Alternative you view data and change data from ‘view data’
Three ways to create tables Create a database in SQL server Object Explorer 1.‘right click’ and use the design tool in Visual Studio 2.Make a SQL script to create the table ”CREATE TABLE table-name ( column-name type …, ….)” Create a database inside the project 3.Entity Framework e.g. include a database into the project “add -> new item -> data -> ADO.NET Entity Data Model”
Create database in SQL Server Object Explorer Open SQL Server Object Explorer Open (localdb)\projects OR (localdb)\v11.0 Open Database ‘right click’ -> add new database … Choose a name
Create tables using SQL For details see Short: CREATE TABLE Persons ( PersonID int NOT NULL PRIMARY KEY, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) );
Create tables using MS SQL gui design For details see: (1) Right click -> Add new table (2) Change name of Table (3) Add columns (4) Update database
Create Database inside the project Create a normal project (form, console, wpf, class-library) ‘right click’ on project add -> new item -> data-> service-based database … name it
Create tables In the project : add -> new item -> data -> ADO.NET Entity Data Model … name it choose ‘empty model’ In diagram ‘right click’ -> add new -> entity (=table) Fill out entity name (= table name) Fill out property for primary key (default id:int) Add more properties/columns one by one – remember type ‘right click’ generate database from model – pick (connect to ) your database