Posts

Showing posts with the label sql server

How to View SQL Queries Generated by Entity Framework

Image
Entity Framework works by translating C# code into SQL statements, executing those statements on a target database, and taking care of mapping any return values back to C# objects. But have you ever wondered what the generated SQL statements actually look like? In this post we will be using a free and simple tool that lets us view the SQL executed on the database.

On Validation and Database Design

Image
SQL Server lets us design tables which have constraints and checks in them. Some examples of these constraints / checks are primary key constraints, nullability flags, maximum length flags, and so on. These checks are used to ensure data integrity. Insert, update, and delete commands which will result in an invalid state when executed are not allowed. However, there are some advantages to not using these checks. In this post, we will talk about some of those advantages.

Adding an SQL Azure Linked Server

Image
When adding an SQL Azure Linked Server through SSMS, you will encounter an error if you go through the normal way (Server Objects > Linked Servers > New Linked Server). That is because the New Linked Server wizard does not allow you to specify the catalog name if the chosen Server Type is SQL Server, which should be chosen when adding an SQL Azure Linked Server. To accomplish this, the stored procedure sp_addlinkedserver should be used.

Enabling SQL Server Authentication after Installation

Image
If you install SQL Server using the default options, Windows authentication will be enabled and SQL Server authentication will be disabled. This post discusses how to enable SQL Server Authentication after installation and also enable the sa login.

Using XML with SQL Server

Image
Sometimes there is a requirement to insert multiple values in the database. This can be done using bulk insert, table-valued functions, through XML, or simply by calling an insert procedure that inserts a single item multiple times. In this post we will be taking a look at how to do multiple inserts using a single query through XML using SQL Server 2008.

Resetting the Primary Key of a Table

Image
This quick tip will show how to reset the primary key values for a table in SQL Server: DBCC CHECKIDENT ([Table], RESEED, [NewCurrentValue]) Where [Table] is the table name and [NewCurrentValue] is the new current value of the id. This would ideally be set to 0, so that the next value (when a row is inserted) will be 1.

Simple Ways to Prevent SQL Injection Attacks

Image
Recently I've been working on a library that would allow direct CRUD operations of any object to the database. The library would detect the types and values of the properties of the classes involved then create Insert, Update, GetAll, and GetById procedures depending on what they find. It was working great and I was happy with the result. However when I showed it to a friend he pointed out that there might be security concern for building such queries. I researched about it a little and I found he was correct: my code was vulnerable to SQL injection attacks.