From bba9936ce992a9d87297e711a4dc704888cd823c Mon Sep 17 00:00:00 2001 From: Damir Dobric Date: Mon, 15 Sep 2025 21:44:06 +0200 Subject: [PATCH 1/7] review --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 5f73129..c263db2 100644 --- a/.gitignore +++ b/.gitignore @@ -405,3 +405,4 @@ FodyWeavers.xsd /DotNet/SqlServer.NativeVectorSearch.Samples/Properties/launchSettingsProd.json /DotNet/SqlClient/Properties/launchSettings Empty.json /DotNet/SqlClient/Properties/launchSettingsProd.json +/DotNet/SqlClient/Properties/launchSettings.prod.json From d73b2b05c5d0ef15a99bf1e17a1212373f6390c1 Mon Sep 17 00:00:00 2001 From: Damir Dobric Date: Mon, 15 Sep 2025 21:44:23 +0200 Subject: [PATCH 2/7] review --- DotNet/SqlClient/Program.cs | 29 ++++++++++--------- .../SqlClient/Properties/launchSettings.json | 8 ++--- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/DotNet/SqlClient/Program.cs b/DotNet/SqlClient/Program.cs index e0d2ed5..c4cb8a9 100644 --- a/DotNet/SqlClient/Program.cs +++ b/DotNet/SqlClient/Program.cs @@ -24,6 +24,7 @@ internal class Program private static string _cConnStr; private static string _cEmbeddingModel; private static string _cApiKey; + private static string _cTableName = "Vectors2"; static Program() { @@ -38,10 +39,10 @@ static async Task Main(string[] args) Console.WriteLine("Hello, World!"); //await CreateAndInsertVectorsAsync(); - //await CreateAndInsertEmbeddingAsync(); - //await ReadVectorsAsync(); - // await FindSimilarAsync(); - //await GenerateTestDocumentsAsync(); + await CreateAndInsertEmbeddingAsync(); + await ReadVectorsAsync(); + await FindSimilarAsync(); + await GenerateTestDocumentsAsync(); await ClassifyDocumentsAsync(); @@ -58,17 +59,17 @@ public static async Task CreateAndInsertVectorsAsync() using (SqlConnection connection = new SqlConnection(_cConnStr)) { // Vector is inserted in the column '[VectorShort] VECTOR(3) NULL' - string sql = $"INSERT INTO [test].[Vectors] ([VectorShort]) VALUES (@Vector)"; + string sql = $"INSERT INTO [test].[{_cTableName}] ([VectorShort]) VALUES (@Vector)"; SqlCommand command1 = new SqlCommand(sql, connection); // Insert vector as string. Note JSON array. - command1.Parameters.AddWithValue("@Vector", "[1.12, 2.22, 3.33]"); + command1.Parameters.AddWithValue("@Vector", "[7.12, -2.22, 3.33]"); SqlCommand command2 = new SqlCommand(sql, connection); // Insert vector as JSON string serialized from the float array. - command2.Parameters.AddWithValue("@Vector", JsonSerializer.Serialize(new float[] { 1.12f, 2.22f, 3.33f })); + command2.Parameters.AddWithValue("@Vector", JsonSerializer.Serialize(new float[] { 4.12f, 22.22f, -3.33f })); connection.Open(); @@ -106,7 +107,7 @@ public static async Task CreateAndInsertEmbeddingAsync() var id = Guid.NewGuid().ToString(); // Embedding is inserted in the column '[Vector] VECTOR(1536) NULL' - SqlCommand command = new SqlCommand($"INSERT INTO [test].[Vectors] ([Vector], [Text]) VALUES ( @Vector, @Text)", connection); + SqlCommand command = new SqlCommand($"INSERT INTO [test].[{_cTableName}] ([Vector], [Text]) VALUES ( @Vector, @Text)", connection); command.Parameters.AddWithValue("@Vector", JsonSerializer.Serialize(embeddingVector.ToArray())); command.Parameters.AddWithValue("@Text", text); @@ -132,7 +133,7 @@ public static async Task GenerateRandomVectorsAsync(int howMany = 10000) for (int i = 0; i < howMany; i++) { - string sql = $"INSERT INTO [test].[Vectors] ([Vector],[Text]) VALUES (@Vector, @Text)"; + string sql = $"INSERT INTO [test].[{_cTableName}] ([Vector],[Text]) VALUES (@Vector, @Text)"; SqlCommand command1 = new SqlCommand(sql, connection); @@ -160,7 +161,7 @@ public static async Task ReadVectorsAsync() { var id = Guid.NewGuid().ToString(); - SqlCommand command = new SqlCommand($"Select TOP(100) * FROM [test].[Vectors]", connection); + SqlCommand command = new SqlCommand($"Select TOP(100) * FROM [test].[{_cTableName}]", connection); connection.Open(); @@ -204,7 +205,7 @@ public static async Task FindSimilarAsync() { var id = Guid.NewGuid().ToString(); - SqlCommand command = new SqlCommand($"Select TOP(100) Id, Text, VECTOR_DISTANCE('cosine', CAST(@Embedding AS Vector(3)), VectorShort) AS Distance FROM [test].[Vectors]", connection); + SqlCommand command = new SqlCommand($"Select TOP(100) Id, Text, VECTOR_DISTANCE('cosine', CAST(@Embedding AS Vector(3)), VectorShort) AS Distance FROM [test].[{_cTableName}]", connection); command.Parameters.AddWithValue("@Embedding", JsonSerializer.Serialize(embedding)); @@ -354,7 +355,7 @@ public static async Task ClassificationByDocumentCountry_Test() { var id = Guid.NewGuid().ToString(); - SqlCommand command = new SqlCommand($"Select TOP({howMany}) Id, Text, VECTOR_DISTANCE('cosine', CAST(@Embedding AS Vector(1536)), Vector) AS Distance FROM [test].[Vectors] ORDER BY DISTANCE", connection); + SqlCommand command = new SqlCommand($"Select TOP({howMany}) Id, Text, VECTOR_DISTANCE('cosine', CAST(@Embedding AS Vector(1536)), Vector) AS Distance FROM [test].[{_cTableName}] ORDER BY DISTANCE", connection); command.Parameters.AddWithValue("@Embedding", JsonSerializer.Serialize(embeddingVector.ToArray())); @@ -412,7 +413,7 @@ public async Task LookupNearest(int loops = 100) { connection.Open(); - SqlCommand command = new SqlCommand($"Select TOP(10) Id, VECTOR_DISTANCE('cosine', CAST(@Embedding AS Vector(1536)), Vector) AS Distance FROM [test].[Vectors] ORDER BY DISTANCE", connection); + SqlCommand command = new SqlCommand($"Select TOP(10) Id, VECTOR_DISTANCE('cosine', CAST(@Embedding AS Vector(1536)), Vector) AS Distance FROM [test].[{_cTableName}] ORDER BY DISTANCE", connection); command.Parameters.AddWithValue("@Embedding", JsonSerializer.Serialize(vector)); @@ -536,7 +537,7 @@ private static async Task InsertVector(string text) using (SqlConnection connection = new SqlConnection(_cConnStr)) { - string sql = $"INSERT INTO [test].[Vectors] ([Vector], [Text]) VALUES (@Vector, @Text)"; + string sql = $"INSERT INTO [test].[{_cTableName}] ([Vector], [Text]) VALUES (@Vector, @Text)"; SqlCommand command1 = new SqlCommand(sql, connection); diff --git a/DotNet/SqlClient/Properties/launchSettings.json b/DotNet/SqlClient/Properties/launchSettings.json index 1d63a81..03f229d 100644 --- a/DotNet/SqlClient/Properties/launchSettings.json +++ b/DotNet/SqlClient/Properties/launchSettings.json @@ -3,10 +3,10 @@ "SqlServer.NativeVectorSearch.Samples": { "commandName": "Project", "environmentVariables": { - "ApiKey": "***", - "EmbeddingModelName": "***", - "SqlConnStr": "***" + "ApiKey": "", + "EmbeddingModelName": "", + "SqlConnStr": "" } } } -} \ No newline at end of file +}a \ No newline at end of file From b54059b10eb0617b2e58f2f26f1db67c45f303ac Mon Sep 17 00:00:00 2001 From: Damir Dobric Date: Tue, 30 Sep 2025 15:31:39 +0200 Subject: [PATCH 3/7] package upgrade --- DotNet/SqlClient/SqlServer.NativeVectorSearch.Samples.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DotNet/SqlClient/SqlServer.NativeVectorSearch.Samples.csproj b/DotNet/SqlClient/SqlServer.NativeVectorSearch.Samples.csproj index 3273f6a..d1ecb4d 100644 --- a/DotNet/SqlClient/SqlServer.NativeVectorSearch.Samples.csproj +++ b/DotNet/SqlClient/SqlServer.NativeVectorSearch.Samples.csproj @@ -8,8 +8,8 @@ - - + + From 9162e28d28812e4ef14abc5f27defa32978dd885 Mon Sep 17 00:00:00 2001 From: Damir Dobric Date: Thu, 2 Oct 2025 20:59:36 +0200 Subject: [PATCH 4/7] Insert Vector of new type SqlVector --- DotNet/SqlClient/Program.cs | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/DotNet/SqlClient/Program.cs b/DotNet/SqlClient/Program.cs index 1bdf055..5ce06d7 100644 --- a/DotNet/SqlClient/Program.cs +++ b/DotNet/SqlClient/Program.cs @@ -1,9 +1,12 @@ -using System.Diagnostics; -using System.Globalization; -using System.Text.Json; -using System.Text; +using Microsoft.Data; using Microsoft.Data.SqlClient; +using Microsoft.Data.SqlTypes; using OpenAI.Embeddings; +using System.Data; +using System.Diagnostics; +using System.Globalization; +using System.Text; +using System.Text.Json; namespace SqlServer.NativeVectorSearch.Samples { @@ -38,7 +41,7 @@ static async Task Main(string[] args) { Console.WriteLine("Hello, World!"); - //await CreateAndInsertVectorsAsync(); + await CreateAndInsertVectorsAsync(); await CreateAndInsertEmbeddingAsync(); await ReadVectorsAsync(); await FindSimilarAsync(); @@ -61,21 +64,25 @@ public static async Task CreateAndInsertVectorsAsync() // Vector is inserted in the column '[VectorShort] VECTOR(3) NULL' string sql = $"INSERT INTO [test].[{_cTableName}] ([VectorShort]) VALUES (@Vector)"; - SqlCommand command1 = new SqlCommand(sql, connection); + SqlCommand command = new SqlCommand(sql, connection); - // Insert vector as string. Note JSON array. - command1.Parameters.AddWithValue("@Vector", "[7.12, -2.22, 3.33]"); + // Demonstrates how to use the new SqlVector type to insert the vector. + var prm = command.Parameters.AddWithValue("@Vector", new SqlVector(new float[] { 7.01f, 7.02f, -7.03f })); - SqlCommand command2 = new SqlCommand(sql, connection); + // Alternative way how to add the vector parameter. + //var prm = command.Parameters.Add("@Vector", SqlDbTypeExtensions.Vector); + //prm.Value= new SqlVector( new float[] { 7.01f, 7.02f, -7.03f }); + // OBSOLETE:. + // Used for compatibility with the old driver Microsoft.Data.SqlClient: Version < 6.1.0. + // Insert vector as string. Note JSON array. + //command.Parameters.AddWithValue("@Vector", "[7.12, -2.22, 3.33]"); // Insert vector as JSON string serialized from the float array. - command2.Parameters.AddWithValue("@Vector", JsonSerializer.Serialize(new float[] { 4.12f, 22.22f, -3.33f })); + //command.Parameters.AddWithValue("@Vector", JsonSerializer.Serialize(new float[] { 4.12f, 22.22f, -3.33f })); connection.Open(); - var result1 = await command1.ExecuteNonQueryAsync(); - - var result2 = await command2.ExecuteNonQueryAsync(); + var result = await command.ExecuteNonQueryAsync(); connection.Close(); } From c73389498d2c85c24316aa34ba7654ca1a962a9f Mon Sep 17 00:00:00 2001 From: Damir Dobric Date: Thu, 2 Oct 2025 21:47:04 +0200 Subject: [PATCH 5/7] ReadVectorsAsync() review and update. --- DotNet/SqlClient/Program.cs | 55 ++++++++++++++----- .../SqlClient/Properties/launchSettings.json | 2 +- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/DotNet/SqlClient/Program.cs b/DotNet/SqlClient/Program.cs index 5ce06d7..c2ec31b 100644 --- a/DotNet/SqlClient/Program.cs +++ b/DotNet/SqlClient/Program.cs @@ -41,10 +41,14 @@ static async Task Main(string[] args) { Console.WriteLine("Hello, World!"); - await CreateAndInsertVectorsAsync(); - await CreateAndInsertEmbeddingAsync(); + //await CreateAndInsertVectorsAsync(); + + //await CreateAndInsertEmbeddingAsync(); + await ReadVectorsAsync(); + await FindSimilarAsync(); + await GenerateTestDocumentsAsync(); await ClassifyDocumentsAsync(); @@ -67,14 +71,14 @@ public static async Task CreateAndInsertVectorsAsync() SqlCommand command = new SqlCommand(sql, connection); // Demonstrates how to use the new SqlVector type to insert the vector. - var prm = command.Parameters.AddWithValue("@Vector", new SqlVector(new float[] { 7.01f, 7.02f, -7.03f })); + command.Parameters.AddWithValue("@Vector", new SqlVector(new float[] { 7.01f, 7.02f, -7.03f })); // Alternative way how to add the vector parameter. //var prm = command.Parameters.Add("@Vector", SqlDbTypeExtensions.Vector); //prm.Value= new SqlVector( new float[] { 7.01f, 7.02f, -7.03f }); - // OBSOLETE:. - // Used for compatibility with the old driver Microsoft.Data.SqlClient: Version < 6.1.0. + // OBSOLETE: + // Supported for compatibility with the old driver Microsoft.Data.SqlClient: Version < 6.1.0. // Insert vector as string. Note JSON array. //command.Parameters.AddWithValue("@Vector", "[7.12, -2.22, 3.33]"); // Insert vector as JSON string serialized from the float array. @@ -99,7 +103,7 @@ public static async Task CreateAndInsertEmbeddingAsync() // The text to be converted to a vector. string text = "Native Vector Search for SQL Server"; - // Generate the embedding vector. + // Generate the embedding vector by using OpenAI SDK var res = await client.GenerateEmbeddingsAsync(new List() { text }); OpenAIEmbedding embedding = res.Value.First(); @@ -116,7 +120,12 @@ public static async Task CreateAndInsertEmbeddingAsync() // Embedding is inserted in the column '[Vector] VECTOR(1536) NULL' SqlCommand command = new SqlCommand($"INSERT INTO [test].[{_cTableName}] ([Vector], [Text]) VALUES ( @Vector, @Text)", connection); - command.Parameters.AddWithValue("@Vector", JsonSerializer.Serialize(embeddingVector.ToArray())); + command.Parameters.AddWithValue("@Vector", new SqlVector(embeddingVector.ToArray())); + + // + // OBSOLETE: Supported for compatibility with the old driver Microsoft.Data.SqlClient: Version < 6.1.0. + //command.Parameters.AddWithValue("@Vector", JsonSerializer.Serialize(embeddingVector.ToArray())); + command.Parameters.AddWithValue("@Text", text); connection.Open(); @@ -154,9 +163,11 @@ public static async Task GenerateRandomVectorsAsync(int howMany = 10000) } } - + /// /// Demonstrates how to read vectors from the table. + /// Please nite the code is not optimized. It is for demonstration purposes only to understand + /// how to deal with the vector type. /// /// @@ -173,13 +184,31 @@ public static async Task ReadVectorsAsync() connection.Open(); using (SqlDataReader reader = await command.ExecuteReaderAsync()) - { + { while (await reader.ReadAsync()) { + // + // Demonstrates how to read the vector as a native type. + var vectorShort = reader.IsDBNull(reader.GetOrdinal("VectorShort")) ? + SqlVector.CreateNull(3) : + reader.GetSqlVector(reader.GetOrdinal("VectorShort")); + + var vectorEmbedding = reader.IsDBNull(reader.GetOrdinal("Vector")) ? + SqlVector.CreateNull(1536) : + reader.GetSqlVector(reader.GetOrdinal("Vector")); + + // + // Demonstrates ow to get a float array from the vector type. + float[] arrShort = vectorShort.Memory.ToArray(); + float[] arrEmbedding = vectorEmbedding.Memory.ToArray(); + (long Id, string VectorShort, string Vector, string Text) row = new( reader.GetInt32(reader.GetOrdinal("Id")), - reader.IsDBNull(reader.GetOrdinal("VectorShort")) ? "-" : reader.GetString(reader.GetOrdinal("VectorShort")), - reader.IsDBNull(reader.GetOrdinal("Vector")) ? "-" : reader.GetString(reader.GetOrdinal("Vector")).Substring(0, 20) + "...", + string.Join(", ", arrShort), + string.Join(", ", arrEmbedding).Substring(0, Math.Min(75, arrEmbedding.Length)) + " ... ", + // OBSOLETE: Supported for compatibility with the old driver Microsoft.Data.SqlClient: Version < 6.1.0. + //reader.IsDBNull(reader.GetOrdinal("VectorShort")) ? "-" : reader.GetString(reader.GetOrdinal("VectorShort")), + //reader.IsDBNull(reader.GetOrdinal("Vector")) ? "-" : reader.GetString(reader.GetOrdinal("Vector")).Substring(0, 20) + "...", reader.IsDBNull(reader.GetOrdinal("Text")) ? "-" : reader.GetString(reader.GetOrdinal("Text")) ); @@ -192,7 +221,7 @@ public static async Task ReadVectorsAsync() foreach (var row in rows) { - Console.WriteLine($"{row.Id}, {row.Vector}, {row.Text}"); + Console.WriteLine($"{row.Id}, {row.VectorShort}, {row.Vector}, {row.Text}"); } } @@ -290,7 +319,7 @@ public static async Task FindSimilarAsync() /// /// public static async Task ClassifyDocumentsAsync() - { + { var invoicesEng = await GetMatching(20, "invoice total item"); var invoiceGER = await GetMatching(20, "rechnung item gesammt"); diff --git a/DotNet/SqlClient/Properties/launchSettings.json b/DotNet/SqlClient/Properties/launchSettings.json index 03f229d..04a914d 100644 --- a/DotNet/SqlClient/Properties/launchSettings.json +++ b/DotNet/SqlClient/Properties/launchSettings.json @@ -9,4 +9,4 @@ } } } -}a \ No newline at end of file +} \ No newline at end of file From 2c94a173d2b49a9f0371e17302cad791b28b14d7 Mon Sep 17 00:00:00 2001 From: Damir Dobric Date: Thu, 2 Oct 2025 22:05:58 +0200 Subject: [PATCH 6/7] Code Upgrade for driver: Microsoft.Data.SqlClient: Version >= 6.1.0. --- DotNet/SqlClient/Program.cs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/DotNet/SqlClient/Program.cs b/DotNet/SqlClient/Program.cs index c2ec31b..5573d6e 100644 --- a/DotNet/SqlClient/Program.cs +++ b/DotNet/SqlClient/Program.cs @@ -41,9 +41,9 @@ static async Task Main(string[] args) { Console.WriteLine("Hello, World!"); - //await CreateAndInsertVectorsAsync(); + await CreateAndInsertVectorsAsync(); - //await CreateAndInsertEmbeddingAsync(); + await CreateAndInsertEmbeddingAsync(); await ReadVectorsAsync(); @@ -80,9 +80,9 @@ public static async Task CreateAndInsertVectorsAsync() // OBSOLETE: // Supported for compatibility with the old driver Microsoft.Data.SqlClient: Version < 6.1.0. // Insert vector as string. Note JSON array. - //command.Parameters.AddWithValue("@Vector", "[7.12, -2.22, 3.33]"); + // command.Parameters.AddWithValue("@Vector", "[7.12, -2.22, 3.33]"); // Insert vector as JSON string serialized from the float array. - //command.Parameters.AddWithValue("@Vector", JsonSerializer.Serialize(new float[] { 4.12f, 22.22f, -3.33f })); + // command.Parameters.AddWithValue("@Vector", JsonSerializer.Serialize(new float[] { 4.12f, 22.22f, -3.33f })); connection.Open(); @@ -93,7 +93,7 @@ public static async Task CreateAndInsertVectorsAsync() } /// - /// Demonstrates how to create a embedding vector from a string by using the embedding model and how to insert it into the table. + /// Demonstrates how to create a embedding vector and how to insert it into the table. /// /// public static async Task CreateAndInsertEmbeddingAsync() @@ -120,6 +120,7 @@ public static async Task CreateAndInsertEmbeddingAsync() // Embedding is inserted in the column '[Vector] VECTOR(1536) NULL' SqlCommand command = new SqlCommand($"INSERT INTO [test].[{_cTableName}] ([Vector], [Text]) VALUES ( @Vector, @Text)", connection); + // Demonstrates how to use the new SqlVector type to insert the vector. command.Parameters.AddWithValue("@Vector", new SqlVector(embeddingVector.ToArray())); // @@ -153,7 +154,7 @@ public static async Task GenerateRandomVectorsAsync(int howMany = 10000) SqlCommand command1 = new SqlCommand(sql, connection); - command1.Parameters.AddWithValue("@Vector", JsonSerializer.Serialize(CreateRandomVector())); + command1.Parameters.AddWithValue("@Vector", new SqlVector(CreateRandomVector())); command1.Parameters.AddWithValue("@Text", i.ToString("D4")); var result1 = await command1.ExecuteNonQueryAsync(); @@ -184,7 +185,7 @@ public static async Task ReadVectorsAsync() connection.Open(); using (SqlDataReader reader = await command.ExecuteReaderAsync()) - { + { while (await reader.ReadAsync()) { // @@ -243,7 +244,7 @@ public static async Task FindSimilarAsync() SqlCommand command = new SqlCommand($"Select TOP(100) Id, Text, VECTOR_DISTANCE('cosine', CAST(@Embedding AS Vector(3)), VectorShort) AS Distance FROM [test].[{_cTableName}]", connection); - command.Parameters.AddWithValue("@Embedding", JsonSerializer.Serialize(embedding)); + command.Parameters.AddWithValue("@Embedding", new SqlVector(embedding)); connection.Open(); @@ -393,7 +394,7 @@ public static async Task ClassificationByDocumentCountry_Test() SqlCommand command = new SqlCommand($"Select TOP({howMany}) Id, Text, VECTOR_DISTANCE('cosine', CAST(@Embedding AS Vector(1536)), Vector) AS Distance FROM [test].[{_cTableName}] ORDER BY DISTANCE", connection); - command.Parameters.AddWithValue("@Embedding", JsonSerializer.Serialize(embeddingVector.ToArray())); + command.Parameters.AddWithValue("@Embedding", new SqlVector(embeddingVector.ToArray())); connection.Open(); @@ -451,7 +452,7 @@ public async Task LookupNearest(int loops = 100) SqlCommand command = new SqlCommand($"Select TOP(10) Id, VECTOR_DISTANCE('cosine', CAST(@Embedding AS Vector(1536)), Vector) AS Distance FROM [test].[{_cTableName}] ORDER BY DISTANCE", connection); - command.Parameters.AddWithValue("@Embedding", JsonSerializer.Serialize(vector)); + command.Parameters.AddWithValue("@Embedding", new SqlVector(vector)); using (var reader = await command.ExecuteReaderAsync(System.Data.CommandBehavior.CloseConnection)) { @@ -578,7 +579,7 @@ private static async Task InsertVector(string text) SqlCommand command1 = new SqlCommand(sql, connection); // Insert vector as string. Note JSON array. - command1.Parameters.AddWithValue("@Vector", JsonSerializer.Serialize(embeddingVector.ToArray())); + command1.Parameters.AddWithValue("@Vector", new SqlVector(embeddingVector.ToArray())); command1.Parameters.AddWithValue("@Text", text); connection.Open(); @@ -651,7 +652,6 @@ public void VectorSerialization_Tests() Console.WriteLine("----------------------------------------------"); Console.WriteLine($"j: {jsonResults.Average()} s: {stringResults.Average()}"); - } private static string ToVectorJsonString(float[] embedding) From 602ad0a043e79131a1472098d2b055927fc7ba03 Mon Sep 17 00:00:00 2001 From: Damir Dobric Date: Sun, 19 Oct 2025 22:08:19 +0200 Subject: [PATCH 7/7] cast removed --- DotNet/SqlClient/Program.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/DotNet/SqlClient/Program.cs b/DotNet/SqlClient/Program.cs index 5573d6e..2213000 100644 --- a/DotNet/SqlClient/Program.cs +++ b/DotNet/SqlClient/Program.cs @@ -242,7 +242,7 @@ public static async Task FindSimilarAsync() { var id = Guid.NewGuid().ToString(); - SqlCommand command = new SqlCommand($"Select TOP(100) Id, Text, VECTOR_DISTANCE('cosine', CAST(@Embedding AS Vector(3)), VectorShort) AS Distance FROM [test].[{_cTableName}]", connection); + SqlCommand command = new SqlCommand($"Select TOP(100) Id, Text, VECTOR_DISTANCE('cosine', @Embedding, VectorShort) AS Distance FROM [test].[{_cTableName}]", connection); command.Parameters.AddWithValue("@Embedding", new SqlVector(embedding)); @@ -392,7 +392,7 @@ public static async Task ClassificationByDocumentCountry_Test() { var id = Guid.NewGuid().ToString(); - SqlCommand command = new SqlCommand($"Select TOP({howMany}) Id, Text, VECTOR_DISTANCE('cosine', CAST(@Embedding AS Vector(1536)), Vector) AS Distance FROM [test].[{_cTableName}] ORDER BY DISTANCE", connection); + SqlCommand command = new SqlCommand($"Select TOP({howMany}) Id, Text, VECTOR_DISTANCE('cosine', @Embedding, Vector) AS Distance FROM [test].[{_cTableName}] ORDER BY DISTANCE", connection); command.Parameters.AddWithValue("@Embedding", new SqlVector(embeddingVector.ToArray())); @@ -450,7 +450,7 @@ public async Task LookupNearest(int loops = 100) { connection.Open(); - SqlCommand command = new SqlCommand($"Select TOP(10) Id, VECTOR_DISTANCE('cosine', CAST(@Embedding AS Vector(1536)), Vector) AS Distance FROM [test].[{_cTableName}] ORDER BY DISTANCE", connection); + SqlCommand command = new SqlCommand($"Select TOP(10) Id, VECTOR_DISTANCE('cosine', @Embedding AS Vector(1536), Vector) AS Distance FROM [test].[{_cTableName}] ORDER BY DISTANCE", connection); command.Parameters.AddWithValue("@Embedding", new SqlVector(vector));