简体   繁体   中英

Use stored procedure in c# using LINQ to SQL

I'm looking for how to use a stored procedure using Linq-to-SQL in C#.

This is my stored procedure:

ALTER PROCEDURE [dbo].[uploadImage] @ppr INT,
    @imagename VARCHAR,
    @imagecontent VARCHAR,
    @imagebinary IMAGE
AS
BEGIN TRANSACTION

IF EXISTS (
        SELECT ImageID
        FROM [ImageStorage]
        WHERE ImageID = (
                SELECT codeimg
                FROM Agent
                WHERE PPR = @ppr
                )
        )
BEGIN
    --select ImageBinary from [ImageStorage] where ImageID = ( select codeimg from Agent where PPR=@ppr) 
    UPDATE ImageStorage
    SET ImageName = @imagename,
        ImageContentType = @imagecontent,
        ImageBinary = @imagecontent
    WHERE imageID = (
            SELECT codeimg
            FROM Agent
            WHERE PPR = @ppr
            )
END
ELSE
    INSERT INTO ImageStorage (
        ImageName,
        ImageContentType,
        ImageBinary
        )
    VALUES (
        @imagename,
        @imagecontent,
        @imagebinary
        )

COMMIT

Thank you

Use this to convert the image:

using (MemoryStream ms = new MemoryStream())
{
     image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
     var binary = new System.Data.Linq.Binary(ms.GetBuffer());
}

pass "binary" to your method as "imagebinary" parameter

You can drag and drop your stored procedure from the designer and it will be available in your data context.

From Microsoft:

To create DataContext methods that return automatically generated types

  1. In Server Explorer/Database Explorer , expand the Stored Procedures node of the database you are working with.
  2. Locate the desired stored procedure and drag it onto an empty area of the O/R Designer.

The DataContext method is created with an automatically generated return type and appears in the Methods pane.

To create DataContext methods that have the return type of an entity class

  1. In Server Explorer/Database Explorer , expand the Stored Procedures node of the database you are working with.
  2. Locate the desired stored procedure and drag it onto an existing entity class in the O/R Designer.

The DataContext method is created with the return type of the selected entity class and appears in the Methods pane.


Secondly consider changing your proc to this:

 ALTER PROCEDURE [dbo].[uploadImage] @ppr INT, @imagename VARCHAR, @imagecontent VARCHAR, @imagebinary IMAGE AS BEGIN UPDATE ImageStorage SET ImageName = @imagename, ImageContentType = @imagecontent, ImageBinary = @imagecontent WHERE @ppr IS NOT NULL AND imageID = ( SELECT codeimg FROM Agent WHERE PPR = @ppr ) IF @@ROWCOUNT = 0 INSERT INTO ImageStorage ( ImageName, ImageContentType, ImageBinary ) VALUES ( @imagename, @imagecontent, @imagebinary ) END 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM