Пример #1
0
        public void TestConvertToBoolean()
        {
            object x;
            bool   defaut = true;

            x = true;
            bool i = SqlConvert.To(x, defaut);

            AssertAreEqual(i, true, "Booléen 1");

            x = false;
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, false, "Booléen 2");

            x = 1;
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, true, "Booléen 3");

            x = null;
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Booléen null 4");

            x = "toto";
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Booléen null 5");

            x = DBNull.Value;
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Booléen null 6");
        }
Пример #2
0
        public void TestAsyncDataSet()
        {
            string  sql = @"
SELECT 3 AS id
SELECT 4 as id, 'toto' as name
";
            DataSet dst = null;

            using (ConnectionParam cnn = new ConnectionParam(myconnectionString, timeOut: 200))
            {
                dst = Task.Run(() => cnn.ExecuteDataSetAsync(sql)).Result;
            }

            Assert.IsNotNull(dst, "Resultat ExecuteDataSetAsync ?");
            Assert.AreEqual(dst.Tables.Count, 2, "Resultat ExecuteDataSetAsync Nombre de table KO");
            Assert.AreEqual(dst.Tables[0].Rows.Count, 1, "Resultat ExecuteDataSetAsync T[0] nbRows KO");
            int n = SqlConvert.To(dst.Tables[0].Rows[0], "id", 0);

            Assert.AreEqual(n, 3, "Resultat ExecuteDataSetAsync Table[0].Rows[0][id] == 3 KO");

            Assert.AreEqual(dst.Tables[1].Rows.Count, 1, "Resultat ExecuteDataSetAsync T[1] nbRows KO");
            n = SqlConvert.To(dst.Tables[1].Rows[0], "id", 0);
            Assert.AreEqual(n, 4, "Resultat ExecuteDataSetAsync Table[1].Rows[0][id] == 4 KO");
            string nom = SqlConvert.To(dst.Tables[1].Rows[0], "name", string.Empty);

            Assert.AreEqual(nom, "toto", "Resultat ExecuteDataSetAsync Table[1].Rows[0][name] == toto KO");
        }
Пример #3
0
        private void Stop(DataSet dst)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new CallMeDataset(Stop), dst);
            }
            else
            {
                this.listBox1.Items.Clear();
                int i = 1;
                foreach (DataTable tbl in dst.Tables)
                {
                    this.listBox1.Items.Add($"Table {i++}");
                    foreach (DataRow row in tbl.Rows)
                    {
                        foreach (DataColumn cl in tbl.Columns)
                        {
                            this.listBox1.Items.Add($"Col{cl.ColumnName} = '{SqlConvert.To(row, cl.ColumnName, string.Empty)}'");
                        }
                    }
                }

                this.label1.Text     = "Fini";
                this.timer1.Enabled  = false;
                this.button1.Enabled = true;
            }
        }
Пример #4
0
        public void TestConvertToDateTime()
        {
            object   x;
            DateTime defaut = DateTime.MinValue;

            x = new DateTime(2018, 12, 3, 17, 45, 0);
            DateTime i = SqlConvert.To(x, defaut);

            AssertAreEqual(i, new DateTime(2018, 12, 3, 17, 45, 0), "Date Time 1");

            x = new DateTime(1753, 1, 1);
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, new DateTime(1753, 1, 1), "Date Time 2");

            x = null;
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Date Time null 3");

            x = "toto";
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Date Time null 4");

            x = DBNull.Value;
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Date Time null 5");
        }
Пример #5
0
        public void TestConvertToDataRowVersion()
        {
            DataTable tbl = new DataTable();

            DataRow row = null;
            string  v;

            v = SqlConvert.To(row, "toto", "defaut1");
            AssertAreEqual(v, "defaut1", "To dataRow Null 1");

            v = SqlConvert.To(row, null, "defaut2");
            AssertAreEqual(v, "defaut2", "To dataRow Null 2");

            row = tbl.NewRow();
            v   = SqlConvert.To(row, "toto", "defaut3");
            AssertAreEqual(v, "defaut3", "To dataRow Null 3");

            tbl.Columns.Add("Nom");
            row = tbl.NewRow();
            v   = SqlConvert.To(row, "toto", "defaut4");
            AssertAreEqual(v, "defaut4", "To dataRow Null 4");

            v = SqlConvert.To(row, "Nom", "defaut5");
            AssertAreEqual(v, "defaut5", "To dataRow Null 5");

            row["Nom"] = "Magic";
            v          = SqlConvert.To(row, "Nom", "defaut6");
            AssertAreEqual(v, "Magic", "To dataRow 6");
        }
