Reputation: 47945
I'm trying to use LINQ to SQL Server in C#/.NET on Visual Studio 2010.
As first thing, I've create the Table "Details" into SQL Server; than I create a LINQ to SQL classes on the project, called DataClasses (.dbml).
Well, after setting the connection to the db, I'd like to extract the data from the Details table.
I do this on a WebForm Page_Load :
DataClassesDataContext data = new DataClassesDataContext();
but seems it is the wrong approch? How should I do?
Upvotes: 1
Views: 6378
Reputation: 13600
SomePageAspx.cs
protected void Page_Load(object sender, EventArgs e){
List<MyBook> books = MyBook.GetAllBooks();
}
MyBook.cs
public partial class MyBook
{
public static List<MyBook> GetAllBooks()
{
using (myDBContext db = new myDBContext())
{
var books = from o in db.MyBooks
select o;
return books.ToList();
}
}
}
Just a rough sketch, but it should be enough for you to get the idea. If not, ask.
Upvotes: 1
Reputation: 3595
You will have to create classes for all the tables and methods for all the stored procedures in the database; for this you can open your server explorer and just drag and drop tables and stored procs. in the .dbml file after you open the .dbml file in visual studio. After that visual studio will create all the necessary codes to get data from your database; you can then just call any one of the stored proc as a method of your DataClassesDataContext class or you can write a query. I prefer calling stored procs, so if you had a stored proc named "GetEmployeeRecord" in your database you would do something like:
var empRec = data.GetEmployeeRecord('your_parameters');
Hope this helps :)
Upvotes: 1