Stored Procedure with LINQ for Insert
How to Use Stored Procedure in LINQ for Insert
1. Crate the table for Example "User_Tbl" such as
2. Create the a Web Application
Right click on project ->Add New Item-> select LINQ to SQL Class and give the name to TestDataClasses click on OK under the App_Code Create a file TestDataClasses.dbml with extension .dbml double click on it.
3. Connect To Database in Visual Studio for this go to Tool->Connect To DataBase after connecting you can see all the database objects in Server Explorer.
Drag and Drop the required objects (such as Table, Stored Procedure etc. on TestDataClasses.dbml.
Save the TestDataClasses.dbml now you can get the DB objects in your page the TestDataClasses show such as
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
public partial class LinqStoredProcedure
: System.Web.UI.Page
{
TestDataClassesDataContext objDBContext = new TestDataClassesDataContext(ConfigurationManager.ConnectionStrings["TestConnectionString"].ToString());
protected void
Page_Load(object sender, EventArgs e)
{
}
protected void btnSave_Click(object
sender, EventArgs e)
{
int? userId = null;
//Stored procedure To insert the record into Table
objDBContext.procInsertUserDetails(txtUserName.Text.Trim(),
txtPassword.Text.Trim(), txtFirstName.Text, txtLastName.Text, txtAddress.Text,
txtCity.Text
, txtState.Text, txtZip.Text,
txtPhone.Text, txtFax.Text, txtUserEmail.Text, true,
DateTime.Now, "admin",
ref userId);
objDBContext.SubmitChanges();
}
}
in the above Example TestDataClassesDataContext is TestDataClasses.dbml file.
Comments
Post a Comment