Пример #6
0
        public void TestConvertToLong()
        {
            object x;
            long   defaut = -1;

            // long
            x = 1;
            long i = SqlConvert.To(x, defaut);

            AssertAreEqual(i, 1, "Long 1");

            x = null;
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Long null 2");

            x = "toto";
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Long null 3");

            x = DBNull.Value;
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Long null 4");

            x = ETestLong.l1;
            ETestLong ti = SqlConvert.To(x, ETestLong.l2);

            AssertAreEqual(ti, ETestLong.l1, "Enum long 5");

            // ULong
            ulong defaut2 = 127;

            x = 1;
            ulong j = SqlConvert.To(x, defaut2);

            AssertAreEqual(j, (ulong)1, "ULong 1");

            x = null;
            j = SqlConvert.To(x, defaut2);
            AssertAreEqual(j, defaut2, "ULong null 2");

            x = "toto";
            j = SqlConvert.To(x, defaut2);
            AssertAreEqual(j, defaut2, "ULong null 3");

            x = DBNull.Value;
            j = SqlConvert.To(x, defaut2);
            AssertAreEqual(j, defaut2, "ULong null 4");

            x = ETestULong.ul1;
            ETestULong sti = SqlConvert.To(x, ETestULong.ul2);

            AssertAreEqual(sti, ETestULong.ul1, "Enum ULong 5");
        }
Пример #7
0
        public void TestConvertToShort()
        {
            object x;
            short  defaut = 255;

            // short
            x = 1;
            short i = SqlConvert.To(x, defaut);

            AssertAreEqual(i, (short)1, "Short 1");

            x = null;
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Short null 2");

            x = "toto";
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Short null 3");

            x = DBNull.Value;
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Short null 4");

            x = ETestShort.s1;
            ETestShort ti = SqlConvert.To(x, ETestShort.s2);

            AssertAreEqual(ti, ETestShort.s1, "Enum Short 5");

            // UShort
            ushort defaut2 = 127;

            x = 1;
            ushort j = SqlConvert.To(x, defaut2);

            AssertAreEqual(j, (ushort)1, "UShort 1");

            x = null;
            j = SqlConvert.To(x, defaut2);
            AssertAreEqual(j, defaut2, "UShort null 2");

            x = "toto";
            j = SqlConvert.To(x, defaut2);
            AssertAreEqual(j, defaut2, "UShort null 3");

            x = DBNull.Value;
            j = SqlConvert.To(x, defaut2);
            AssertAreEqual(j, defaut2, "UShort null 4");

            x = ETestUShort.us1;
            ETestUShort sti = SqlConvert.To(x, ETestUShort.us2);

            AssertAreEqual(sti, ETestUShort.us1, "Enum UShort 5");
        }
Пример #8
0
        public void TestConvertToInt()
        {
            object x;
            int    defaut = -1;

            // int
            x = 1;
            int i = SqlConvert.To(x, defaut);

            AssertAreEqual(i, 1, "Int 1");

            x = null;
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Int null 2");

            x = "toto";
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Int null 3");

            x = DBNull.Value;
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Int null 4");

            x = ETestInt.v1;
            ETestInt ti = SqlConvert.To(x, ETestInt.v2);

            AssertAreEqual(ti, ETestInt.v1, "Enum int 5");

            // UInt
            uint defaut2 = 127;

            x = 1;
            uint j = SqlConvert.To(x, defaut2);

            AssertAreEqual(j, (uint)1, "UInt 1");

            x = null;
            j = SqlConvert.To(x, defaut2);
            AssertAreEqual(j, defaut2, "UInt null 2");

            x = "toto";
            j = SqlConvert.To(x, defaut2);
            AssertAreEqual(j, defaut2, "UInt null 3");

            x = DBNull.Value;
            j = SqlConvert.To(x, defaut2);
            AssertAreEqual(j, defaut2, "UInt null 4");

            x = ETestUInt.ui1;
            ETestUInt sti = SqlConvert.To(x, ETestUInt.ui2);

            AssertAreEqual(sti, ETestUInt.ui1, "Enum UInt 5");
        }
Пример #9
0
        public void TestAsyncScalar()
        {
            string sql = @"
SELECT 3 AS id
";
            int    n   = 0;

            using (ConnectionParam cnn = new ConnectionParam(myconnectionString, timeOut: 200))
            {
                n = SqlConvert.To(Task.Run(() => cnn.ExecuteScalarAsync(sql)).Result, 0);
            }

            Assert.AreEqual(n, 3, "Resultat ExecuteScalarAsync ?");
        }
