---
title: "Use stored procedure in c# using LINQ to SQL"  
description: "Use stored procedure in c# using LINQ to SQL"  
author: "Anonymous User"  
published: 2014-12-11  
updated: 2014-12-11  
canonical: https://www.mindstick.com/forum/12783/use-stored-procedure-in-c-sharp-using-linq-to-sql  
category: "asp.net"  
tags: ["linq", "sql server", "linq to sql"]  
reading_time: 2 minutes  

---

# Use stored procedure in c# using LINQ to SQL

I'm looking for how to use a [stored procedure](https://www.mindstick.com/articles/803/using-stored-procedure-in-asp-dot-net) using [Linq](https://www.mindstick.com/articles/12007/language-integrated-query-linq-queries)-to-[SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) in C#.

This is my stored procedure:

```
ALTER PROCEDURE [dbo].[uploadImage] @ppr INT,    @imagename VARCHAR,    @imagecontent VARCHAR,    @imagebinary IMAGEASBEGIN TRANSACTIONIF 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            )ENDELSE    INSERT INTO
ImageStorage (        ImageName,       
ImageContentType,        ImageBinary        )    VALUES (        @imagename,        @imagecontent,        @imagebinary        )COMMIT
```

Thank you

## Replies

### Reply by Anonymous User

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

### Reply by Anonymous User

consider changing your proc to this:

```
ALTER PROCEDURE [dbo].[uploadImage] @ppr INT,    @imagename VARCHAR,    @imagecontent VARCHAR,    @imagebinary IMAGEASBEGIN    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
```


---

Original Source: https://www.mindstick.com/forum/12783/use-stored-procedure-in-c-sharp-using-linq-to-sql

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
