80 likes | 232 Views
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
E N D
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 • ‘right click’ and use the design tool in Visual Studio • Make a SQL script to create the table ”CREATE TABLE table-name ( column-name type …, ….)” Createa database inside the project • 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
Createtablesusing SQL • For detailsseehttp://www.w3schools.com/sql/default.asp • Short: CREATE TABLE Persons(PersonIDint NOT NULL PRIMARY KEY,LastNamevarchar(255) NOT NULL,FirstNamevarchar(255),Address varchar(255),City varchar(255));
Createtablesusing MS SQL gui design • For detailssee: http://msdn.microsoft.com/en-us/library/hh272695(v=vs.103).aspx (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 projectadd -> new item -> data-> service-based database … name it
Createtables • In the project :add -> new item -> data -> ADO.NET Entity Data Model … name itchoose ‘empty model’ • In diagram ‘right click’ -> add new -> entity (=table) • Fill out entityname (= tablename) • Fill out property for primarykey (default id:int) • Add more properties/columns one by one – remember type • ‘right click’ generate database from model – pick (connect to ) your database