@@ -140,37 +140,107 @@ You can start using the new **vector** type right away. The following examples s
140140> Requires ** Microsoft.Data.SqlClient 6.1.0** or later for native vector support.
141141
142142``` csharp
143- static void InsertNonNullVal (SqlConnection conn )
144- {
145- Console .WriteLine (" Inserting non-null value with SqlDbType" );
146143
147- using SqlCommand command = new SqlCommand (" INSERT INTO dbo.vectors VALUES (@Id, @VectorData)" , conn );
144+ using Microsoft .Data ;
145+ using Microsoft .Data .SqlClient ;
146+ using Microsoft .Data .SqlTypes ;
148147
149- command .Parameters .AddWithValue (" @Id" , 1 );
148+ namespace VectorSampleApp
149+ {
150+ class Program
151+ {
152+ // Set your environment variable or fallback to local server
153+ private static readonly string connectionString =
154+ Environment .GetEnvironmentVariable (" CONNECTION_STR" )
155+ ?? " Server=tcp:localhost,1433;Database=Demo2;Integrated Security=True;TrustServerCertificate=True" ;
150156
151- var vectorParameter = new SqlParameter (" @VectorData" , Microsoft .Data .SqlDbTypeExtensions .Vector );
152- vectorParameter .Value = new Microsoft .Data .SqlTypes .SqlVectorFloat32 (new float [] { 3 . 14159 f , 1 . 61803 f , 1 . 41421 f });
153- command .Parameters .Add (vectorParameter );
157+ private const int VectorDimensions = 3 ;
158+ private const string TableName = " dbo.Vectors" ;
154159
155- command .ExecuteNonQuery ();
156- }
160+ static void Main ()
161+ {
162+ using var connection = new SqlConnection (connectionString );
163+ connection .Open ();
164+ SetupTables (connection , TableName , VectorDimensions );
165+ InsertVectorData (connection , TableName );
166+ ReadVectorData (connection , TableName );
167+ }
157168
158- static void ReadWithGetValue (SqlConnection connection )
159- {
160- Console .WriteLine (" Reading values using GetValue method:" );
169+ private static void SetupTables (SqlConnection connection , string tableName , int vectorDimensionCount )
170+ {
171+ using var command = connection .CreateCommand ();
172+
173+ command .CommandText = $@"
174+ IF OBJECT_ID('{tableName }', 'U') IS NOT NULL DROP TABLE {tableName };
175+ IF OBJECT_ID('{tableName }Copy', 'U') IS NOT NULL DROP TABLE {tableName }Copy;" ;
176+ command .ExecuteNonQuery ();
177+
178+ command .CommandText = $@"
179+ CREATE TABLE {tableName } (
180+ Id INT IDENTITY(1,1) PRIMARY KEY,
181+ VectorData VECTOR({vectorDimensionCount })
182+ );
183+
184+ CREATE TABLE {tableName }Copy (
185+ Id INT IDENTITY(1,1) PRIMARY KEY,
186+ VectorData VECTOR({vectorDimensionCount })
187+ );" ;
188+ command .ExecuteNonQuery ();
189+ }
161190
162- using (SqlCommand commandSourceData = new SqlCommand (" SELECT v FROM dbo.vectors;" , connection ))
163- using (SqlDataReader reader = commandSourceData .ExecuteReader ())
164- while (reader .Read ())
165- {
166- var vector = reader .GetValue (0 );
167- if (vector != null && vector != DBNull .Value )
191+ private static void InsertVectorData (SqlConnection connection , string tableName )
192+ {
193+ using var command = new SqlCommand ($" INSERT INTO {tableName } (VectorData) VALUES (@VectorData)" , connection );
194+ var param = command .Parameters .Add (" @VectorData" , SqlDbTypeExtensions .Vector );
195+
196+ // Insert null using DBNull.Value
197+ param .Value = DBNull .Value ;
198+ command .ExecuteNonQuery ();
199+
200+ // Insert non-null vector
201+ param .Value = new SqlVector <float >(new float [] { 3 . 14159 f , 1 . 61803 f , 1 . 41421 f });
202+ command .ExecuteNonQuery ();
203+
204+ // Insert typed null vector
205+ param .Value = SqlVector <float >.CreateNull (VectorDimensions );
206+ command .ExecuteNonQuery ();
207+
208+ // Prepare once and reuse for loop
209+ command .Prepare ();
210+ for (int i = 0 ; i < 10 ; i ++ )
211+ {
212+ param .Value = new SqlVector <float >(new float []
213+ {
214+ i + 0 . 1 f ,
215+ i + 0 . 2 f ,
216+ i + 0 . 3 f
217+ });
218+ command .ExecuteNonQuery ();
219+ }
220+ }
221+
222+ private static void ReadVectorData (SqlConnection connection , string tableName )
168223 {
169- Console .WriteLine (" Type: " + vector .GetType () + " Element Count: " + ((SqlVectorFloat32 )vector ).Length );
224+ using var command = new SqlCommand ($" SELECT VectorData FROM {tableName }" , connection );
225+ using var reader = command .ExecuteReader ();
170226
171- var values = ((SqlVectorFloat32 )vector ).Values ;
172- Console .WriteLine (" Values: " + string .Join (" , " , values ));
173- }
227+ while (reader .Read ())
228+ {
229+ var sqlVector = reader .GetSqlVector <float >(0 );
230+
231+ Console .WriteLine ($" Type: {sqlVector .GetType ()}, IsNull: {sqlVector .IsNull }, Length: {sqlVector .Length }" );
232+
233+ if (! sqlVector .IsNull )
234+ {
235+ float [] values = sqlVector .Memory .ToArray ();
236+ Console .WriteLine (" VectorData: " + string .Join (" , " , values ));
237+ }
238+ else
239+ {
240+ Console .WriteLine (" VectorData: NULL" );
241+ }
242+ }
243+ }
174244 }
175245}
176246```
@@ -245,7 +315,41 @@ public void getVectorData() throws SQLException {
245315 }
246316}
247317```
248-
318+ Example of selecting vector data from table
319+ ``` java
320+ @Test
321+ public void getVectorData() throws SQLException {
322+ String query = " SELECT v FROM " + AbstractSQLGenerator . escapeIdentifier(tableName);
323+ try (PreparedStatement stmt = connection. prepareStatement(query)) {
324+ try (ResultSet rs = stmt. executeQuery()) {
325+ assertTrue(rs. next(), " No result found for inserted vector." );
326+ ResultSetMetaData meta = rs. getMetaData();
327+ int columnCount = meta. getColumnCount();
328+
329+ while (rs. next()) {
330+ for (int i = 1 ; i <= columnCount; i++ ) {
331+ String columnName = meta. getColumnName(i);
332+ int columnType = meta. getColumnType(i); // from java.sql.Types
333+
334+ Object value = null ;
335+ switch (columnType) {
336+ case Types . VARCHAR:
337+ case Types . NVARCHAR:
338+ value = rs. getString(i);
339+ break ;
340+ case microsoft.sql. Types . VECTOR:
341+ value = rs. getObject(i, microsoft.sql. Vector . class);
342+ }
343+
344+ System . out. println(columnName + " = " + value + " (type: " + columnType + " )" );
345+ }
346+ System . out. println(" ---" );
347+ }
348+ }
349+ }
350+ }
351+ ```
352+
249353### [ Python (mssql-python)] ( #tab/python )
250354
251355With Python, applications can write and read vector data using ` json.loads ` and ` json.dumps ` :
@@ -362,4 +466,4 @@ The **vector** type has the following limitations:
362466
363467- [ Overview of vectors in the SQL Database Engine] ( ../../relational-databases/vectors/vectors-sql-server.md )
364468- [ Intelligent applications] ( /azure/azure-sql/database/ai-artificial-intelligence-intelligent-applications?view=azuresql&preserve-view=true#vector-search )
365- - [ Vector Functions] ( ../functions/vector-functions-transact-sql.md )
469+ - [ Vector Functions] ( ../functions/vector-functions-transact-sql.md )
0 commit comments