0% found this document useful (0 votes)
69 views16 pages

Primeros Pasos en SQL Server

This document provides steps to create a C# console application that connects to a SQL Server database using Entity Framework code-first approach. It describes creating User and Task model classes, adding Entity Framework dependencies via NuGet, generating a database based on these models, and performing basic CRUD operations on user and task data.

Uploaded by

Mari Valdes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views16 pages

Primeros Pasos en SQL Server

This document provides steps to create a C# console application that connects to a SQL Server database using Entity Framework code-first approach. It describes creating User and Task model classes, adding Entity Framework dependencies via NuGet, generating a database based on these models, and performing basic CRUD operations on user and task data.

Uploaded by

Mari Valdes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Step 1.

1 Install SQL Server

Step 2.1 Create a C# app that connects to SQL Server


and executes queries
Create a C# console application

1. Launch Visual Studio Community


2. Click File -> New -> Project
3. In the New project dialog, click Windows located under Visual C# in the
Templates node
4. Click Console Application Visual C#
5. Name the project “SqlServerSample”
6. Click OK to create the project

Visual Studio creates a new C# Console Application project and opens the file Program.cs.
Replace the contents of Program.cs by copying and pasting the code below into the file.
Don’t forget to replace the username and password with your own. Save and close the file.

using System;
using System.Text;
using System.Data.SqlClient;
namespace SqlServerSample
{
class Program
{
static void Main(string[] args)
{
try
{
// Build connection string
SqlConnectionStringBuilder builder = new
SqlConnectionStringBuilder();
builder.DataSource = "localhost"; // update me
builder.UserID = "sa"; // update me
builder.Password = "your_password"; // update me
builder.InitialCatalog = "master";

// Connect to SQL
Console.Write("Connecting to SQL Server ... ");
using (SqlConnection connection = new
SqlConnection(builder.ConnectionString))
{
connection.Open();
Console.WriteLine("Done.");
}
}
catch (SqlException e)
{
Console.WriteLine(e.ToString());
}

Console.WriteLine("All done. Press any key to finish...");


Console.ReadKey(true);
}
}
}
Press F5 to build and run the project.

Now replace the code in Program.cs by copying and pasting the code below into the file. This will
create a database and a table, and will insert, update, delete, and read a few rows. Don’t forget to
update the username and password with your own. Save and close the file.
using System;
using System.Text;
using System.Data.SqlClient;

namespace SqlServerSample
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Connect to SQL Server and demo Create,
Read, Update and Delete operations.");

// Build connection string


SqlConnectionStringBuilder builder = new
SqlConnectionStringBuilder();
builder.DataSource = "localhost"; // update me
builder.UserID = "sa"; // update me
builder.Password = "your_password"; // update me
builder.InitialCatalog = "master";