Пример #10
0
        public void TestConvertToOther()
        {
            object x;
            char   defaut = 'x';

            x = 'a';
            try
            {
                char i = SqlConvert.To(x, defaut);
            }
            catch (ArgumentException ex)
            {
                AssertAreEqual(ex.Message, "Type non géré", "Other 1");
            }
        }
Пример #11
0
        public void TestConvertToGuid()
        {
            object x;
            Guid   defaut = Guid.Empty;

            x = Guid.NewGuid();
            Guid i = SqlConvert.To(x, defaut);

            AssertAreEqual(i, x, "Guid 1");

            x = null;
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Guid null 2");

            x = "toto";
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Guid null 3");

            x = DBNull.Value;
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Guid null 4");
        }
Пример #12
0
        public void TestConvertToNumeric()
        {
            object x;

            // decimal
            decimal defaut = -1m;

            x = 3.5m;
            decimal i = SqlConvert.To(x, defaut);

            AssertAreEqual(i, 3.5m, "Decimal 1");

            x = null;
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Decimal null 2");

            x = "toto";
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Decimal null 3");

            x = DBNull.Value;
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Decimal null 4");

            // double
            double defaut2 = -1;

            x = 3.5m;
            double j = SqlConvert.To(x, defaut2);

            AssertAreEqual(j, 3.5, "Double 1");

            x = null;
            j = SqlConvert.To(x, defaut2);
            AssertAreEqual(j, defaut2, "Double null 2");

            x = "toto";
            j = SqlConvert.To(x, defaut2);
            AssertAreEqual(j, defaut2, "Double null 3");

            x = DBNull.Value;
            j = SqlConvert.To(x, defaut2);
            AssertAreEqual(j, defaut2, "Double null 4");

            // Float
            float defaut3 = -1f;

            x = 3.5f;
            float k = SqlConvert.To(x, defaut3);

            AssertAreEqual(k, 3.5f, "Float 1");

            x = null;
            k = SqlConvert.To(x, defaut3);
            AssertAreEqual(k, defaut3, "Float null 2");

            x = "toto";
            k = SqlConvert.To(x, defaut3);
            AssertAreEqual(j, defaut3, "Float null 3");

            x = DBNull.Value;
            k = SqlConvert.To(x, defaut3);
            AssertAreEqual(j, defaut3, "Float null 4");
        }
Пример #13
0
        public void TestConvertToByte()
        {
            object x;
            byte   defaut = 255;

            // byte
            x = 1;
            byte i = SqlConvert.To(x, defaut);

            AssertAreEqual(i, 1, "Byte 1");

            x = null;
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Byte null 2");

            x = "toto";
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Byte null 3");

            x = DBNull.Value;
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Byte null 4");

            x = ETestByte.b1;
            ETestByte ti = SqlConvert.To(x, ETestByte.b2);

            AssertAreEqual(ti, ETestByte.b1, "Enum Byte 5");

            // SByte
            sbyte defaut2 = 127;

            x = 1;
            sbyte j = SqlConvert.To(x, defaut2);

            AssertAreEqual(j, (sbyte)1, "SByte 1");

            x = null;
            j = SqlConvert.To(x, defaut2);
            AssertAreEqual(j, defaut2, "SByte null 2");

            x = "toto";
            j = SqlConvert.To(x, defaut2);
            AssertAreEqual(j, defaut2, "SByte null 3");

            x = DBNull.Value;
            j = SqlConvert.To(x, defaut2);
            AssertAreEqual(j, defaut2, "SByte null 4");

            x = ETestSByte.sb1;
            ETestSByte sti = SqlConvert.To(x, ETestSByte.sb2);

            AssertAreEqual(sti, ETestSByte.sb1, "Enum SByte 5");

            ////// byte Array
            ////byte[] defaut3 = new byte[3] { 1, 0, 1 };
            ////x = new byte[2] { 1, 0 };
            ////byte[] k = SqlConvert.To(x, defaut3);
            ////Assert.IsNotNull(k, "Byte Array null");
            ////AssertAreEqual(k.Length, 2, "Taille du byte array");
            ////AssertAreEqual(k[0], 1, "Byte Array 1[0]");
            ////AssertAreEqual(k[1], 0, "Byte Array 1[1]");

            ////x = null;
            ////k = SqlConvert.To(x, defaut3);
            ////AssertAreEqual(k, defaut3, "Byte Array Null 2");

            ////x = "toto";
            ////k = SqlConvert.To(x, defaut3);
            ////AssertAreEqual(k, defaut3, "Byte Array Null 3");

            ////x = DBNull.Value;
            ////k = SqlConvert.To(x, defaut3);
            ////AssertAreEqual(k, defaut3, "Byte Array Null 4");
        }