Skip to content

Commit 86727f8

Browse files
committed
completed Dapper sample
1 parent 133ef08 commit 86727f8

File tree

4 files changed

+58
-10
lines changed

4 files changed

+58
-10
lines changed

DotNet/Dapper/Model.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ public class Post
1414
public int PostId { get; set; }
1515
public string Title { get; set; } = string.Empty;
1616
public string Content { get; set; } = string.Empty;
17-
// We'll use SqlVector<float> to represent the SQL Server vector type when passing to SqlClient
18-
public SqlVector<float>? Embedding { get; set; }
17+
public float[]? Embedding { get; set; }
1918
public int BlogId { get; set; }
2019
}
2120

DotNet/Dapper/Program.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@
88
// Load .env
99
Env.Load();
1010

11+
// Update Dapper Type Handler to handle SqlVector<T>
12+
SqlMapper.AddTypeHandler(new VectorTypeHandler());
13+
14+
// Get connection string from environment variable
1115
var connectionString = Environment.GetEnvironmentVariable("MSSQL");
1216
if (string.IsNullOrWhiteSpace(connectionString))
1317
{
1418
Console.WriteLine("Please set the MSSQL environment variable (for example in a .env file). Exiting.");
1519
return;
1620
}
1721

22+
// Create SQL connection
1823
await using var connection = new SqlConnection(connectionString);
19-
await connection.OpenAsync();
2024

2125
// Confirm database project objects exist
2226
var tableExists = await connection.ExecuteScalarAsync<int?>(
@@ -54,35 +58,34 @@
5458
{
5559
// compute embedding
5660
var vector = embeddingClient.GetEmbedding(np.Content);
57-
var sqlVector = new SqlVector<float>(vector);
5861

5962
// check if post exists for this blog
6063
var existingPostId = await connection.QuerySingleOrDefaultAsync<int?>(
6164
"SELECT PostId FROM dbo.Posts WHERE BlogId = @BlogId and Title = @Title",
62-
new { BlogId = blogId, @Title = np.Title });
65+
new { BlogId = blogId, Title = np.Title });
6366

6467
if (existingPostId.HasValue)
6568
{
6669
await connection.ExecuteAsync(
6770
"UPDATE dbo.Posts SET Content = @Content, Embedding = @Embedding WHERE PostId = @PostId",
68-
new { np.Content, Embedding = sqlVector, PostId = existingPostId.Value }
71+
new { np.Content, Embedding = vector, PostId = existingPostId.Value }
6972
);
7073
}
7174
else
7275
{
7376
await connection.ExecuteAsync(
7477
"INSERT INTO dbo.Posts (Title, Content, Embedding, BlogId) VALUES (@Title, @Content, @Embedding, @BlogId)",
75-
new { np.Title, np.Content, Embedding = sqlVector, BlogId = blogId }
78+
new { np.Title, np.Content, Embedding = vector, BlogId = blogId }
7679
);
7780
}
7881
}
7982

8083
// Query for similar posts
8184
Console.WriteLine("\n----------\n");
82-
string searchPhrase = "I want to use Azure SQL, EF Core and vectors in my app!";
85+
string searchPhrase = "I want to use Azure SQL, Dapper and vectors in my app!";
8386
Console.WriteLine($"Search phrase is: '{searchPhrase}'...");
8487

85-
var queryVector = new SqlVector<float>(embeddingClient.GetEmbedding(searchPhrase));
88+
var queryVector = embeddingClient.GetEmbedding(searchPhrase);
8689

8790
var sql = @"
8891
SELECT TOP (5) p.Title,

DotNet/Dapper/TypeHandler.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Microsoft.Data.SqlClient;
2+
using Dapper;
3+
using Microsoft.Data.SqlTypes;
4+
using Microsoft.Data;
5+
6+
public class VectorTypeHandler: SqlMapper.TypeHandler<float[]>
7+
{
8+
public override float[] Parse(object value)
9+
{
10+
return ((SqlVector<float>)value).Memory.ToArray();
11+
}
12+
13+
public override void SetValue(System.Data.IDbDataParameter parameter, float[]? value)
14+
{
15+
parameter.Value = value is not null ? new SqlVector<float>(value) : DBNull.Value;
16+
((SqlParameter)parameter).SqlDbType = SqlDbTypeExtensions.Vector;
17+
}
18+
}

DotNet/Dapper/content.json

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,38 @@
55
},
66
{
77
"Title": "Vectors with Azure SQL and Dapper",
8-
"Content": "You can store vectors in Azure SQL and query them from Dapper by passing SqlVector parameters to Microsoft.Data.SqlClient."
8+
"Content": "You can store vectors in Azure SQL and query them from Dapper by defining a type handler that uses the new SqlVector type from Microsoft.Data.SqlClient."
99
},
1010
{
1111
"Title": "Dapper and vector search",
1212
"Content": "This sample shows how to use Dapper for vector-enabled queries against Azure SQL (vector column type)."
13+
},
14+
{
15+
"Title": "SQL Server Best Practices",
16+
"Content": "Here are some best practices for using SQL Server in your applications."
17+
},
18+
{
19+
"Title": "Python and Flask",
20+
"Content": "Learn how to build a web app using Python and Flask!"
21+
},
22+
{
23+
"Title": "Django for REST APIs",
24+
"Content": "Create a REST API using Django!"
25+
},
26+
{
27+
"Title": "JavaScript for Beginners",
28+
"Content": "Learn JavaScript from scratch!"
29+
},
30+
{
31+
"Title": "Node vs Rust",
32+
"Content": "Which one should you choose for your next project?"
33+
},
34+
{
35+
"Title": "Pizza or Focaccia?",
36+
"Content": "What's the difference between pizza and focaccia. Learn everything you need to know!"
37+
},
38+
{
39+
"Title": "Chocolate Eggs for your next dessert",
40+
"Content": "Try this delicious recipe for chocolate eggs!"
1341
}
1442
]

0 commit comments

Comments
 (0)