Part 1 LINQ to SQL



How to Use LINQ to SQL in C Sharp

Six Parts:

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.

Steps

Setting Up

  1. Create or open a C# program in Visual Studio.The example uses the Console Application template.
  2. Right click on your program in Solution Explorer.
  3. ClickAdd Reference...
  4. Under.NET, selectSystem.Data.Linq
  5. ClickOK
  6. 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.
  7. Create a new DataContext class by entering this code, replacinglogin_stringwith your actual login string:
    classMyDatabase DataContext privateconstStringLoginString publicMyDatabase LoginString
  8. 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

  1. 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.
  2. Add the follow line to finalize the changes to the database:
    dbSubmitChanges

Associating a Table with a Class

  1. 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

  1. Add this line to the MyDatabase class:
    publicTableEmployee Employees
  2. 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

  1. Know that querying the database can be done within your source code using a syntax which is similar to SQL.
  2. 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
  3. 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

Summary

  1. 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 Use LINQ to SQL in C Sharp
How to Use LINQ to SQL in C Sharp images

2019 year
2019 year - How to Use LINQ to SQL in C Sharp pictures

How to Use LINQ to SQL in C Sharp recommend
How to Use LINQ to SQL in C Sharp forecasting photo

How to Use LINQ to SQL in C Sharp pictures
How to Use LINQ to SQL in C Sharp picture

How to Use LINQ to SQL in C Sharp How to Use LINQ to SQL in C Sharp new photo
How to Use LINQ to SQL in C Sharp new pics

images How to Use LINQ to SQL in C Sharp
pics How to Use LINQ to SQL in C Sharp

Watch How to Use LINQ to SQL in C Sharp video
Watch How to Use LINQ to SQL in C Sharp video

Forum on this topic: How to Use LINQ to SQL in , how-to-use-linq-to-sql-in/
Forum on this topic: How to Use LINQ to SQL in , how-to-use-linq-to-sql-in/ , how-to-use-linq-to-sql-in/

Related News


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
Oxycodone Reviews
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



Date: 06.12.2018, 17:36 / Views: 54555