// Connect to SQL
Console.Write("Connecting to SQL Server ... ");
using (SqlConnection connection = new
SqlConnection(builder.ConnectionString))
{
connection.Open();
Console.WriteLine("Done.");

// Create a sample database


Console.Write("Dropping and creating database
'SampleDB' ... ");
String sql = "DROP DATABASE IF EXISTS [SampleDB];
CREATE DATABASE [SampleDB]";
using (SqlCommand command = new SqlCommand(sql,
connection))
{
command.ExecuteNonQuery();
Console.WriteLine("Done.");
}

// Create a Table and insert some sample data


Console.Write("Creating sample table with data, press
any key to continue...");
Console.ReadKey(true);
StringBuilder sb = new StringBuilder();
sb.Append("USE SampleDB; ");
sb.Append("CREATE TABLE Employees ( ");
sb.Append(" Id INT IDENTITY(1,1) NOT NULL PRIMARY
KEY, ");
sb.Append(" Name NVARCHAR(50), ");
sb.Append(" Location NVARCHAR(50) ");
sb.Append("); ");
sb.Append("INSERT INTO Employees (Name, Location)
VALUES ");
sb.Append("(N'Jared', N'Australia'), ");
sb.Append("(N'Nikita', N'India'), ");
sb.Append("(N'Tom', N'Germany'); ");
sql = sb.ToString();
using (SqlCommand command = new SqlCommand(sql,
connection))
{
command.ExecuteNonQuery();
Console.WriteLine("Done.");
}

// INSERT demo
Console.Write("Inserting a new row into table, press
any key to continue...");
Console.ReadKey(true);
sb.Clear();
sb.Append("INSERT Employees (Name, Location) ");
sb.Append("VALUES (@name, @location);");
sql = sb.ToString();
using (SqlCommand command = new SqlCommand(sql,
connection))
{
command.Parameters.AddWithValue("@name", "Jake");
command.Parameters.AddWithValue("@location",
"United States");
int rowsAffected = command.ExecuteNonQuery();
Console.WriteLine(rowsAffected + " row(s)
inserted");
}

// UPDATE demo
String userToUpdate = "Nikita";
Console.Write("Updating 'Location' for user '" +
userToUpdate + "', press any key to continue...");
Console.ReadKey(true);
sb.Clear();
sb.Append("UPDATE Employees SET Location = N'United
States' WHERE Name = @name");
sql = sb.ToString();
using (SqlCommand command = new SqlCommand(sql,
connection))
{
command.Parameters.AddWithValue("@name",
userToUpdate);
int rowsAffected = command.ExecuteNonQuery();
Console.WriteLine(rowsAffected + " row(s)
updated");
}

// DELETE demo
String userToDelete = "Jared";
Console.Write("Deleting user '" + userToDelete + "',
press any key to continue...");
Console.ReadKey(true);
sb.Clear();
sb.Append("DELETE FROM Employees WHERE Name =
@name;");
sql = sb.ToString();
using (SqlCommand command = new SqlCommand(sql,
connection))
{
command.Parameters.AddWithValue("@name",
userToDelete);
int rowsAffected = command.ExecuteNonQuery();
Console.WriteLine(rowsAffected + " row(s)
deleted");
}

// READ demo
Console.WriteLine("Reading data from table, press any
key to continue...");
Console.ReadKey(true);
sql = "SELECT Id, Name, Location FROM Employees;";
using (SqlCommand command = new SqlCommand(sql,
connection))
{

using (SqlDataReader reader =


command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine("{0} {1} {2}",
reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
}
}
}
}
}
catch (SqlException e)
{
Console.WriteLine(e.ToString());
}

Console.WriteLine("All done. Press any key to finish...");


Console.ReadKey(true);
}
}
}

Press F5 to build and run your project.


Step 2.2 Create a C# app that connects to SQL Server
using the Entity Framework ORM in .NET Framework
Create a C# console application

1. Launch Visual Studio Community


2. Click File -> New -> Project
3. In the New project dialog, click Windows located under Visual C# in the
Templates node
4. Click Console Application Visual C#
5. Name the project “SqlServerEFSample”
6. Click OK to create the project

Visual Studio creates a new C# Console Application project and opens the file Program.cs.

Add Entity Framework dependencies to your project

1. Open the Package Manager Console in Visual Studio with “Tools -> Nuget Package
Manager -> Package Manager Console”
2. Type: “Install-Package EntityFramework”
3. Hit enter
Close the Package Manager Console. You have successfully added the required Entity
Framework dependencies to your project.

For this sample, let’s create two tables. The first will hold data about “users” and the other
will hold data about “tasks”.

Create User.cs:

1. Click Project -> Add Class


2. Type “User.cs” in the name field
3. Click Add to add the new class to your project

Copy and paste the following code into the User.cs file. Save and close the file.

using System;
using System.Collections.Generic;

namespace SqlServerEFSample
{
public class User
{
public int UserId { get; set; }
public String FirstName { get; set; }
public String LastName { get; set; }
public virtual IList<Task> Tasks { get; set; }

public String GetFullName()


{
return this.FirstName + " " + this.LastName;
}
public override string ToString()
{
return "User [id=" + this.UserId + ", name=" +
this.GetFullName() + "]";
}
}
}
Create Task.cs:

1. Click Project -> Add Class


2. Type “Task.cs” in the name field
3. Click Add to add the new class to your project

Copy and paste the following code into the Task.cs file. Save and close the file.

using System;

namespace SqlServerEFSample
{
public class Task
{
public int TaskId { get; set; }
public string Title { get; set; }
public DateTime DueDate { get; set; }
public bool IsComplete { get; set; }
public virtual User AssignedTo { get; set; }

public override string ToString()


{
return "Task [id=" + this.TaskId + ", title=" + this.Title +
", dueDate=" + this.DueDate.ToString() + ", IsComplete=" +
this.IsComplete + "]";
}
}
}

Create EFSampleContext.cs:

1. Click Project -> Add Class


2. Type “EFSampleContext.cs” in the name field
3. Click Add to add the new class to your project

Copy and paste the following code into the EFSampleContext.cs file. Save and close the
file.

using System;
using System.Data.Entity;

namespace SqlServerEFSample
{
public class EFSampleContext : DbContext
{
public EFSampleContext(string connectionString)
{
Database.SetInitializer<EFSampleContext>(new
DropCreateDatabaseAlways<EFSampleContext>());
this.Database.Connection.ConnectionString = connectionString;
}
public DbSet<User> Users { get; set; }
public DbSet<Task> Tasks { get; set; }
}
}

Replace the code in the Program.cs file in your by copying and pasting the code into the file. Don’t
forget to update the username and password with your own. Save and close the file.
using System;
using System.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace SqlServerEFSample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("** C# CRUD sample with Entity Framework
and SQL Server **\n");
try
{
// Build connection string
SqlConnectionStringBuilder builder = new
SqlConnectionStringBuilder();
builder.DataSource = "localhost"; // update me
builder.UserID = "sa"; // update me
builder.Password = "your_password"; // update me
builder.InitialCatalog = "EFSampleDB";

using (EFSampleContext context = new


EFSampleContext(builder.ConnectionString))
{
Console.WriteLine("Created database schema from C#
classes.");

// Create demo: Create a User instance and save it to


the database
User newUser = new User { FirstName = "Anna",
LastName = "Shrestinian" };
context.Users.Add(newUser);
context.SaveChanges();
Console.WriteLine("\nCreated User: " +
newUser.ToString());

// Create demo: Create a Task instance and save it to


the database
Task newTask = new Task() { Title = "Ship Helsinki",
IsComplete = false, DueDate = DateTime.Parse("04-01-2017") };
context.Tasks.Add(newTask);
context.SaveChanges();
Console.WriteLine("\nCreated Task: " +
newTask.ToString());

// Association demo: Assign task to user


newTask.AssignedTo = newUser;
context.SaveChanges();
Console.WriteLine("\nAssigned Task: '" +
newTask.Title + "' to user '" + newUser.GetFullName() + "'");

// Read demo: find incomplete tasks assigned to user


'Anna'
Console.WriteLine("\nIncomplete tasks assigned to
'Anna':");
var query = from t in context.Tasks
where t.IsComplete == false &&
t.AssignedTo.FirstName.Equals("Anna")
select t;
foreach (var t in query)
{
Console.WriteLine(t.ToString());
}

// Update demo: change the 'dueDate' of a task


Task taskToUpdate = context.Tasks.First(); // get the
first task
Console.WriteLine("\nUpdating task: " +
taskToUpdate.ToString());
taskToUpdate.DueDate = DateTime.Parse("06-30-2016");
context.SaveChanges();
Console.WriteLine("dueDate changed: " +
taskToUpdate.ToString());

// Delete demo: delete all tasks with a dueDate in


2016
Console.WriteLine("\nDeleting all tasks with a
dueDate in 2016");
DateTime dueDate2016 = DateTime.Parse("12-31-2016");
query = from t in context.Tasks
where t.DueDate < dueDate2016
select t;
foreach (Task t in query)
{
Console.WriteLine("Deleting task: " +
t.ToString());
context.Tasks.Remove(t);
}
context.SaveChanges();

// Show tasks after the 'Delete' operation - there


should be 0 tasks
Console.WriteLine("\nTasks after delete:");
List <Task> tasksAfterDelete = (from t in
context.Tasks select t).ToList <Task> ();
if (tasksAfterDelete.Count == 0)
{
Console.WriteLine("[None]");
}
else
{
foreach (Task t in query)
{
Console.WriteLine(t.ToString());
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}

Console.WriteLine("All done. Press any key to finish...");


Console.ReadKey(true);
}
}
}
Press F5 to build and run the project.

Congrats you just created two C# apps! Check out the next section to learn about how you
can make your C# apps faster with SQL Server’s Columnstore feature.

3 Make your C# app up to 100x faster

Now that you have explored the basics, you are ready to see how you can make your app better
with SQL Server. In this module we will show you a simple example of Columnstore Indexes and
how they can improve data processing speeds. Columnstore Indexes can achieve up to 100x better
performance on analytical workloads and up to 10x better data compression than traditional
rowstore indexes.

Step 3.1
Note! With the introduction of Service Pack 1 for SQL Server 2016, features in the
database engine related to application development are now available across all editions of
SQL Server (from Express through Enterprise). This includes innovations that can
significantly improve your application’s throughput, latency, and security. Examples
include the in-memory columnstore used in this tutorial, in-memory OLTP, data
compression, table partitioning, Hadoop integration with PolyBase, Always Encrypted,
row-level security, and data masking. Enjoy!

To showcase the capabilities of Columnstore indexes, let’s create a C# application that


creates a sample database and a sample table with 5 million rows and then runs a simple
query before and after adding a Columnstore index.

Create a C# console application

1. Launch Visual Studio Community


2. Click File -> New -> Project
3. In the New project dialog, click Windows located under Visual C# in the
Templates node
4. Click Console Application Visual C#
5. Name the project “SqlServerColumnstoreSample”
6. Click OK to create the project

Visual Studio creates a new C# Console Application project and opens the file Program.cs.
Replace the contents of Program.cs by copying and pasting the code below into the file.
Don’t forget to replace the username and password with your own. Save and close the file.

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SqlServerColumnstoreSample
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("*** SQL Server Columnstore demo ***");

// Build connection string


SqlConnectionStringBuilder builder = new
SqlConnectionStringBuilder();
builder.DataSource = "localhost"; // update me
builder.UserID = "sa"; // update me
builder.Password = "your_password"; // update me
builder.InitialCatalog = "master";
// Connect to SQL
Console.Write("Connecting to SQL Server ... ");
using (SqlConnection connection = new
SqlConnection(builder.ConnectionString))
{
connection.Open();
Console.WriteLine("Done.");

// Create a sample database


Console.Write("Dropping and creating database
'SampleDB' ... ");
String sql = "DROP DATABASE IF EXISTS [SampleDB];
CREATE DATABASE [SampleDB]";
using (SqlCommand command = new SqlCommand(sql,
connection))
{
command.ExecuteNonQuery();
Console.WriteLine("Done.");
}

// Insert 5 million rows into the table


'Table_with_5M_rows'
Console.Write("Inserting 5 million rows into table
'Table_with_5M_rows'. This takes ~1 minute, please wait ... ");
StringBuilder sb = new StringBuilder();
sb.Append("USE SampleDB; ");
sb.Append("WITH a AS (SELECT * FROM (VALUES(1),(2),
(3),(4),(5),(6),(7),(8),(9),(10)) AS a(a))");
sb.Append("SELECT TOP(5000000)");
sb.Append("ROW_NUMBER() OVER (ORDER BY a.a) AS
OrderItemId ");
sb.Append(",a.a + b.a + c.a + d.a + e.a + f.a + g.a +
h.a AS OrderId ");
sb.Append(",a.a * 10 AS Price ");
sb.Append(",CONCAT(a.a, N' ', b.a, N' ', c.a, N' ',
d.a, N' ', e.a, N' ', f.a, N' ', g.a, N' ', h.a) AS ProductName ");
sb.Append("INTO Table_with_5M_rows ");
sb.Append("FROM a, a AS b, a AS c, a AS d, a AS e, a
AS f, a AS g, a AS h;");
sql = sb.ToString();
using (SqlCommand command = new SqlCommand(sql,
connection))
{
command.ExecuteNonQuery();
Console.WriteLine("Done.");
}

// Execute SQL query without columnstore index


double elapsedTimeWithoutIndex =
SumPrice(connection);
Console.WriteLine("Query time WITHOUT columnstore
index: " + elapsedTimeWithoutIndex + "ms");

// Add a Columnstore Index


Console.Write("Adding a columnstore to table
'Table_with_5M_rows' ... ");
sql = "CREATE CLUSTERED COLUMNSTORE INDEX
columnstoreindex ON Table_with_5M_rows;";
using (SqlCommand command = new SqlCommand(sql,
connection))
{
command.ExecuteNonQuery();
Console.WriteLine("Done.");
}

// Execute the same SQL query again after columnstore


index was added
double elapsedTimeWithIndex = SumPrice(connection);
Console.WriteLine("Query time WITH
columnstore index: " + elapsedTimeWithIndex + "ms");

// Calculate performance gain from adding columnstore


index
Console.WriteLine("Performance
improvement with columnstore index: "
+
Math.Round(elapsedTimeWithoutIndex / elapsedTimeWithIndex) + "x!");
}
Console.WriteLine("All done. Press any key to
finish...");
Console.ReadKey(true);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}

public static double SumPrice(SqlConnection connection)


{
String sql = "SELECT SUM(Price) FROM Table_with_5M_rows";
long startTicks = DateTime.Now.Ticks;
using (SqlCommand command = new SqlCommand(sql, connection))
{
try
{
var sum = command.ExecuteScalar();
TimeSpan elapsed =
TimeSpan.FromTicks(DateTime.Now.Ticks) - TimeSpan.FromTicks(startTicks);
return elapsed.TotalMilliseconds;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
return 0;
}
}
}
Press F5 to build and run your project.

The performance of the query was greatly improved! Now that you’ve built a few C# apps with
SQL Server and .NET Core, continue checking out other SQL Server features.

Try the mssql extension for Visual Studio Code!


Use the mssql extension for VS Code to connect to SQL Server running in Linux,
Windows or Docker, Azure SQL DB and Azure SQL DW. … See more Get rich T-SQL
language services (ex. IntelliSense and keyword completion), while typing queries and run
your queries to see results – all within VS Code and on Linux, Mac and Windows.

Follow the steps here to install and use the mssql extension for VS Code.

You might also like