Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Last active March 27, 2025 23:09
Show Gist options
  • Save karenpayneoregon/a99298e67212e5baa0159b19660e8a3d to your computer and use it in GitHub Desktop.
Save karenpayneoregon/a99298e67212e5baa0159b19660e8a3d to your computer and use it in GitHub Desktop.
GitHub Copilot statements to stored procedures
// create a method in DapperOperations.cs named GetImageById using Dapper
public class DapperOperations
{
private readonly string _connectionString;
public DapperOperations(string connectionString)
{
_connectionString = connectionString;
}
public async Task<Image> GetImageById(int id)
{
using (var connection = new SqlConnection(_connectionString))
{
var query = SqlStatements.UspSelectImageByIdentifier;
var parameters = new { id };
return await connection.QueryFirstOrDefaultAsync<Image>(query, parameters);
}
}
}
public class Image
{
public int Id { get; set; }
public byte[] Photo { get; set; }
public string Description { get; set; }
}
create a stored procedure for each with a prefix of usp_ for SQL-Server and keep the originals
public class SqlStatements
{
public static string InsertImage =>
"""
INSERT INTO [dbo].[Pictures1]
(
[Photo],
Description
)
VALUES
(
@ByteArray,
@Description
)
""";
public static string SelectImageByIdentifier =>
"""
SELECT
id,
Photo,
Description
FROM
dbo.Pictures1
WHERE
dbo.Pictures1.id = @id;
""";
public static string ReadAllImages =>
"""
SELECT
Id,
Photo,
[Description]
FROM
dbo.Pictures1
""";
}
public class SqlStatements
{
public static string InsertImage =>
"""
INSERT INTO [dbo].[Pictures1]
(
[Photo],
Description
)
VALUES
(
@ByteArray,
@Description
)
""";
public static string SelectImageByIdentifier =>
"""
SELECT
id,
Photo,
Description
FROM
dbo.Pictures1
WHERE
dbo.Pictures1.id = @id;
""";
public static string ReadAllImages =>
"""
SELECT
Id,
Photo,
[Description]
FROM
dbo.Pictures1
""";
public static string UspInsertImage =>
"""
CREATE PROCEDURE usp_InsertImage
@ByteArray VARBINARY(MAX),
@Description NVARCHAR(MAX)
AS
BEGIN
INSERT INTO [dbo].[Pictures1]
(
[Photo],
Description
)
VALUES
(
@ByteArray,
@Description
)
END
""";
public static string UspSelectImageByIdentifier =>
"""
CREATE PROCEDURE usp_SelectImageByIdentifier
@id INT
AS
BEGIN
SELECT
id,
Photo,
Description
FROM
dbo.Pictures1
WHERE
dbo.Pictures1.id = @id;
END
""";
public static string UspReadAllImages =>
"""
CREATE PROCEDURE usp_ReadAllImages
AS
BEGIN
SELECT
Id,
Photo,
[Description]
FROM
dbo.Pictures1;
END
""";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment