Linq存储过程插入没有结果

时间:2013-01-31 10:56:46

标签: .net vb.net linq linq-to-sql stored-procedures

我尝试使用Linq To SQL插入带有存储过程的数据。 我没有收到任何错误消息,我的数据也没有插入。 代码在vb中,因为我也在这个项目中使用xml。

代码

        Using db As New booksDataContext()

        db.testprocedure("kkk")
        db.SubmitChanges()

        End Using

存储过程

ALTER PROCEDURE testprocedure

(
@test varchar(50)
)

AS
BEGIN
INSERT INTO test (testcolum)
VALUES (@test)
END

我可以在调试时进入此功能并返回0

<Global.System.Data.Linq.Mapping.FunctionAttribute(Name:="dbo.testprocedure")>  _
Public Function testprocedure(<Global.System.Data.Linq.Mapping.ParameterAttribute(DbType:="VarChar(50)")> ByVal test As String) As Integer
    Dim result As IExecuteResult = Me.ExecuteMethodCall(Me, CType(MethodInfo.GetCurrentMethod,MethodInfo), test)
    Return CType(result.ReturnValue,Integer)
End Function

我已经完成了一个可以使用perfekt的Select,所以它没有错误的datacontext

  Sub SelectAllBooks()

    Dim db As New booksDataContext()

    For Each book In db.SelectBooks("Madde")

        Dim title = book.title
        Dim author = book.author
        Dim genre = book.genre
        Dim price = book.price
        Dim publish_date = book.publish_date
        Dim description = book.description
        Dim bookid = book.bookid


        Console.WriteLine(title & " " & author & " " & genre & " " & price & " " & publish_date & " " & description & " " & bookid)

    Next
    Console.ReadLine()
End Sub

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

检查这是一个非常好的教程:

LINQ to SQL (Updating our Database using Stored Procedures)

另外,如果值正确插入,请检查test表。

除非另有说明,否则所有系统存储过程都返回值0.这表示成功,非零值表示失败。

RETURN (Transact-SQL)

答案 1 :(得分:0)

你的桌子上有一把主钥匙吗?这是我能够通过LINQ获取INSERT / UPDATE / DELETE方法的唯一方法。