如何使用存储过程从linq 2 sql将数据插入到两个表中

时间:2022-08-29 16:42:01

I using linq 2 sql with the asp.net mvc. I am having two table schedular and schdulerhistory and I am having a stored procedure which will insert data in this two table with one transaction.

我使用linq 2 sql与asp.net mvc。我有两个表调度和schdulerhistory,我有一个存储过程,它将在这两个表中插入一个事务的数据。

I want to use that stored procedure with the linq. How i can accomplish this things with linq 2 sql.

我想将该存储过程与linq一起使用。我如何用linq 2 sql完成这些事情。

3 个解决方案

#1


One stored procedure with two inserts:

一个存储过程有两个插入:

CREATE PROCEDURE YouProcedureName
(
     @Params1         char(2)
    ,@Params2         int
    ,@Params3         varchar(10)
)
AS 

INSERT INTO YourTable1
        (Col1    , Col2)
    VALUES
        (@Parms1 , @Params2)


INSERT INTO YourTable2
        (Col1    , Col3)
    VALUES
        (@Parms1 , @Params3)


GO

how to call a stored procedure from LINQ:

如何从LINQ调用存储过程:

http://www.mssqltips.com/tip.asp?tip=1542

http://www.google.com/search?hl=en&as_q=linq+to+sql%2C+how+to+call+a+stored+procedure&as_epq=&as_oq=&as_eq=&num=100&lr=&as_filetype=&ft=i&as_sitesearch=&as_qdr=all&as_rights=&as_occt=any&cr=&as_nlo=&as_nhi=&safe=images

#2


Scott Gu has an article on using stored procedures with LINQ to SQL.

Scott Gu有一篇关于在LINQ to SQL中使用存储过程的文章。

#3


In your server explorer, drag your stored procedure into your DBML file.

在服务器资源管理器中,将存储过程拖到DBML文件中。

Once this is done, you can create an instance of your DataContext object and call that stored procedure directly as if it were a class level method.

完成此操作后,您可以创建DataContext对象的实例并直接调用该存储过程,就像它是类级方法一样。

For example:

MyDataContext db = new MyDataContext();

MyDataContext db = new MyDataContext();

var storedProcedureResultSet = db.NameOfMyStoredProcedure(parameter);

var storedProcedureResultSet = db.NameOfMyStoredProcedure(parameter);

#1


One stored procedure with two inserts:

一个存储过程有两个插入:

CREATE PROCEDURE YouProcedureName
(
     @Params1         char(2)
    ,@Params2         int
    ,@Params3         varchar(10)
)
AS 

INSERT INTO YourTable1
        (Col1    , Col2)
    VALUES
        (@Parms1 , @Params2)


INSERT INTO YourTable2
        (Col1    , Col3)
    VALUES
        (@Parms1 , @Params3)


GO

how to call a stored procedure from LINQ:

如何从LINQ调用存储过程:

http://www.mssqltips.com/tip.asp?tip=1542

http://www.google.com/search?hl=en&as_q=linq+to+sql%2C+how+to+call+a+stored+procedure&as_epq=&as_oq=&as_eq=&num=100&lr=&as_filetype=&ft=i&as_sitesearch=&as_qdr=all&as_rights=&as_occt=any&cr=&as_nlo=&as_nhi=&safe=images

#2


Scott Gu has an article on using stored procedures with LINQ to SQL.

Scott Gu有一篇关于在LINQ to SQL中使用存储过程的文章。

#3


In your server explorer, drag your stored procedure into your DBML file.

在服务器资源管理器中,将存储过程拖到DBML文件中。

Once this is done, you can create an instance of your DataContext object and call that stored procedure directly as if it were a class level method.

完成此操作后,您可以创建DataContext对象的实例并直接调用该存储过程,就像它是类级方法一样。

For example:

MyDataContext db = new MyDataContext();

MyDataContext db = new MyDataContext();

var storedProcedureResultSet = db.NameOfMyStoredProcedure(parameter);

var storedProcedureResultSet = db.NameOfMyStoredProcedure(parameter);