Test class to insert and read multidimensional array fields
-----------------------------------------------------------

                 Key: DNET-332
                 URL: http://tracker.firebirdsql.org/browse/DNET-332
             Project: .NET Data provider
          Issue Type: Task
         Environment: Windows GNU/Linux ubuntu amd
            Reporter: luis olascoaga
            Assignee: Jiri Cincura


The following tables and the class are to test the solution presented in x to 
insert and read multidimensional array type fields:

CREATE TABLE VECINT (

    ID      INTEGER NOT NULL,

    VECTOR  INTEGER [3:7]

);



CREATE TABLE VECDEC (

    ID      INTEGER NOT NULL,

    VECTOR  DECIMAL(4,2) [1:5]

);



CREATE TABLE VECCHAR (

    ID      INTEGER,

    VECTOR  CHAR(20) [2:6]

);



CREATE TABLE VECVARCHAR (

    ID      INTEGER,

    VECTOR  VARCHAR(10) [3:7]

);





CREATE TABLE MATINT (

    ID      INTEGER NOT NULL,

    MATRIX  INTEGER [2:5,3:7]

);



CREATE TABLE MATDEC (

    ID      INTEGER NOT NULL,

    MATRIX  DECIMAL(4,2) [5:7,1:4]

);



CREATE TABLE MATCHAR (

    ID      INTEGER,

    MATRIX  CHAR(20) [1:5,2:5]

);



CREATE TABLE MATVARCHAR (

    ID      INTEGER,

    MATRIX  VARCHAR(10) [3:6,5:7]

);



using System;

using FirebirdSql.Data.FirebirdClient;



class TestArray{

        

 private static FbConnection Con;       

        

 private static void Conectar(){

  FbConnectionStringBuilder ConStr = new FbConnectionStringBuilder();

  ConStr.ServerType = 0;

  ConStr.Database = "TestArray.fdb";

  ConStr.UserID = "sysdba";

  ConStr.Password = "masterkey";

  ConStr.DataSource = "localhost";

  Con = new FbConnection();

  Con.ConnectionString = ConStr.ToString();

  Con.Open();

 }

 

 private static void ShowVec(string Table,Array Vec){

  int i=1;

  Console.WriteLine("Data for table " + Table);

  foreach(object val in Vec){

   Console.WriteLine("Element(" + i + ") = " + val);

   i++;

  }

  Console.WriteLine("Press any key to continue . . . ");

  Console.ReadKey(true);

  Console.Clear();

 }

 

 public static void Insert(string Table,Array Val){

  string sql = "INSERT INTO " + Table + " Values(@ValId,@ValVec)";

  FbCommand cmd = new FbCommand(sql,Con);       

  cmd.Parameters.Add("@ValId",FbDbType.Integer).Value = 10;

  cmd.Parameters.Add("@ValVec",FbDbType.Array).Value = Val;

  cmd.ExecuteNonQuery();

  cmd.Dispose();

 }

 

 public static void InsertVec(){

  int[] VecI = {10,15,20,25,30};//Vector Integer[3:7]

  decimal[] VecD = {1.5m,2.5m,3.5m,4.5m,5.5m};//Vector Decimal(4,2)[1:5]

  string[] VecC = {"EspaƱa","nuevo","campeon","del","mundo"};//Vector 
Char(20)[2:6]

  string[] VecVC = {"Spain","new","world","champion","2010"};//Vector 
VarChar(10)[3:7]

  Insert("VecInt",VecI);

  Insert("VecDec",VecD);

  Insert("VecChar",VecC);

  Insert("VecVarChar",VecVC);

 }

 

 public static Array CreateMat(Type tp,int nf,int nc){

  int i,j,val;  

  Random R = new Random();

  Array Vec = Array.CreateInstance(tp,nf,nc);

  for(i=0;i<nf;i++)

   for(j=0;j<nc;j++){

        val = R.Next(1,100);

        if(tp==typeof(int))

         Vec.SetValue(val,i,j);

        else

         if(tp==typeof(string))

          Vec.SetValue("'" + val +"'",i,j);

         else

          Vec.SetValue((decimal)val/10,i,j);

  }

  return Vec;

 }

 

 public static void InsertMat(){

  Insert("MatInt",CreateMat(typeof(int),4,5));//MATRIX Integer[2:5,3:7]

  Insert("MatDec",CreateMat(typeof(decimal),3,4));//Decimal(4,2) [5:7,1:4]

  Insert("MatChar",CreateMat(typeof(string),5,4));//Char(20)[1:5,2:5]

  Insert("MatVarChar",CreateMat(typeof(string),4,3));//VarChar(10)[3:6,5:7]     

 }

 

 public static void ShowDatas(){

  int i;        

  string sql="Select * From {0} Rows 1";

  string []Tabs={"VecInt","VecDec","VecChar","VecVarChar",

                 "MatInt","MatDec","MatChar","MatVarChar"};

  FbCommand cmd = new FbCommand("",Con);

  FbDataReader rd;

  Console.Clear();

  for(i=0;i<Tabs.Length;i++){

   cmd.CommandText=string.Format(sql,Tabs[i]);

   rd = cmd.ExecuteReader();

   if(rd.Read())

    ShowVec(Tabs[i],(Array)rd.GetValue(1));

  }

  cmd.Dispose();

 }

 

 public static void Main(string[] args) {

  Conectar();

  InsertVec();

  InsertMat();

  ShowDatas();

  Con.Close();

  Con.Dispose();

 }



}


-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: 
http://tracker.firebirdsql.org/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

       

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Firebird-net-provider mailing list
Firebird-net-provider@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/firebird-net-provider

Reply via email to