Issue Details (XML | Word | Printable)

Key: DNET-332
Type: Task Task
Status: Open Open
Priority: Major Major
Assignee: Jiri Cincura
Reporter: luis olascoaga
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
.NET Data provider

Test class to insert and read multidimensional array fields

Created: 13/Jul/10 11:24 PM   Updated: 13/Jul/10 11:34 PM
Component/s: ADO.NET Provider
Affects Version/s: 2.5.1, 2.5.2
Fix Version/s: None

Environment: Windows GNU/Linux ubuntu amd


 Description  « Hide
The following tables and the class are to test the solution presented in http://tracker.firebirdsql.org/browse/DNET-331 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();

 }



}


 All   Comments   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
There are no comments yet on this issue.