Advertisement
Guest User

Untitled

a guest
May 19th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.73 KB | None | 0 0
  1. using Npgsql;
  2. using NpgsqlTypes;
  3. using System;
  4. using System.Data;
  5.  
  6. namespace NPgSqlTest
  7. {
  8.   class Program
  9.   {
  10.  
  11. /*
  12.  
  13. -- object definitions in the database
  14.  
  15. CREATE TYPE xy
  16.        AS (x real,
  17.            y real);
  18.  
  19. CREATE FUNCTION public.fxy(_xy xy)
  20.                 RETURNS real
  21. AS
  22. $$
  23. BEGIN
  24.   RETURN _xy.x + _xy.y;
  25. END;
  26. $$
  27. LANGUAGE plpgsql;
  28. */
  29.  
  30.     public class XY
  31.     {
  32.       public float x;
  33.       public float y;
  34.       public XY()
  35.       {
  36.  
  37.       }
  38.     }
  39.  
  40.     static int Main(string[] args)
  41.     {
  42.       NpgsqlConnection.MapCompositeGlobally<XY>("public.xy");
  43.       try
  44.       {
  45.         using (NpgsqlConnection connection = new NpgsqlConnection("Host=*; Port=*; Database=*; Username=*; password=*;"))
  46.         {
  47.          
  48.           connection.Open();
  49.  
  50.           using (NpgsqlCommand command = connection.CreateCommand())
  51.           {
  52.             command.CommandType = CommandType.StoredProcedure;
  53.             command.CommandText = "fxy";
  54.             NpgsqlParameter parameter = new NpgsqlParameter("_xy", NpgsqlDbType.Composite);
  55.             XY xy = new XY();
  56.             xy.x = 7;
  57.             xy.y = 10;
  58.             parameter.Value = xy;
  59.             // commenting the following line out I get the exception
  60.             // when I uncomment it, the exception is gone and everythings works fine
  61.             //parameter.SpecificType = typeof(XY);
  62.             command.Parameters.Add(parameter);
  63.             Console.Out.WriteLine(command.ExecuteScalar());
  64.           }
  65.         }
  66.       }
  67.       catch (Exception exception)
  68.       {
  69.         Console.Error.WriteLine(exception);
  70.         Console.Error.WriteLine();
  71.       }
  72.  
  73.       Console.Out.WriteLine("DONE");
  74.       Console.In.Read();
  75.       return 0;
  76.     }
  77.   }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement