Exemplo n.º 1
0
    public static void cp_Lease_Calc2()
    {
        SqlMetaData[] fields = new SqlMetaData[5];
        fields[0] = new SqlMetaData("LeaseId", SqlDbType.Int);
        fields[1] = new SqlMetaData("LeaseVendor", SqlDbType.NVarChar, 50);
        fields[2] = new SqlMetaData("LeaseNumber", SqlDbType.NVarChar, 50);
        fields[3] = new SqlMetaData("ContactDate", SqlDbType.DateTime);
        fields[4] = new SqlMetaData("TotalAmount", SqlDbType.Money);

        // calculate values

        // set record
        SqlDataRecord record = new SqlDataRecord(fields);

        // start record set
        SqlContext.Pipe.SendResultsStart(record);

        //assemble first record
        record.SetInt32(0, 1001);
        record.SetString(1, "LeaseLeaseLease Inc.");
        record.SetString(2, "123-456-7890");
        record.SetDateTime(3, DateTime.Now);
        record.SetSqlMoney(4, 2000);

        // send row
        SqlContext.Pipe.SendResultsRow(record);


        // assemble second record
        record.SetInt32(0, 1002);
        record.SetString(1, "LeaseLeaseLease Inc.");
        record.SetString(2, "123-456-7891");
        record.SetDateTime(3, DateTime.Now);
        record.SetSqlMoney(4, 4000);

        // send row
        SqlContext.Pipe.SendResultsRow(record);

        // send record set
        SqlContext.Pipe.SendResultsEnd();

        return;
    }
Exemplo n.º 2
0
 public static void SetSqlNullableMoney(this SqlDataRecord sdr, int index, decimal?value)
 {
     if (value.HasValue)
     {
         sdr.SetSqlMoney(index, value.GetValueOrDefault());
     }
     else
     {
         sdr.SetDBNull(index);
     }
 }
        public void SqlRecordFillTest()
        {
            SqlMetaData[] metaData = new SqlMetaData[]
            {
                new SqlMetaData("col1", SqlDbType.Bit),
                new SqlMetaData("col2", SqlDbType.TinyInt),
                new SqlMetaData("col3", SqlDbType.VarBinary, 1000),
                new SqlMetaData("col4", SqlDbType.NVarChar, 1000),
                new SqlMetaData("col5", SqlDbType.DateTime),
                new SqlMetaData("col6", SqlDbType.Float),
                new SqlMetaData("col7", SqlDbType.UniqueIdentifier),
                new SqlMetaData("col8", SqlDbType.SmallInt),
                new SqlMetaData("col9", SqlDbType.Int),
                new SqlMetaData("col10", SqlDbType.BigInt),
                new SqlMetaData("col11", SqlDbType.Real),
                new SqlMetaData("col12", SqlDbType.Decimal),
                new SqlMetaData("col13", SqlDbType.Money),
                new SqlMetaData("col14", SqlDbType.Variant)
            };

            SqlDataRecord record = new SqlDataRecord(metaData);

            for (int i = 0; i < record.FieldCount; i++)
            {
                Assert.Equal($"col{i + 1}", record.GetName(i));
            }

            record.SetBoolean(0, true);
            Assert.Equal(true, record.GetBoolean(0));

            record.SetByte(1, 1);
            Assert.Equal(1, record.GetByte(1));

            byte[] bb1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            byte[] bb2 = new byte[5];
            record.SetSqlBinary(2, new SqlBinary(new byte[0]));
            record.SetBytes(2, 0, bb1, 0, 3);
            record.SetBytes(2, 2, bb1, 6, 3);

            // Verify the length of the byte array
            Assert.Equal(5, record.GetBytes(2, 0, bb2, 0, 5));

            Assert.Equal(5, record.GetBytes(2, 0, null, 0, 0));

            byte[] expected = new byte[] { 1, 2, 7, 8, 9 };
            Assert.Equal <byte>(expected, bb2);

            char[] cb1 = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
            char[] cb2 = new char[5];
            record.SetChars(3, 0, cb1, 0, 3);
            record.SetChars(3, 2, cb1, 4, 3);

            char[] expectedValue = new char[] { 'a', 'b', 'e', 'f', 'g' };
            Assert.Equal(expectedValue.Length, record.GetChars(3, 0, cb2, 0, 5));
            Assert.Equal <char>(expectedValue, new string(cb2, 0, (int)record.GetChars(3, 0, null, 0, 0)));

            record.SetString(3, "");
            string xyz = "xyz";

            record.SetString(3, "xyz");
            Assert.Equal(xyz, record.GetString(3));
            Assert.Equal(xyz.Length, record.GetChars(3, 0, cb2, 0, 5));
            Assert.Equal(xyz, new string(cb2, 0, (int)record.GetChars(3, 0, null, 0, 0)));

            record.SetChars(3, 2, cb1, 4, 3);
            Assert.Equal(5, record.GetChars(3, 0, cb2, 0, 5));

            string interleavedResult = "xyefg";

            Assert.Equal(interleavedResult, new string(cb2, 0, (int)record.GetChars(3, 0, null, 0, 0)));
            Assert.Equal(interleavedResult, record.GetString(3));

            record.SetSqlDateTime(4, SqlDateTime.MaxValue);
            Assert.Equal(SqlDateTime.MaxValue, record.GetSqlDateTime(4));

            record.SetSqlDouble(5, SqlDouble.MaxValue);
            Assert.Equal(SqlDouble.MaxValue, record.GetSqlDouble(5));

            SqlGuid guid = new SqlGuid("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4");

            record.SetSqlGuid(6, guid);
            Assert.Equal(guid, record.GetSqlGuid(6));

            record.SetSqlInt16(7, SqlInt16.MaxValue);
            Assert.Equal(SqlInt16.MaxValue, record.GetSqlInt16(7));

            record.SetSqlInt32(8, SqlInt32.MaxValue);
            Assert.Equal(SqlInt32.MaxValue, record.GetSqlInt32(8));

            record.SetSqlInt64(9, SqlInt64.MaxValue);
            Assert.Equal(SqlInt64.MaxValue, record.GetSqlInt64(9));

            record.SetSqlSingle(10, SqlSingle.MinValue);
            Assert.Equal(SqlSingle.MinValue, record.GetSqlSingle(10));

            record.SetSqlDecimal(11, SqlDecimal.Null);
            record.SetSqlDecimal(11, SqlDecimal.MaxValue);
            Assert.Equal(SqlDecimal.MaxValue, record.GetSqlDecimal(11));

            record.SetSqlMoney(12, SqlMoney.MaxValue);
            Assert.Equal(SqlMoney.MaxValue, record.GetSqlMoney(12));


            // Try adding different values to SqlVariant type
            for (int i = 0; i < record.FieldCount - 1; ++i)
            {
                object valueToSet = record.GetSqlValue(i);
                record.SetValue(record.FieldCount - 1, valueToSet);
                object o = record.GetSqlValue(record.FieldCount - 1);

                if (o is SqlBinary)
                {
                    Assert.Equal <byte>(((SqlBinary)valueToSet).Value, ((SqlBinary)o).Value);
                }
                else
                {
                    Assert.Equal(valueToSet, o);
                }

                record.SetDBNull(record.FieldCount - 1);
                Assert.Equal(DBNull.Value, record.GetSqlValue(record.FieldCount - 1));

                record.SetDBNull(i);
                Assert.Equal(DBNull.Value, record.GetValue(i));
            }
        }
Exemplo n.º 4
0
        public static SqlDataRecord Convert(ExcelSignleRow row, DataSourceNames name)
        {
            try
            {
                if (row == null) throw new ArgumentNullException("data can't be null in convert");
                if (row.PrimaryKeyRequired(name) == false)
                {
                    throw new NullReferenceException($"Primary Key can't be null{name}");
                }
                var record = new SqlDataRecord(Constants.Structures[name]);
                for (var index = 0; index < Constants.Structures[name].Length; index++)
                {
                    var fieldName = Constants.Structures[name][index].Name;
                    var property = row.Properties.TryFirst(o => o.Descriptor.FiledName.Equals(fieldName, StringComparison.OrdinalIgnoreCase));
                    if (property == null)
                        record.SetDBNull(index);
                    else
                    {

                        if (property.Descriptor.Type == System.Data.SqlDbType.DateTime)
                        {
                            if (property.Value.TryToUnixStampDateTime(out long? timestamp))
                            {
                                record.SetInt64(index, timestamp ?? 0);
                            }
                            else
                            {
                                record.SetDBNull(index);
                            }
                        }
                        else if (property.Descriptor.Type == System.Data.SqlDbType.Int)
                        {
                            if (int.TryParse(property.Value?.ToString(), out int val))
                            {
                                record.SetInt32(index, val);
                            }
                            else
                            {
                                record.SetDBNull(index);
                            }
                        }
                        else if (property.Descriptor.Type == System.Data.SqlDbType.Money)
                        {
                            if (decimal.TryParse(property.Value?.ToString(), out decimal money))
                            {
                                record.SetSqlMoney(index, money);
                            }
                            else
                            {
                                record.SetDBNull(index);
                            }
                        }
                        else
                        {
                            record.SetStringIfNullOrEmpty(index, property.Value?.ToString());
                        }
                    }
                }
                return record;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
Exemplo n.º 5
0
    public static void GetSPList(SqlString SPUrl, SqlString list_name)
    {
        SPWebService WebService = new SPWebService(SPUrl.ToString());
        SPList       MyList     = WebService.GetSPList(list_name.ToString());

        List <SqlMetaData> Cols = new List <SqlMetaData>();

        //
        // The logic is to iterate through each ColumDef from the SharePoint list and add a corresponding
        // column metadata for the result set to SQL Server.  Basically, this loop matches up data types
        // instead of having the SQL resultset have all columns with varchar (which probably would have been fine
        // for most purposes)
        //
        foreach (ColumnDef CD in MyList.GetColumnDefs())
        {
            switch (CD.ColType.ToUpper())
            {
            case "TEXT":
                Cols.Add(new SqlMetaData(CD.ColName, SqlDbType.NVarChar, CD.ColLen));
                break;

            case "NUMBER":
                Cols.Add(new SqlMetaData(CD.ColName, SqlDbType.Decimal, 18, 5));
                break;

            case "CURRENCY":
                Cols.Add(new SqlMetaData(CD.ColName, SqlDbType.Money));
                break;

            case "DATETIME":
                Cols.Add(new SqlMetaData(CD.ColName, SqlDbType.DateTime2));
                break;

            case "BOOLEAN":
                Cols.Add(new SqlMetaData(CD.ColName, SqlDbType.Bit));
                break;

            default:
                Cols.Add(new SqlMetaData(CD.ColName, SqlDbType.NVarChar, 4000));
                break;
            }
        }

        SqlPipe pipe = SqlContext.Pipe;

        SqlDataRecord rec = new SqlDataRecord(Cols.ToArray());

        pipe.SendResultsStart(rec);

        int      i;
        decimal  tempdec;
        DateTime tempdt;

        //
        // The logic here is similar to the columns above, but it is being done for each row
        // in the result set.  The list data comes out of the webservice as XML which I left as strings.
        // For each row, it iterates through each column and compares what the datatype is in SharePoint (ColumnDef)
        // and does a conversion for the resultset
        //

        foreach (var listrow in MyList.GetRows())
        {
            i = -1;
            foreach (ColumnDef CD in MyList.GetColumnDefs())
            {
                i++;
                tempdec = 0;
                switch (CD.ColType.ToUpper())
                {
                case "TEXT":
                    rec.SetSqlString(i, listrow[i]);
                    break;

                case "NUMBER":
                    decimal.TryParse(listrow[i], out tempdec);
                    rec.SetSqlDecimal(i, tempdec);
                    break;

                case "CURRENCY":
                    decimal.TryParse(listrow[i], out tempdec);
                    rec.SetSqlMoney(i, tempdec);
                    break;

                case "DATETIME":
                    if (!DateTime.TryParse(listrow[i], out tempdt))
                    {
                        tempdt = new DateTime(1900, 1, 1);
                    }
                    rec.SetSqlDateTime(i, tempdt);
                    break;

                case "BOOLEAN":
                    if (listrow[i].Equals("1"))
                    {
                        rec.SetSqlBoolean(i, true);
                    }
                    else
                    {
                        rec.SetSqlBoolean(i, false);
                    }
                    break;

                default:
                    rec.SetSqlString(i, listrow[i]);
                    break;
                }
            }
            pipe.SendResultsRow(rec);
        }
        pipe.SendResultsEnd();
    }