Then, we ‘ll use these entities in our project to query CRUD operations. Create a new C# Console Application named Crud Operations With Linq Sql in your Visual Studio. In my pc, it’s under the You may have to change your server name and the location C:\Sql Metal\DAL I used to store the new file. Open the Northwind.file and see that not only your entities are mapped to your tables, but all relationships information have been created too. Add a new C# class file named “Queries” in your project. public static void Create Order(string customer ID, int employee ID, Date Time order Date, Date Time required Date, Date Time shipped Date, int ship Via, decimal freight, string ship Name, string ship Address, string ship City, string ship Region, string ship Postal Code, string ship Country) Either call three times the Create Order method or run the above code three times in a row to create some orders for the Customer with Customer ID=”CHRST”. It’s normal though, considering the fact that this customer is associated with three orders. We need to call the Delete All On Submit function on the Orders table, passing as a parameter our customer’s orders. Right click your project and add a reference to the System. The file will contain a North Wind Data Context class derived from the System. Nice, now let’s try to delete our Customer with “Customer ID=CHRST”. These orders have a foreign key Customer ID pointing to the customer we want to delete. Change the above Delete Customer function’s code like this. So i have decided to create one simple example with LINQ-To-SQL and also decide to blog about it.
First i have created a simple table called which have 4 fields like Product Id, Name, Description and Price. Here is the description for each fields LINQ to SQL Classes like following.
Then open Database Explorer and drag and drop Product table on the newly generated LINQ-to-SQL Class like following. Now we will insert some data to our table and then first we will write code to get all product information from the database.
You use it in the same way that you would use an ADO. In fact, the Data Context is initialized with a connection or connection string you supply.
The purpose of the Data Context is to translate your requests for objects into SQL queries made against the database and then assemble objects out of the results.
Hitting the save button will invoke the method Save Customer Info() which is responsible for doing the insert operation.
Now if we look at the database we can see that the data we entered was successfully being saved to the database.LINQ to SQL is an O/RM (object relational mapping) implementation that ships in the .NET Framework "Orcas" release, and which allows you to model a relational database using . You can then query the database using LINQ, as well as update/insert/delete data from it.The Data Context enables language-integrated query by implementing the same operator pattern as the standard query operators such as Where and Select As you can see, the code above was very straight forward.First we have created a new instance of the Data Context which we had created on STEP 2 and wrapped it inside the “using” block; this is to ensure that the Data Context will be disposed after its processing.CRUD stands for CREATE, READ, UPDATE, DELETE and corresponds to the SQL statements INSERT, SELECT, UPDATE and DELETE respectively.