Part 1 LINQ to SQL
How to Use LINQ to SQL in C Sharp
LINQ to SQL is a .Net framework component from Microsoft which is used to access to Microsoft SQL Server databases. This article outlines how to set up some basic functionality with LINQ to SQL in C#. It describes basic steps to creating a data context, associating a new class to a database entity, and inserting and querying data from the database.
To demonstrate how to use LINQ to SQL, a database called MyDatabase will be used, and a new table called Employees will be created. You will need to change names and data types to match the requirements of your database.
Note: These instructions do not include any error checking.
Create or open a C# program in Visual Studio.The example uses the Console Application template.
Right click on your program in Solution Explorer.
Add the following lines to the top of the source code:
usingSystem.Linq usingSystem.Data.Linq usingSystem.Data.Linq.Mapping
- Note: Visual Studio may have already added the line for System.Linq.
Create a new DataContext class by entering this code, replacinglogin_stringwith your actual login string:classMyDatabase DataContext privateconstStringLoginString publicMyDatabase LoginString
Add this line to your Main method to create a new instance of the MyDatabase class:
MyDatabase db MyDatabase
- The DataContext instance acts as your connection to SQL Server.
Creating a New Table
Add this line your Main method to create a new table in your database:
dbExecuteCommand"Create table employees. (ID int, Name varchar(50), Department varchar(50))"
- Other SQL commands may be issued in a similar manner, by passing the command string to ExecuteCommand.
Add the follow line to finalize the changes to the database:dbSubmitChanges
Associating a Table with a Class
Add these lines to your source code:
TableName "Employees" publicclassEmployee ColumnIsPrimaryKey public ID Column publicstringName Column publicstringDepartment
- This will define a new class, with theTableattribute indicating that the class represents table data, theNameparameter associating a name for that table, theColumnattribute indicating column names and types, and theIsPrimaryKeyparameter indicating the primary key column.
Inserting Data to the Database
Add this line to the MyDatabase class:publicTableEmployee Employees
In your Main method, create three new rows of data with new instances of the Employee class and filling in the data, as follows:Employee employee1 Employee employee1 employee1 "John Smith" employee1Department"Sales" dbEmployeesInsertOnSubmitemployee1 Employee employee2 Employee employee2 employee2 "Ted Black" employee2Department"Research" dbEmployeesInsertOnSubmitemployee2 Employee employee3 Employee employee3 employee3 "Allen Gottlieb" employee3Department"Sales" dbEmployeesInsertOnSubmitemployee3 dbSubmitChanges
Querying the Database
Know that querying the database can be done within your source code using a syntax which is similar to SQL.
To access the data for everyone in sales, and display their names and ID numbers, use the following lines:salesDept e dbEmployees whereeDepartment"Sales" selecte foreachemployee salesDept ConsoleWriteLineemployee employee
Similar queries can be made using similar code.For instance, to get and display Ted Black's ID number, you could use this code:queryResult e dbEmployees wheree "Ted Black" selecte tedBlack queryResultFirst ConsoleWriteLinetedBlack
Look at the whole thing.This is the entire program shown above.
Video: How to Use Linq To Sql for Connect SQL Database with In C# ?
How to Support a Friend After Surgery
How to Download Audio Books
Caramel Chocolate Chip Is the Newest Girl Scout Cookie
Photo: Self driving Subaru with triple lidar stack in San Francisco
Beautiful Bridal Headpieces from Enchanted Atelier by Liv Hart 2015 – Part 2
Top 22 Choppy Hairstyles You’ll See in 2019
Oral Psoriasis: How the Disease Can Affect Your Mouth
Acne and Depression: More Than Skin Deep
How to Fix a Broken Friendship
How to Act Happy
10 Best Purple Shampoos for Blonde Hair – Reviews and Buyer’s Guide
How a food reporter beat anorexia
Online Support for Families of PML Patients
How To Do Everything Better: Set a Stylish Table That Won’t Break theBank