-4

I am trying to learn more about web development with C# and ASP.Net.

I am using LINQ to store values from a contact form in a database, however, the database also requires a 'Policy ID' that is generated by calling a stored procedure.

I have absolutely no idea how to about calling the stored procedure, obtaining the integer value generated and then using that value to set PolicyID in the table.

Could anybody provide me with a process that will work like so:

Execute SPROC, return POLICY ID. Get PolicyID, set as PolicyNo. add PolicyNo to db.PolicyID

I appreciate some of this information may appear quite amateurish, but I have never used LINQ nor Stored Procedures before. I have been a front end developer up until now.

Thanks in advance!

EDIT:

Here is the C# code used so far:

using (MagTestDataContext context = new MagTestDataContext())
    {
        //create new instance of tblPolicy object
        tblPolicy policy = new tblPolicy();
        var policyNo = context.spNextPolicyID();
        //add values to field
        policy.PolicyID = policyNo;
        policy.PolicyHolder = tbName.Text;
        policy.EMail = tbEmail.Text;
        policy.Telephone = tbTelephone.Text;
        policy.Address1 = tbAddressLine1.Text;
        policy.Address2 = tbAddressLine2.Text;
        policy.Address3 = tbAddressLine3.Text;
        policy.Address4 = tbAddressLine4.Text;
        policy.PostCode = tbPostcode.Text;
        policy.ExtraInfo10 = tbInsurer.Text;
        policy.ExtraInfo11 = ddReason.Text;
        policy.ExtraInfo12 = tbOther.Text;
    }
4

1 回答 1

0

看起来您正在使用实体框架。如果是这样,那么您可以使用以下命令执行存储过程:

using (MagTestDataContext context = new MagTestDataContext())
{
    var policyid = db.ExecuteQuery<int>("DECLARE @ret INT; EXEC spNextPolicyID @ret OUTPUT; SELECT @ret").Single();
}
于 2013-04-18T12:37:23.443 回答